一、题目
二、程序代码
1. AD采集及滤波
void Set_Collect_Proc(void)
{uint8_t a;float ADC;if((uwTick-uwTick_Collect)<1000) return;uwTick_Collect=uwTick;for(a=0;a<=9;a++){ADC+=Get_R37();}ADC=ADC*3.3f/4096;R37_ADC=ADC/10;H=R37_ADC*K;if(H<=E2PROM_Num[0]){Level=0;}else if( (H<=E2PROM_Num[1])&&(H>E2PROM_Num[0]) ){Level=1;}else if( (H<=E2PROM_Num[2])&&(H>E2PROM_Num[1]) ){Level=2;}else if(H>E2PROM_Num[2]){Level=3;}if(Level_old!=Level){if(Level>Level_old){sprintf((char *)str,"A:H%02d+L%d+U\r\n",H,Level);}else{sprintf((char *)str,"A:H%02d+L%d+D\r\n",H,Level);}HAL_UART_Transmit(&huart1,str, strlen((const char *)str), 50);led2_sign=1;}Level_old=Level;
}
2. LCD显示
void Lcd_Proc(void)
{if((uwTick - uwTick_Lcd_Set_Point)<100) return;//减速函数uwTick_Lcd_Set_Point = uwTick;if(LCD_Show_Sign==1){LCD_SetTextColor(Black);sprintf((char *)Lcd_Str," Liquid Level ");LCD_DisplayStringLine(Line0,Lcd_Str);sprintf((char *)Lcd_Str," Height:%d ",H);LCD_DisplayStringLine(Line1,Lcd_Str);sprintf((char *)Lcd_Str," ADC:%3.2f ",R37_ADC);LCD_DisplayStringLine(Line2,Lcd_Str);sprintf((char *)Lcd_Str," Level:%d ",Level);LCD_DisplayStringLine(Line3,Lcd_Str);}else{ LCD_SetTextColor(Black);sprintf((char *)Lcd_Str," Parameter Setup ");LCD_DisplayStringLine(Line0,Lcd_Str);if(E2PROM_Select==1){LCD_SetTextColor(Green);}else{LCD_SetTextColor(Black);}sprintf((char *)Lcd_Str," Threshold1: %2d ",E2PROM_Num[0]);LCD_DisplayStringLine(Line1,Lcd_Str);if(E2PROM_Select==2){LCD_SetTextColor(Green);}else{LCD_SetTextColor(Black);}sprintf((char *)Lcd_Str," Threshold2: %2d ",E2PROM_Num[1]);LCD_DisplayStringLine(Line2,Lcd_Str);if(E2PROM_Select==3){LCD_SetTextColor(Green);}else{LCD_SetTextColor(Black);}sprintf((char *)Lcd_Str," Threshold3: %2d ",E2PROM_Num[2]);LCD_DisplayStringLine(Line3,Lcd_Str);}
}
3. 按键功能
功能:
按键①:页面切换、储存阈值至E2PROM。
按键②:阈值设定选择。
按键③:阈值增加5cm,增加到95cm为止。
按键④:阈值减少5cm,减少到5cm为止。
uint8_t Key_Scan(void)
{uint8_t key=0;if( HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET ){key=1;}else if( HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET ){key=2;}else if( HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET ){key=3;}else if( HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET ){key=4;}return key;
}
void Key_Proc(void)
{if( (uwTick-uwTick_key)<50 ) return;uwTick_key=uwTick;Key_val=Key_Scan();Key_down=Key_val&(Key_old^Key_val);Key_up=~Key_val&(Key_old^Key_val);Key_old=Key_val;key_now=Key_down;if(Key_down==1){if(LCD_Show_Sign==1){LCD_Show_Sign=2;}else if(LCD_Show_Sign==2){E2PROM_T[0]=E2PROM_Num[0];E2PROM_T[1]=E2PROM_Num[1];E2PROM_T[2]=E2PROM_Num[2];IIC_24C02_Write(E2PROM_T,0,3);LCD_Show_Sign=1;Key_down=0;}}else if(Key_down==2){if(E2PROM_Select==1){E2PROM_Select=2;}else if(E2PROM_Select==2){E2PROM_Select=3;}else if(E2PROM_Select==3){E2PROM_Select=1;}}else if(Key_down==3){if(E2PROM_Select==1){E2PROM_Num[0]+=5;if(E2PROM_Num[0]>=100){E2PROM_Num[0]=95;}if(E2PROM_Num[0]>=E2PROM_Num[1]){E2PROM_Num[0]=E2PROM_Num[1];}}if(E2PROM_Select==2){E2PROM_Num[1]+=5;if(E2PROM_Num[1]>=100){E2PROM_Num[1]=95;}if(E2PROM_Num[1]>=E2PROM_Num[2]){E2PROM_Num[1]=E2PROM_Num[2];}}if(E2PROM_Select==3){E2PROM_Num[2]+=5;if(E2PROM_Num[2]>=100){E2PROM_Num[2]=95;}}}else if(Key_down==4){if(E2PROM_Select==1){E2PROM_Num[0]-=5;if(E2PROM_Num[0]<=5){E2PROM_Num[0]=5;}}if(E2PROM_Select==2){E2PROM_Num[1]-=5;if(E2PROM_Num[1]<=5){E2PROM_Num[1]=5;}if(E2PROM_Num[1]<=E2PROM_Num[0]){E2PROM_Num[1]=E2PROM_Num[0];}}if(E2PROM_Select==3){E2PROM_Num[2]-=5;if(E2PROM_Num[2]<=5){E2PROM_Num[2]=5;}if(E2PROM_Num[2]<=E2PROM_Num[1]){E2PROM_Num[2]=E2PROM_Num[1];}}}}
4. 串口查询与输出功能
1.查询功能
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(rx_buffer[0] == 'C'){T_sign=1;counter=0;led_Usart_sign=1; }else if(rx_buffer[0] == 'S'){ T_sign=2;counter=0; led_Usart_sign=1;}rx_buffer[0]=0;HAL_UART_Receive_IT(&huart1, (uint8_t *)(&rx_buffer), 1);}
void Usart1_Proc(void)
{if(T_sign==1){sprintf((char *)str, "C:H%02d+L%02d\r\n",(uint8_t)H,(uint8_t)Level);HAL_UART_Transmit(&huart1,str, strlen((const char *)str), 50);T_sign=0;}else if(T_sign==2){sprintf((char *)str, "S:TL%2d+TM%2d+TH%2d\r\n",E2PROM_Num[0],E2PROM_Num[1],E2PROM_Num[2]);HAL_UART_Transmit(&huart1,str, strlen((const char *)str), 50);T_sign=0;}
}
输出功能:写入到了“AD采集及滤波”代码中。
if(Level_old!=Level){if(Level>Level_old){sprintf((char *)str,"A:H%02d+L%d+U\r\n",H,Level);}else{sprintf((char *)str,"A:H%02d+L%d+D\r\n",H,Level);}HAL_UART_Transmit(&huart1,str, strlen((const char *)str), 50);led2_sign=1;}Level_old=Level;
5. 状态指示
void LED_Show(uint8_t led)
{HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC,led<<8, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
void LED_Proc(void)
{if((uwTick-uwTick_led)<200) return;uwTick_led=uwTick;led1_num++;if(led1_num==5){led1_num=0;LED^=0x01;}if(led2_sign==1){LED^=0x02;led2_times++;if(led2_times>=10){led2_sign=0;led2_times=0;LED&=0xfd;}}if(led_Usart_sign==1){LED^=0x04;led4_times++;if(led4_times>=10){led_Usart_sign=0;led4_times=0;LED&=0xfb;}}}
重点:关注初始化时E2PROM的保存方式!!