123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include "process.h"
- #include "gpio.h"
- #include "cfg.h"
- #include "ac780x_gpio.h"
- #include "PressureSensor.h"
- #include "adc.h"
- uint16_t g_blinkLedTime; /*LED闪烁频率控制时间*/
- uint16_t g_blinkLedTgtTime; /*LED目标闪烁频率*/
- uint8_t g_mr_Interval = 0; /* 压力传感器更新周期 */
- uint16_t g_print_interval = 0; /* 打印周期 */
- #define STATUS_DETECTINTERVAL (40) /*60ms * 8 == 320ms 去抖动 */
- uint8_t g_runstate; //状态
- uint8_t g_state; //状态
- uint8_t g_exception; //异常,主要指IIC 通信
- uint8_t close_count =0;
- uint8_t open_count=0;
- static void update_state()
- {
- // //判定阀开状态
- // if(config->valvecolse_base <= 0){
- //
- // if(getHalldiff() > (config->valvecolse_base + config->threshold)){
- // //g_state = STATUS_OPEN;
- // if(open_count < 0xFF) open_count++;
- // close_count=0;
- // }else{
- // //g_state = STATUS_CLOSE;
- // open_count=0;
- // if(close_count < 0xFF) close_count++;
- // }
- //
- // }else{
-
- if(getHalldiff() >= (config->valvecolse_base + config->threshold)){
- //g_state = STATUS_CLOSE;
- open_count=0;
- if(close_count < 0xFF) close_count++;
- }else{
- //g_state = STATUS_OPEN;
- if(open_count < 0xFF) open_count++;
- close_count=0;
- }
-
- // }
-
- if(close_count >= 10){
- g_state = STATUS_CLOSE;
- }else if(open_count >= 10){
- g_state = STATUS_OPEN;
- }
-
- if(STATUS_OPEN == g_state){
- g_runstate = STATUS_OPEN;
- }else if(STATUS_CLOSE == g_state){
- g_runstate = STATUS_CLOSE;
- }
-
- if(1 == g_exception){
- g_runstate = STATUS_EXCEPTION;
- }
- }
- void Process_Init(void)
- {
- /*初始化控制变量.*/
- g_blinkLedTime = 0;
- g_blinkLedTgtTime = BLINK_LED_DFTT;
-
- g_mr_Interval = 0;
- g_exception = 0;
-
- g_state = STATUS_CLOSE;
- update_state();
-
- printf("process init \r\n");
- }
- void Process_RunPeriod(void)
- {
- /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
- if (g_blinkLedTime >= g_blinkLedTgtTime)
- {
- g_blinkLedTime = 0;
-
- switch(g_runstate){
- case STATUS_CLOSE:
- REDLED_OFF;
- GREENLED_TOGGLE;
- break;
- case STATUS_OPEN:
- GREENLED_OFF;
- REDLED_TOGGLE;
- break;
- case STATUS_UNKNOW:
- REDLED_OFF;
- GREENLED_ON;
- break;
- default:
- GREENLED_OFF;
- REDLED_ON;
- break;
-
- };
-
- }
-
-
-
- //压力传感器测量请求
- if(g_mr_Interval >= 50){
- g_mr_Interval=0;
- g_exception = PressureSensor_MR();
-
- update_state();
-
- }
-
-
-
- // if(g_print_interval >= 1000){
- // g_print_interval=0;
- // PressureSensor_Print();
- //
- // printf("the NTC temperature: %f \r\n",getTemperature());
- // printf("the DIFF ADC1-ADC2: %d \r\n", getHalldiff());
- // }
-
-
-
- }
- uint8_t Process_GetValveStatus(void)
- {
- return g_state;
- }
- float Process_GetTemperature(void)
- {
- return getTemperature();
- }
- float Process_GetPressure(void)
- {
- float pressure = 0.0;
- float temperature = 0.0;
-
- //PressureSensor_Getvalue(&pressure);
- PressureSensor_Getvalue2(&pressure, &temperature);
-
- return pressure;
-
- }
|