How to solve the problem that two mutually exclusive flags in the business logic code of STM8S003K3 both execute the same function
[Copy link]
First of all, this is a program controlled by a faucet. The main control board reports data to the display screen driven by ht1621b through the serial port. The display screen will display different icons according to the serial port only after receiving the data from the serial port. The problematic program is attached below. First, after booting up, there is a touch IC button on the display screen, and the icons on the display screen flash in turn (this process is the faucet exhaust process) until the button is pressed for 10 seconds to exit the exhaust. After exiting the exhaust, the water can be discharged in first and second gears by the STM8S to control the booster pump IO port output (the first gear is the half-pressure output controlled by PWM, and the second gear is the full-pressure output). After the booster pump has no work for xmin (x represents the number of minutes), it enters the automatic flushing state. In this state, the booster pump can work in the first gear to clean the water in the water tank. There are two conditions for exiting: one is water shortage, and the other is that the water pump is idling for 5s. Now it can exit when there is water shortage, but the water pump idling for 5s collects data through AD current, but the conditions for exiting when the water pump is idling for 5s and water shortage are different, and SystemStatus.work_mode (working mode: first and second gear water discharge) and SystemStatus.auto_clean (self-cleaning mode flag) are mutually exclusive. SystemStatus.zyb_mode (boost pump is PWM half-pressure output or full-pressure output) belongs to a subset of SystemStatus.work_mode. I don't know how to execute both conditions, but the functions executed are similar. The problematic codes are written below, but I don't know how to solve them?
- * Main control board status information */
- typedef struct waterbox_info
- {
- uint8_t tds;
- uint16_t temperature;
- uint8_t out; //water out
- uint8_t fullwater;
- uint8_t makewater;
- uint8_t uv;
- uint8_t work_mode;
- uint8_t auto_clean;
- uint8_t err_code; /*Error code: water leakage*/
- uint8_t crc_code;
- uint8_t outair; //Self-exhaust
- uint8_t isEmpty; //No water in the water tank
- uint8_t zyb;
- uint8_t liquid_level; //liquid level protect
- uint8_t leakwater; /*water leakage*/
- uint8_t jump_outair; //skip exhaust
- uint8_t zyb_mode;
- }System_t;
- /* Structure */
- System_t SystemStatus = {0};
- uint16_t Current_Aver_Buf[ZYB_BUF_LEN]={0}; //Boost pump average value buffer length
- uint16_t temp = 0;
- uint16_t temp1 = 0;
- #define ZYB_BUF_LEN 20
- uint16_t get_current_average_value(uint16_t *buf,uint16_t val)
- {
- volatile uint16_t sum = 0,temp = 0;
- volatile uint16_t average = 0;
-
- int i = 0;
- for (i = 0; i < ZYB_BUF_LEN - 1; ++i)
- {
- buf[i] = buf[i+1];
- }
- buf[ZYB_BUF_LEN - 1] = val;
- for (i = 0; i < ZYB_BUF_LEN; ++i)
- {
- sum += buf[i];
- }
- average = sum / ZYB_BUF_LEN;
- return average;
- }
- /*
- *@function:zyb_current_check(void)
- * @brief: Current detection
- *@param:None
- * @retval:None
- */
- void zyb_current_check(void)
- {
-
- if(SystemStatus.zyb_mode != 0x00 || (SystemStatus.auto_clean)) //2 conditions: 1: working mode is 1st or 2nd gear water discharge 2: self-cleaning mode
- {
- if (TIMER_STOP == current_timer.timer_run_flag)
- {
- /* code */
- current_timer.cnt = 0;
- current_timer.timer_run_flag = TIMER_RUN;
- }
- else if (TIMER_END == current_timer.timer_run_flag)
- {
- current_timer.timer_run_flag = TIMER_STOP;
- temp = Get_ADC_Convertion(ZYB_CURRENT); //ADC channel conversion
- temp1 = get_current_average_value(Current_Aver_Buf,temp); // average current value
- }
- /*Booster pump idling detection, booster pump stops working if idling time exceeds 5s*/
- if(zyb_timer.cnt >= 5000)
- {
- if((SystemStatus.zyb_mode == 0x01) && (temp1 < 10)) //50 The PWM duty cycle of the first gear of the water pump is 50%
- {
- zyb_timer.timer_run_flag = TIMER_STOP;
-
- FSF_F2_OFF;
- JSF_F1_OFF;
- PSF_F3_OFF;
-
- Close_ZYB();
-
- SystemStatus.out = 0;
- SystemStatus.work_mode = 0; //0x00: stop discharging water
- SystemStatus.zyb = 0;
-
-
- /* If the booster pump runs idle for 5 seconds, the main control board will exit the self-cleaning mode*/
-
- if(SystemStatus.auto_clean)
- {
- SystemStatus.auto_clean = 0x00;
- }
- }
- else if((SystemStatus.zyb_mode == 0x02)&&(temp1 < 30))//100 The PWM duty cycle of the second-gear water discharge of the water pump is 100%, that is, full pressure output
- {
- zyb_timer.timer_run_flag = TIMER_STOP;
-
- FSF_F2_OFF;
- JSF_F1_OFF;
- PSF_F3_OFF;
- Close_ZYB();
-
- SystemStatus.out = 0;
- SystemStatus.work_mode = 0;
- SystemStatus.zyb = 0;
-
- /* If the booster pump runs idle for 5 seconds, the main control board will exit the self-cleaning mode, and the self-cleaning mode is the second gear water output*/
- if(SystemStatus.auto_clean)
- {
- SystemStatus.auto_clean = 0x00;
- }
-
- }
- }
- }
- }
- /**
- *Function: Auto_Clean_proc
- *Function: Self-cleaning process, if there is no operation for 48 hours, it will start self-cleaning and the button will be invalid
- *Parameters: None
- *Return value: None
- */
- void Auto_Clean_proc(void)
- {
- if(Auto_clean_timer.min >= 10) //48 hours of self-cleaning, Auto_clean_timer.hour >= 48, change the time to 30min
- {
- SystemStatus.auto_clean = AUTO_CLEAN_STATUS; // Self-cleaning is the second gear water output
-
-
-
-
-
- }
- if(SystemStatus.auto_clean && SystemStatus.outair) //Self-cleaning and not self-exhausting
- {
- static uint8_t old_status = 0;
-
- if(SystemStatus.auto_clean && old_status != SystemStatus.auto_clean) //48 hours self-cleaning
- {
- PSF_F3_ON; //Drain valve-F3 open
- JSF_F1_OFF; //Water inlet valve-F1 closed
- FSF_F2_OFF; //Drain valve-F2 is closed
- ///Open_two_mode(); //Water pump discharges water in second gear
- Open_one_mode(); //Test with one mode of water output
-
- }
- SystemStatus.fullwater = 0x00;
- SystemStatus.makewater = 0x00;
- SystemStatus.work_mode = 0x00;
- old_status = SystemStatus.auto_clean;
-
-
- }
- if(TIMER_STOP == Auto_clean_timer.timer_run_flag)
- {
- Auto_clean_timer.timer_run_flag = TIMER_RUN;
- }
- if(SystemStatus.out || !SystemStatus.outair || SystemStatus.auto_clean)
- {
- //SystemStatus.auto_clean =0x00;
- Auto_clean_timer.hour = 0;
- Auto_clean_timer.min = 0;
- Auto_clean_timer.sec = 0 ;
- Auto_clean_timer.cnt = 0;
- }
-
-
- }
- /**
- * Function: HeartBeat_Send
- *Function:
- *Parameter: None
- *Return value: None
- */
- void HeartBeat_Send(void)
- {
- if(TIMER_STOP == HeartBeat_timer.timer_run_flag)
- {
- HeartBeat_timer.timer_run_flag = TIMER_RUN;
- HeartBeat_timer.cnt = 1000; //1s
-
- if(TIMER_RUN == HeartBeat_timer.timer_run_flag)
- {
- uint8_t buf[21] = "AA55"; //
- uint8_t crc_code = 0;
- uint8_t i=0;
-
- buf[4] = (SystemStatus.tds / 16); //tds
- buf[4] = (buf[4] >= 0 &&buf[4]<=9)?buf[4]+'0':buf[4]+0x37;
- buf[5] = (SystemStatus.tds % 16);
- buf[5] = (buf[5] >= 0 &&buf[5]<=9)?buf[5]+'0':buf[5]+0x37;
-
- buf[6] = SystemStatus.out ? '1': '0'; //Water out
- buf[7] = SystemStatus.fullwater&0x40? '1':'0'; //0x40 is short of water
- buf[8] = SystemStatus.fullwater&0x20? '1':'0'; //0x20 water replenishment
- buf[9] = SystemStatus.makewater ? '1': '0'; //Water making mode
- buf[10] = SystemStatus.uv ? '1': '0'; //uv
- buf[11] = SystemStatus.work_mode & 0x04 ? '1':'0'; //Water pump working mode: second gear water discharge
- buf[12] = SystemStatus.work_mode & 0x02 ? '1':'0'; //Water pump working mode: first gear water discharge
- buf[13] = SystemStatus.auto_clean ? '1': '0'; //
-
- // buf[14] = 0; //¤á
- // buf[15] = 0;
- buf[14] = SystemStatus.liquid_level ? '1': '0';
- buf[15] = !SystemStatus.outair ? '1': '0';
- buf[16] = SystemStatus.err_code ? '1': '0'; //Error code
-
-
- for(i=0;i<=16;i++)
- {
- crc_code += buf[i];
- }
- buf[17] = (crc_code / 16);
- buf[17] = (buf[17] >= 0 &&buf[17]<=9)?buf[17]+'0':buf[17]+0x37;
- buf[18] = (crc_code % 16);
- buf[18] = (buf[18] >= 0 &&buf[18]<=9)?buf[18]+'0':buf[18]+0x37;
- buf[19] = '\r';
- buf[20] = '\n';
- //UART1_SendString(buf);
- for(i=0;i<=20;i++)
- {
- while((UART1->SR & UART1_FLAG_TXE)==RESET);
- UART1->DR = buf[i];
- while((UART1->SR & UART1_FLAG_TC)==RESET);
- }
- }
- }
- }
|