Timer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "Timer.h"
  2. #include "boardinit.h"
  3. #include "../App/public.h"
  4. #include "../App/xspDataOpt.h"
  5. uint32_t g_iDelayfinish;
  6. volatile uint32_t gTimer_tickt = 0;
  7. TimerInfo g_timeInfo;
  8. void TIMER5_IRQHandler(void)
  9. {
  10. if(timer_flag_get(TIMER5,TIMER_FLAG_UP) != RESET){
  11. g_iDelayfinish = 1;
  12. timer_disable(TIMER5);
  13. timer_flag_clear(TIMER5,TIMER_FLAG_UP);/* 定时器更新中断的标识位需要手动清除 */
  14. }
  15. }
  16. void timer5_init(void)
  17. {
  18. timer_parameter_struct timer_initpara;
  19. rcu_periph_clock_enable(RCU_TIMER5);/* 使能TIMER5时钟 */
  20. /* TIMER5 初始化配置 */
  21. timer_deinit(TIMER5);
  22. timer_initpara.prescaler = 7200-1;/* 时钟预分频系数 10KHz*/
  23. timer_initpara.period = 1000-1;/* 自动重装载值 500-500us 1000-1ms */
  24. timer_initpara.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
  25. timer_init(TIMER5,&timer_initpara);
  26. timer_single_pulse_mode_config(TIMER5,TIMER_SP_MODE_SINGLE);
  27. //timer_enable(TIMER5);
  28. nvic_irq_enable(TIMER5_IRQn, 3, 3);/* TIMER5中断设置,抢占优先级3,子优先级3 */
  29. //timer_interrupt_enable(TIMER5,TIMER_INT_UP);/* 使能更新中断 */
  30. timer_flag_clear(TIMER5,TIMER_FLAG_UP);/* 定时器更新中断的标识位需要手动清除 */
  31. timer_interrupt_disable(TIMER5,TIMER_INT_UP);
  32. timer_counter_value_config(TIMER5,0);
  33. timer_disable(TIMER5);
  34. }
  35. void start_delay_ms(uint32_t delayms)
  36. {
  37. g_iDelayfinish = 0;
  38. //timer_prescaler_config(TIMER5,10800-1);
  39. timer_autoreload_value_config(TIMER5,delayms*10-1);
  40. timer_counter_value_config(TIMER5,0);
  41. timer_enable(TIMER5);
  42. timer_interrupt_enable(TIMER5,TIMER_INT_UP);/* 使能更新中断 */
  43. }
  44. void stop_delay_ms(void)
  45. {
  46. timer_disable(TIMER5);
  47. timer_flag_clear(TIMER5,TIMER_FLAG_UP);/* 定时器更新中断的标识位需要手动清除 */
  48. }
  49. static void display_led_status(uint32_t tickt)
  50. {
  51. static volatile uint8_t ledgreen = 0, ledred=0;
  52. static volatile uint8_t num = 0;
  53. if(0 == g_devMsg.ledgreen) { // 绿灯状态不亮
  54. gpio_bit_set(GPIOC, GPIO_PIN_13);
  55. }
  56. else if(1 == g_devMsg.ledgreen){ // 绿灯慢闪
  57. if(tickt%50000==0) {
  58. if(ledgreen==0) gpio_bit_set(GPIOC, GPIO_PIN_13);
  59. else gpio_bit_reset(GPIOC, GPIO_PIN_13);
  60. ledgreen = !ledgreen;
  61. }
  62. }
  63. else if(2 == g_devMsg.ledgreen) { // 绿灯快闪
  64. if(tickt%20000==0) {
  65. if(ledgreen==0) gpio_bit_set(GPIOC, GPIO_PIN_13);
  66. else gpio_bit_reset(GPIOC, GPIO_PIN_13);
  67. ledgreen = !ledgreen;
  68. }
  69. }
  70. else if(3 == g_devMsg.ledgreen) { // 绿灯常亮
  71. gpio_bit_reset(GPIOC, GPIO_PIN_13);
  72. }
  73. else if((4 == g_devMsg.ledgreen) ||(5 == g_devMsg.ledgreen)){
  74. if( g_devMsg.ledgreen == 5){
  75. if((tickt%30002) == 0){
  76. g_devMsg.ledgreen = 6;
  77. return;
  78. }
  79. }
  80. if(tickt%20000==0) {
  81. if(ledgreen==0) {
  82. gpio_bit_set(GPIOC, GPIO_PIN_13);
  83. gpio_bit_set(GPIOA, GPIO_PIN_0);
  84. }
  85. else {
  86. gpio_bit_reset(GPIOC, GPIO_PIN_13);
  87. gpio_bit_reset(GPIOA, GPIO_PIN_0);
  88. num++;
  89. if(num>g_devMsg.ledred){
  90. g_devMsg.ledgreen = 5;// 上电显示灯已经结束
  91. gpio_bit_set(GPIOC, GPIO_PIN_13);
  92. gpio_bit_set(GPIOA, GPIO_PIN_0);
  93. ledgreen = 1;
  94. }
  95. }
  96. ledgreen = !ledgreen;
  97. }
  98. return;
  99. }
  100. if(0 == g_devMsg.ledred) { // 红灯状态不亮
  101. gpio_bit_set(GPIOA, GPIO_PIN_0);
  102. }
  103. else if(1 == g_devMsg.ledred){ // 红灯慢闪
  104. if(tickt%50000==0) {
  105. if(ledred==0) gpio_bit_set(GPIOA, GPIO_PIN_0);
  106. else gpio_bit_reset(GPIOA, GPIO_PIN_0);
  107. ledred = !ledred;
  108. }
  109. }
  110. else if(2 == g_devMsg.ledred) { // 红灯快闪
  111. if(tickt%20000==0) {
  112. if(ledred==0) gpio_bit_set(GPIOA, GPIO_PIN_0);
  113. else gpio_bit_reset(GPIOA, GPIO_PIN_0);
  114. ledred = !ledred;
  115. }
  116. }
  117. else if(3 == g_devMsg.ledred) { // 红灯常亮
  118. gpio_bit_reset(GPIOA, GPIO_PIN_0);
  119. }
  120. }
  121. void TIMER4_IRQHandler(void)
  122. {
  123. volatile uint8_t status = 0,i = 0,j=0;
  124. volatile uint16_t status1 = 0,data;
  125. if(RESET != timer_flag_get(TIMER4,TIMER_FLAG_UP)){
  126. gTimer_tickt++;
  127. display_led_status(gTimer_tickt);
  128. // deal_gun_status(gTimer_tickt);
  129. #if (SEL_74CH165)
  130. // if((g_timeInfo.bExit == 1) &&((gTimer_tickt-g_timeInfo.exitTick)>=g_timeInfo.delay) ){
  131. // if((0 == g_timeInfo.bRead) && (RESET == gpio_input_bit_get(g_extimsg.csGroup,g_extimsg.extiPin))){
  132. // //exti_interrupt_disable(g_extimsg.extiLine); // 关闭外部中断
  133. // reset_port_pin(PLNALL,USE_PORT_ID);
  134. // delay_ms(3);//延时
  135. // set_port_pin(PLNALL,USE_PORT_ID);
  136. // g_timeInfo.bExit = 0;
  137. // g_timeInfo.bRead = 1;
  138. // }
  139. // }
  140. #else
  141. if((g_timeInfo.bExit == 1) &&((gTimer_tickt-g_timeInfo.exitTick)>=g_timeInfo.delay) ){
  142. status = gpio_input_bit_get(GPIOE,g_extimsg.extiPin);
  143. exti_interrupt_disable(g_extimsg.extiLine); // 关闭外部中断
  144. status1 = (status == 1 ? 0xFFFF : 0x0000);
  145. gpio_bit_set(GPIOD,GPIO_PIN_ALL);
  146. for(j=0;j<10;j++);
  147. for(i=0;i<8;i++){
  148. gpio_bit_reset(GPIOD,1<<i);
  149. gpio_bit_reset(GPIOD,1<<(i+8));
  150. for(j=0;j<10;j++);
  151. data = (status1^ gpio_input_port_get(GPIOE));//
  152. g_timeInfo.srcDa[i] = data&0xFF;
  153. g_timeInfo.srcDa[i+8] = (data>>8)&0xFF;
  154. gpio_bit_set(GPIOD,GPIO_PIN_ALL);// CS
  155. for(j=0;j<10;j++);
  156. }
  157. g_timeInfo.bExit = 0;
  158. g_timeInfo.bRead = 1;
  159. }
  160. #endif
  161. timer_flag_clear(TIMER4,TIMER_FLAG_UP);/* 定时器更新中断的标识位需要手动清除 */
  162. }
  163. }
  164. /*-------------------------------------------------------------------------
  165. * 初始化定时器4
  166. * 10us中断一次
  167. * ------------------------------------------------------------------------*/
  168. void timer4_init(void)
  169. {
  170. timer_parameter_struct timer_initpara;
  171. rcu_periph_clock_enable(RCU_TIMER4);/* 使能TIMER5时钟 */
  172. /* TIMER5 初始化配置 */
  173. timer_deinit(TIMER4);
  174. timer_initpara.prescaler = 71;/* 时钟预分频系数 */
  175. timer_initpara.period = 9;/* 自动重装载值 500-500us 1000-1ms */
  176. timer_init(TIMER4,&timer_initpara);
  177. timer_enable(TIMER4);
  178. nvic_irq_enable(TIMER4_IRQn, 3, 2);/* TIMER5中断设置,抢占优先级3,子优先级3 */
  179. timer_interrupt_enable(TIMER4,TIMER_INT_UP);/* 使能更新中断 */
  180. }