3336 views|9 replies

931

Posts

3

Resources
The OP
 

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:
  1. void timer_config(void) { /* ----------------------------------------------------------------------- TIMER2CLK is 100KHz Timer 2 CLK is 100kHz TIMER2 channel0 duty cycle = (25000/ 50000)* 100 = 50% ----------------------------------------------------------------------- */ timer_oc_parameter_struct timer_ocintpara; //Define data structure timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER2); //Enable clock timer_deinit(TIMER2); //Specify timer/* TIMER configuration timer configuration*/ timer_initpara.prescaler = 719; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 99999; //Timing 1000 milliseconds timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER2,&timer_initpara); timer_interrupt_enable(TIMER2, TIMER_INT_UP); //Enable TIMER2 nvic_irq_enable(TIMER2_IRQn, 0); //Set priority to 0(0-2) /* auto-reload preload enable automatic reload preload enable */ timer_auto_reload_shadow_enable(TIMER2); /* auto-reload preload enable */ timer_enable(TIMER2); }
复制代码
The following is the code for the timer interrupt processing:
  1. void TIMER2_IRQHandler(void) { if(RESET != timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP)) { if(time>0) time--; } timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP); }
复制代码
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:
  1. 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.



This post is from GD32 MCU

Latest reply

Thanks for sharing!  Details Published on 2019-5-18 08:39
 

2056

Posts

0

Resources
2
 
Good sharing, share more of these domestic MCUs so that everyone can use them and improve them
This post is from GD32 MCU
 
 
 

111

Posts

0

Resources
3
 
The timer clock frequency is 144hz.
This post is from GD32 MCU

Comments

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
 
 
 

931

Posts

3

Resources
4
 
This post was last edited by hujj on 2019-5-17 18:52
serialworld posted on 2019-5-17 17:11 The timer clock frequency is 144hz.
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?
This post is from GD32 MCU
 
 
 

111

Posts

0

Resources
5
 
The problem is that the maximum value of the timer is 65536, which is too large.
This post is from GD32 MCU

Comments

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
 
 
 

61

Posts

1

Resources
6
 
How much frequency do you need?
This post is from GD32 MCU

Comments

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
 
Personal signature

intersil Techwell应用工程师

 
 

931

Posts

3

Resources
7
 
serialworld posted on 2019-5-17 20:10 The problem is that the maximum value of the timer is 16 bits, which is 65536. 99999 is too large
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.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
8
 
Li Baiyi posted on 2019-5-17 21:22 How much frequency do you need
Originally I planned to use a 1 second timing, but now I plan to use the original 500 milliseconds of the routine.
This post is from GD32 MCU
 
 
 

172

Posts

0

Resources
9
 
Thanks for sharing!
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
10
 
I changed the value back to 49999, and this time the timing was accurate, 500 milliseconds.
This post is from GD32 MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list