MDR_TIMER1->PSG = 0; // .
Next you need to change the way the signal is generated. As we remember from the previous article, the OCCM bit group of the CH1_CNTRL register is responsible for this. Then we selected the invert mode of the REF signal with CNT == CCR1. Since CCR was = 0 by default, by adjusting the ARR of the main counter, we got the same effect. Now we are recording the number of ticks of the whole period in ARR (0xFFFF), and in CRR we will write the number of unit ticks at the output (pulse duration). And the mode will be 6 (0b110: 1, if DIR = 0 (up), CNT <CCR, otherwise 0;). MDR_TIMER1->ARR = 0xFFFF; // . 0 0xFFFF. MDR_TIMER1->CCR1 = timeEnabledState; // 1 0 - . MDR_TIMER1->CH1_CNTRL = 6<<TIMER_CH_CNTRL_OCCM_Pos; // REF = 1, CNT < CCR1, 0 - CNT >= CCR1;
MDR_TIMER1->CH1_CNTRL2 = TIMER_CH_CNTRL2_CCRRLD; // CCR1 CNT = 0. .
// 65535 = 3·5·17·257. 257 0 - 257 - . => 3*5*17 = 255.
From the calculation we see that walking with an interval of 255, we can go through the whole scale from 0 to 257. So we proceed. We get the following code. // 65535 = 3·5·17·257. 257 0 - 257 - . => 3*5*17 = 255. if (UP_FLAG == 0) PWM_time+=255; // , - . - - . else if (DOWN_FLAG == 0) PWM_time-=255; else if (LEFT_FLAG == 0) PWM_time-=255; else if (RIGHT_FLAG == 0) PWM_time+=255; if (PWM_time < 0) PWM_time = 0; // , "" . else if (PWM_time > 0xFFFF) PWM_time = 0xFFFF;
It remains only to assign a new value. MDR_TIMER1->CCR1 = PWM_time; // .
I will note once again that we don’t touch the period. He is permanent. We change only the pulse duration. The whole interruption will be as follows. int PWM_time = 0xFFFF; // . void Timer2_IRQHandler (void) { MDR_TIMER2->STATUS = 0; // . . //LED1_FLAG = !LED1_FLAG; // , . // 65535 = 3·5·17·257. 257 0 - 257 - . => 3*5*17 = 255. if (UP_FLAG == 0) PWM_time+=255; // , - . - - . else if (DOWN_FLAG == 0) PWM_time-=255; else if (LEFT_FLAG == 0) PWM_time-=255; else if (RIGHT_FLAG == 0) PWM_time+=255; if (PWM_time < 0) PWM_time = 0; // , "" . else if (PWM_time > 0xFFFF) PWM_time = 0xFFFF; MDR_TIMER1->CCR1 = PWM_time; // . }
I deliberately hid an LED inversion string indicating that an interrupt is occurring. Because there is not enough current for 2 LEDs and unwanted ripples appear.Source: https://habr.com/ru/post/270887/
All Articles