soft_flow.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "soft_flow.h"
  2. #include "can_link.h"
  3. #include "soft_can.h"
  4. #include "tim.h"
  5. #include <string.h>
  6. volatile uint32_t g_arr_update_count = {0}; // ARR更新(溢出)次数
  7. volatile uint32_t g_last_ccr_value[2] = {0}; // 最后一次捕获的CCR值
  8. uint32_t flow_raw[2] ={0};
  9. //流量计结构体
  10. flow_hl flow_dev1;
  11. flow_hl flow_dev2;
  12. void Flow_Function()
  13. {
  14. // flow_dev1.speed = ( 1000.0f ) / FlOW_KP *
  15. // ( 1000000.0f /flow_raw[0] ); //ml/min
  16. // //flow_dev1.irq_last_count = flow_dev1.irq_count;
  17. //
  18. // flow_dev2.speed = ( 1000.0f ) / FlOW_KP *
  19. // ( 1000000.0f /flow_raw[0] ); //ml/min
  20. // //flow_dev2.irq_last_count = flow_dev2.irq_count;
  21. uint8_t canTxData[8] = {0};
  22. // memcpy(&canTxData[0], &flow_dev1.speed, 4);
  23. // memcpy(&canTxData[4], &flow_dev2.speed, 4);
  24. // Can_Txmsg_Target_Init(FLOW_SPEED_MSG_ID, canTxData);
  25. // can_send(&hfdcan2);
  26. memcpy(&canTxData[0], &flow_dev1.irq_count, 4);
  27. memcpy(&canTxData[4], &flow_dev2.irq_count, 4);
  28. Can_Txmsg_Target_Init(FLOW_COUNT_MSG_ID, canTxData);
  29. can_send(&hfdcan2);
  30. }
  31. // 输入捕获中断回调函数
  32. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  33. {
  34. if (htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
  35. {
  36. flow_raw[1] = 20000 * g_arr_update_count + HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3) - g_last_ccr_value[0];
  37. g_last_ccr_value[1] = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); // 记录当前CCR1值
  38. g_arr_update_count = 0;
  39. flow_dev2.irq_count++;
  40. }
  41. if (htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
  42. {
  43. flow_raw[0] = 20000 * g_arr_update_count + HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4) - g_last_ccr_value[1];
  44. g_last_ccr_value[0] = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); // 记录当前CCR1值
  45. g_arr_update_count = 0;
  46. flow_dev1.irq_count++;
  47. }
  48. }
  49. bool Start_80_hz = false;
  50. bool Start_10_hz = false;
  51. bool Start_5_hz = false;
  52. bool Start_1_hz = false;
  53. uint32_t time2 = 0,ret;
  54. // 定时器周期溢出(ARR更新)中断回调函数
  55. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
  56. if (htim->Instance == TIM2)
  57. {
  58. g_arr_update_count++; // ARR更新时,计数加1
  59. }
  60. if (htim->Instance == TIM5)
  61. {
  62. static uint8_t time_count = 0;
  63. Start_80_hz = true;
  64. time_count++;
  65. if (time_count % 8 == 0)
  66. {
  67. Start_10_hz = true;
  68. }
  69. if (time_count % 26 == 0)
  70. {
  71. Start_5_hz = true;
  72. }
  73. if (time_count % 80 == 0)
  74. {
  75. Start_1_hz = true;
  76. time_count = 0;
  77. }
  78. }
  79. }