You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.6 KiB

4 years ago
  1. #include "t21_t8.h"
  2. #include "led.h"
  3. volatile unsigned char timer_cnt;
  4. extern volatile int motor_jerk_state;
  5. int32_t wait_send_to_motor_time1s=0;
  6. int32_t recv_motor_data_time=0;
  7. int32_t recv_lcd_data_time=0;
  8. int32_t inquire_error_time=0;
  9. volatile uint32_t g_ticket100us = 0;//一直再记录过去的时间
  10. /**
  11. * @brief 32M的情况下初始化时钟1作为ticket源
  12. *
  13. */
  14. void init_timer1_as_ticketustimer_in32M() {
  15. // T11M
  16. /**
  17. * @brief
  18. * 1ms一次中断
  19. * T11CM = 0x0f 16 = 2MHZ
  20. * T11CH = 0 0
  21. * T11PH T11PL = 200 //100us
  22. * T11PH = 0x07
  23. * T11PL = 0xD0
  24. */
  25. T11CL = 0x00; //定时器模式
  26. T11CM = 0x0F; //预分频1:16 //最大1:16
  27. T11CH = 0x00; //后分频次数0+1
  28. static_assert(configTicketUs <= 10000 && configTicketUs >= 100);
  29. uint16_t count = 2000 / 1000 * configTicketUs;
  30. T11PH = count >> 8; //周期值高8位
  31. T11PL = count & 0xFF; // 200
  32. T11VIE = 1; //打开T11溢出中断
  33. T11VIF = 0; //清标志位
  34. T11EN = 1; //使能T11
  35. }
  36. int32_t clear_time()
  37. {
  38. if(wait_send_to_motor_time1s>=UINT32_MAX)
  39. {
  40. wait_send_to_motor_time1s=0;
  41. }
  42. if(recv_motor_data_time>=UINT32_MAX)
  43. {
  44. recv_motor_data_time=0;
  45. }
  46. if(g_ticket100us>=UINT32_MAX)
  47. {
  48. g_ticket100us=0;
  49. }
  50. if(recv_lcd_data_time>=UINT32_MAX)
  51. {
  52. recv_lcd_data_time=0;
  53. }
  54. if(inquire_error_time>=UINT32_MAX)
  55. {
  56. inquire_error_time=0;
  57. }
  58. }
  59. uint32_t hal_has_passed_ticket(uint32_t lastticket) {
  60. uint32_t now = g_ticket100us;
  61. if (now >= lastticket) {
  62. return now - lastticket;
  63. } else {
  64. return UINT32_MAX - lastticket + now;
  65. }
  66. }
  67. uint32_t hal_has_passedms(uint32_t lastticket) {
  68. /**
  69. * ticket的数值进行修改.
  70. */
  71. static_assert(configTicketUs == 1000);
  72. return hal_has_passed_ticket(lastticket);
  73. }
  74. int32_t hal_get_ticket()
  75. {
  76. return g_ticket100us;
  77. }
  78. int32_t hal_get_irqticket()
  79. {
  80. return g_ticket100us;
  81. }
  82. int32_t sleep_ms(int32_t time)
  83. {
  84. /**
  85. * @brief ms级的延时
  86. *
  87. */
  88. int32_t tickstart = hal_get_ticket();
  89. int32_t wait =time;
  90. while(hal_get_ticket()-tickstart<wait)
  91. {
  92. }
  93. }
  94. void hal_ticket_inc()
  95. {
  96. g_ticket100us++;
  97. wait_send_to_motor_time1s++;
  98. recv_motor_data_time++;
  99. recv_lcd_data_time++;
  100. inquire_error_time++;
  101. }
  102. void system_module_process_interrupt() {
  103. /**
  104. * @brief 11
  105. */
  106. if (T11VIE == 1 && T11VIF == 1) //定时器溢出中断
  107. {
  108. hal_ticket_inc();
  109. T11VIF = 0; //清标志位
  110. }
  111. }