Exemplo n.º 1
0
int main(void) 
{
	int x = 32768;
	
	trim_xtal();
	uart_init(UART1, 115200);
	rtc_init();
	
	printf("pwm test\r\n");
	pwm_init_stopped(TMR0, 12000000, x);
	pwm_init_stopped(TMR1, 12000000, x);
	TMR0->ENBL |= TMR_ENABLE_BIT(TMR0) | TMR_ENABLE_BIT(TMR1);

	for(;;) {
		printf("duty %d = %d%%\r\n", x, ((x * 100 + 32768) / 65536));
		switch(uart1_getc()) {
		case '[': x -= 1; break;
		case ']': x += 1; break;
		case '-': x -= 32; break;
		case '=': x += 32; break;
		case '_': x -= 512; break;
		case '+': x += 512; break;

		case '`': x = 65535 * 0/10; break;
		case '1': x = 65535 * 1/10; break;
		case '2': x = 65535 * 2/10; break;
		case '3': x = 65535 * 3/10; break;
		case '4': x = 65535 * 4/10; break;
		case '5': x = 65535 * 5/10; break;
		case '6': x = 65535 * 6/10; break;
		case '7': x = 65535 * 7/10; break;
		case '8': x = 65535 * 8/10; break;
		case '9': x = 65535 * 9/10; break;
		case '0': x = 65535 * 10/10; break;

		}
		x &= 65535;
		pwm_duty(TMR0, x);
	}
}
Exemplo n.º 2
0
void main ()
{
    setup_timer_3 (T3_DISABLED|T3_DIV_BY_1);
    pwm_init (0);
    pwm_duty (0, 50);
    usb_init ();
    lcd_init ();
    lcd_putc ('\f');
    delay_ms (1);
    lcd_refresh ();

    while (TRUE) {
        usb_task ();
        if (usb_enumerated ()) {
            if (usb_kbhit (1)) {
                rx_msg_len = usb_get_packet (1, &rx_msg, sizeof (rx_msg));
                process_usb_data ();
            }
        }
        lcd_refresh ();
        delay_ms (100);
    }
}
Exemplo n.º 3
0
int main(void)
{
	//prm parses the new pwm value to the pwm_duty function
	double prm;
	
	//newP acts as an intermediate value
    double newP;
	
	//setPoint stores the current voltage setpoint
    double setPoint;
	
	//currVoltage stores the current voltage
	double currVoltage;
	
	//currADC is the voltage at the voltage divider
    double currADC;
	
	//dutyCycle stores the duty cycle as a value between 0 and 1
    double dutyCycle;
	
	init_stdio2uart0();
	init_pwm(); 
	init_adc();
	
	//Create pid_data structure
	pid_data *pid = (pid_data *) malloc(sizeof(pid));
    
	//Set initial values
    pid->dt =  0.0025;
    pid->i = 0;
	pid->error = 0;
	pid->d = 0;
	
    //Set default setpoint and prm value
    setPoint = 5.0f;
	prm = 50.0f;
	
	//Ensure the duty cycle reflects prm
    dutyCycle = prm/256;
	
	//Create the sp variable for comparison purposes
    sp = (uint8_t)(setPoint*10);
	
    initTimer();
	
	//Enable interrupts
	sei();
	
	for(;;)
    {
		//Set the value for the repeat enable value to 0 at the beginning
		cont = 0;
        
		//Retrieve current value for adc        
        //and calculate error relative to setPoint
        currADC = v_load();
        currVoltage = (currADC / GAMMA)+CALIB_ERROR;
        pid->error = setPoint - currVoltage;
        
        //Cache new value of PWM to a variable
        dutyCycle = dutyCycle + calcPid(pid);
		
		//Check if this value is within bounds
		if(dutyCycle < 0) dutyCycle = 0;
		else if(dutyCycle > 0.90) dutyCycle = 0.90;
        newP = dutyCycle * 256;
        
		//Retest with value converted
        if(newP < 0) prm = 0;
        else if(newP > PWM_DUTY_MAX) prm = PWM_DUTY_MAX;
        else prm = newP;
        
        //Update PWM
        pwm_duty((uint8_t)prm);
        pid->erprev = pid->error;
        
		//Cache current voltage for UART and check whether the setpoint has changed
		v = (uint8_t)(currVoltage * 10);
		if(sp != (uint8_t)(setPoint*10)) setPoint = ((double) sp)/10;
		
		while(!cont);
	}
	//free the memory should the loop ever terminate
    free(pid);
    return 0;
}
Exemplo n.º 4
0
void process_usb_data ()
{
    pwm_init (rx_msg.u.enabled);
    pwm_duty (rx_msg.u.pwm1_duty_percent, rx_msg.u.pwm2_duty_percent);
    lcd_refresh ();
}
Exemplo n.º 5
0
Arquivo: mppt.c Projeto: jkoelker/mppt
static void pwm(uint16_t duty)
{
    _pwm = duty;
    pwm_duty(_pwm);
}