GD32E231 DIY Competition (8) - Complete the driver of timer 2[Copy link]
In order to control the length of the alarm sound, I used the function of timer 2. The specific method is to first configure timer 2 to count for one second, then assign the set time length to a variable, and decrement the variable by 1 in the timer interrupt processing. When the variable is equal to 0, turn off the alarm sound. Below is the timer configuration code:
Set the alarm mode in the external interrupt handler, turn on the power of the music IC when the alarm flag is obtained in the main loop, and turn off the power of the music IC after the time is reached:
if(mode==110){ alar++; //Alarm count LCD_line_clear(3); LCD_line_clear(4); LCD_write_string(0,3,(uint8_t *) "Alarm record"); LCD_write_value(53,4,3,0,1,alar); date_write(); //Add alarm record LCD_write_value(18,2,3,0,1,recn); LCD_write_value(60,2,3,0,1,addr); mode = 0; if(ala8==4){ //Start music gpio_bit_set(GPIOA, GPIO_PIN_2);//Send start signal delay_1ms(100); gpio_bit_reset(GPIOA, GPIO_PIN_2);//Cancel start signal } else{ if(ala8>0) //Specify alarm sound alarm_type(ala8-1); time = ala9; //Alarm sound duration gpio_bit_reset(GPIOA, GPIO_PIN_3);//Start alarm sound gd_eval_led_off(LED2); } } if(time==0){ gpio_bit_set(GPIOA, GPIO_PIN_3); //Turn off the alarm sound gd_eval_led_off(LED2); } else LCD_write_value(53,3,3,0,0,time); //Show the remaining time
复制代码
After testing, the program code basically meets the requirements, but I configured the timer time to be one second, but the actual test found that it was less than one second, about 0.5 seconds, and the reason has not been found yet.
timer_initpara.period = 99999; //1000 milliseconds timing. In the original example, the timing was 500 milliseconds, and the value was 49999. I thought that changing it to 99999 would allow the timing to be 1000 milliseconds.
Details
Published on 2019-5-17 18:51
timer_initpara.period = 99999; //1000 milliseconds timing The original example was 500 milliseconds timing, and this value is 49999. I thought changing it to 99999 would allow 1000 milliseconds timing. How do I calculate it?
Thanks for your advice! I didn't notice this problem. No wonder it only took more than half a second. I changed it back to 5000 milliseconds and tried again.
Details
Published on 2019-5-18 06:53
The original preparation was to use a 1 second timing, but now the preparation is still to use the original 500 milliseconds of the routine.
Details
Published on 2019-5-18 06:55
Thanks for your advice! I didn't notice this problem. No wonder it only takes about half a second. I'll change it back to 5000 milliseconds and try again.