Alloy
Advanced Member level 4
Hey
I want to run PWM on PIC32MX.
I know that I must:
- select PPS (Peripheral Pin Select) for PWM
- setup PWM
I think I did that but it does not work, what have I missed?
PS: The blink LED code is working, I just can't get PWMs to work.
I want to run PWM on PIC32MX.
I know that I must:
- select PPS (Peripheral Pin Select) for PWM
- setup PWM
I think I did that but it does not work, what have I missed?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 unsigned int current_duty, old_duty, current_duty1, old_duty1; unsigned int pwm_period1, pwm_period2; void main() { JTAGEN_bit = 0; // Disable JTAG /* ADC Digital Mode */ ANSELA = 0; ANSELB = 0; TRISA = 0; // Initialize PORTA as output TRISB = 0; // Initialize PORTB as output LATA = 0; // Set PORTA to zero LATB = 0; // Set PORTB to zero Unlock_IOLOCK(); PPS_Mapping(_RPA0, _OUTPUT, _C1OUT); PPS_Mapping(_RPA1, _OUTPUT, _C2OUT); Lock_IOLOCK(); current_duty = 100; // initial value for current_duty current_duty1 = 100; // initial value for current_duty1 pwm_period1 = PWM_Init(5000 , 1, 1, 2); pwm_period2 = PWM_Init(10000, 2, 1, 3); PWM_Start(1); PWM_Start(2); PWM_Set_Duty(current_duty, 1); // Set current duty for PWM1 PWM_Set_Duty(current_duty1, 2); // Set current duty for PWM2 while(1) { Delay_ms(1); current_duty = current_duty + 5; // increment current_duty if (current_duty > pwm_period1) { // if we increase current_duty greater then possible pwm_period1 value current_duty = 0; // reset current_duty value to zero } PWM_Set_Duty(current_duty, 1); // set newly acquired duty ratio current_duty1 = current_duty1 + 5; // increment current_duty if (current_duty1 > pwm_period2) { // if we increase current_duty greater then possible pwm_period1 value current_duty1 = 0; // reset current_duty value to zero } PWM_Set_Duty(current_duty1, 2); // set newly acquired duty ratio } }
PS: The blink LED code is working, I just can't get PWMs to work.