process.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "process.h"
  2. #include "gpio.h"
  3. #include "cfg.h"
  4. #include "ac780x_gpio.h"
  5. #include "PressureSensor.h"
  6. #include "adc.h"
  7. uint16_t g_blinkLedTime; /*LED闪烁频率控制时间*/
  8. uint16_t g_blinkLedTgtTime; /*LED目标闪烁频率*/
  9. uint16_t g_mr_count = 0; /* 压力传感器更新计数 */
  10. uint16_t g_stateupdate_count = 0; /* 状态更新计数 */
  11. uint16_t g_print_interval = 0; /* 打印周期 */
  12. #define STATUS_DETECTINTERVAL (40) /*60ms * 8 == 320ms 去抖动 */
  13. uint8_t g_runstate; //状态
  14. uint8_t g_state; //状态
  15. uint8_t g_exception; //异常,主要指IIC 通信
  16. uint8_t close_count =0;
  17. uint8_t open_count=0;
  18. static void update_state(void)
  19. {
  20. // //判定阀开状态
  21. if(config->valvecolse_base == 0){ // 未标定状态
  22. g_state = STATUS_UNKNOW;
  23. }else{
  24. if(getHalldiff() >= (config->valvecolse_base + config->threshold)){
  25. //g_state = STATUS_CLOSE;
  26. open_count=0;
  27. if(close_count < 0xFF) close_count++;
  28. }else{
  29. //g_state = STATUS_OPEN;
  30. if(open_count < 0xFF) open_count++;
  31. close_count=0;
  32. }
  33. }
  34. if(close_count >= 10){
  35. g_state = STATUS_CLOSE;
  36. }else if(open_count >= 10){
  37. g_state = STATUS_OPEN;
  38. }
  39. if(STATUS_OPEN == g_state){
  40. g_runstate = STATUS_OPEN;
  41. }else if(STATUS_CLOSE == g_state){
  42. g_runstate = STATUS_CLOSE;
  43. }else {
  44. g_runstate = STATUS_UNKNOW;
  45. }
  46. if(1 == g_exception){
  47. g_runstate = STATUS_EXCEPTION;
  48. }
  49. }
  50. void Process_Init(void)
  51. {
  52. /*初始化控制变量.*/
  53. g_blinkLedTime = 0;
  54. g_blinkLedTgtTime = BLINK_LED_DFTT;
  55. g_mr_count = 0;
  56. g_exception = 0;
  57. g_state = STATUS_UNKNOW;
  58. update_state();
  59. printf("process init \r\n");
  60. }
  61. void Process_Timer1_Callback(void)
  62. {
  63. if(g_blinkLedTime < 0xFFFF)
  64. {
  65. g_blinkLedTime++;
  66. }
  67. if(g_mr_count < 0xFFFF){
  68. g_mr_count++;
  69. }
  70. if(g_stateupdate_count < 0xFFFF){
  71. g_stateupdate_count++;
  72. }
  73. if(g_print_interval < 0xFFFF){
  74. g_print_interval++;
  75. }
  76. }
  77. void Process_RunPeriod(void)
  78. {
  79. /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
  80. if (g_blinkLedTime >= g_blinkLedTgtTime)
  81. {
  82. g_blinkLedTime = 0;
  83. switch(g_runstate){
  84. case STATUS_CLOSE:
  85. REDLED_OFF;
  86. GREENLED_TOGGLE;
  87. break;
  88. case STATUS_OPEN:
  89. GREENLED_OFF;
  90. REDLED_TOGGLE;
  91. break;
  92. case STATUS_UNKNOW:
  93. REDLED_OFF;
  94. GREENLED_ON;
  95. break;
  96. default:
  97. GREENLED_OFF;
  98. REDLED_ON;
  99. break;
  100. };
  101. }
  102. //压力传感器测量请求
  103. if(g_mr_count >= 500){
  104. g_mr_count=0;
  105. g_exception = PressureSensor_Read();
  106. //PressureSensor_Read();
  107. //update_state();
  108. }else if(g_mr_count == 480){
  109. PressureSensor_MR();
  110. //update_state();
  111. }
  112. if(g_stateupdate_count >= 100){
  113. update_state();
  114. }
  115. // if(g_print_interval >= 1000){
  116. // g_print_interval=0;
  117. // PressureSensor_Print();
  118. //
  119. // printf("the NTC temperature: %f \r\n",getTemperature());
  120. // printf("the DIFF ADC1-ADC2: %d \r\n", getHalldiff());
  121. // }
  122. }
  123. uint8_t Process_GetValveStatus(void)
  124. {
  125. return g_state;
  126. }
  127. float Process_GetTemperature(void)
  128. {
  129. return getTemperature();
  130. }
  131. float Process_GetPressure(void)
  132. {
  133. float pressure = 0.0;
  134. float temperature = 0.0;
  135. //PressureSensor_Getvalue(&pressure);
  136. PressureSensor_Getvalue2(&pressure, &temperature);
  137. return pressure;
  138. }