#include "main.h"
#include "bsp\bsp_led_key.h"
#include "bsp\bsp_rcc.h"
#include "bsp\bsp_lcd.h"
#include "stdio.h"
#include "bsp\bsp_adc.h"
#include "bsp\bsp_i2c.h"
//延迟
__IO uint32_t uwtick_key_point = 0;
__IO uint32_t uwtick_led_point = 0;
__IO uint32_t uwtick_lcd_point = 0;
//led
uint8_t ucled = 0;
//key
uint8_t key_value = 0;
uint8_t key_down = 0;
uint8_t key_old = 0;
uint8_t key_up = 0;
//lcd
uint8_t lcd_disp_string[20];
//user
uint8_t disp_state = 0x00;
unsigned char threshold_ctrl[3] = {30,50,70};
unsigned char threshold_set[3] = {30,50,70};
uint8_t usercmdate = 0;
float uservdata = 0;
uint8_t nmsl = 0;
void SystemClock_Config(void);
void Key_Proc(void);
void Led_Proc(void);
void Lcd_Proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_KEY_Init();
LCD_Init();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
ADC2_Init();
I2CInit();
eeprom_read(threshold_ctrl,0,3);
threshold_set[0] = threshold_ctrl[0];
threshold_set[1] = threshold_ctrl[1];
threshold_set[2] = threshold_ctrl[2];
while (1)
{
Key_Proc();
Led_Proc();
Lcd_Proc();
}
}
void Lcd_Proc(void)
{
if((uwTick - uwtick_lcd_point)< 100)
return;
uwtick_lcd_point = uwTick;
if(disp_state == 0x00)
{sprintf((char*)lcd_disp_string," Liquid Level");
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line2,lcd_disp_string);
uservdata = (getADC2()/4096.0*3.3);
usercmdate =(uint8_t)(uservdata*(100.0/3.3));
sprintf((char*)lcd_disp_string," Height:%2d",usercmdate);
LCD_DisplayStringLine(Line3,lcd_disp_string);
sprintf((char*)lcd_disp_string," ADC:%4.2f",uservdata);
LCD_DisplayStringLine(Line4,lcd_disp_string);
if(usercmdate<=threshold_set[0])
sprintf((char*)lcd_disp_string," Level:0");
else if((usercmdate>threshold_ctrl[0])&&(usercmdate<=threshold_ctrl[1]))
sprintf((char*)lcd_disp_string," Level:1");
else if((usercmdate>threshold_ctrl[1])&&(usercmdate<=threshold_ctrl[2]))
sprintf((char*)lcd_disp_string," Level:2");
else if(usercmdate>threshold_ctrl[2])
sprintf((char*)lcd_disp_string," Level:3");
LCD_DisplayStringLine(Line5,lcd_disp_string);}
else if((disp_state>>4) == 0x01)
{
sprintf((char*)lcd_disp_string," Parameter Setup");
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line2,lcd_disp_string);
sprintf((char*)lcd_disp_string," Threshold1:%2d",(unsigned int)threshold_set[0]);
if(disp_state == 0x10)
LCD_SetTextColor(Green);
else
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line4,lcd_disp_string);
sprintf((char*)lcd_disp_string," Threshold2:%2d",(unsigned int)threshold_set[1]);
if(disp_state == 0x11)
LCD_SetTextColor(Green);
else
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line5,lcd_disp_string);
sprintf((char*)lcd_disp_string," Threshold3:%2d",(unsigned int)threshold_set[2]);
if(disp_state == 0x12)
LCD_SetTextColor(Green);
else
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line6,lcd_disp_string);
}
}
void Key_Proc(void)
{
if((uwTick - uwtick_key_point)< 100)
return;
uwtick_key_point = uwTick;
key_value = key_scan();
key_down = key_value&(key_value^key_old);
key_up = ~key_value&(key_value^key_old);
key_old = key_value;
if(key_down == 1)
{if(disp_state == 0x00)
{LCD_Clear(White);
disp_state = 0x10;}
else if((disp_state >> 4) == 0x01)
{
LCD_Clear(White);
threshold_ctrl[0] = threshold_set[0];
threshold_ctrl[1] = threshold_set[1];
threshold_ctrl[2] = threshold_set[2];
eeprom_write(threshold_ctrl,0,3);
disp_state = 0x00;}
}
if(key_down == 2)
{
if((disp_state >> 4) == 0x01)
{
if(++disp_state == 0x13)
{
disp_state = 0x10;
}
if(disp_state == 0x12)
{
nmsl = 1;
}
else if((disp_state == 0x10)||(disp_state == 0x11))
nmsl = 0;
}}
if(key_down == 3)
{
switch(disp_state)
{
case 0x10:
{if(threshold_set[0] < 95)
threshold_set[0] += 5;
break; }
case 0x11:
{if(threshold_set[1] < 95)
threshold_set[1] += 5;
break;}
case 0x12:
{if(threshold_set[2] < 95)
threshold_set[2] += 5;
break;}
default:
break;
}
}
if(key_down == 4)
{
switch(disp_state)
{
case 0x10:
{if(threshold_set[0] > 5)
threshold_set[0] -= 5;
break;}
case 0x11:
{if(threshold_set[1] > 5)
threshold_set[1] -= 5;
break;}
// case 0x12:
// {threshold_set[2]-= 5;
// if(threshold_set[2] == 0)
// threshold_set[2] = 5;
// break;}莫名其妙的一加上括号里的东西就会切换不了界面只能再加一个标志位了
default:
break;
}
if(nmsl == 1)
{threshold_set[2]-= 5;
if(threshold_set[2] == 0)
threshold_set[2] = 5;}
}
}
蓝桥杯2016年省赛的题目,还没写完,遇到了一个奇怪的问题,只要把取消注释就会导致界面切换不了,一直在第一个界面正常显示,我试了好久,只要有0x12和这段代码就会出这个问题,给我搞得很恼火。找不到和界面切换的标志位有什么联系,我想知道为什么会这样。