个人做题记录,仅供参考
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
#include "lcd.h"
#include "key.h"
#include "at24c02.h"
#include "adc.h"
#include "usart.h"
#include "led.h"
u32 TimingDelay = 0;
uint8_t Lcd_flag = 0;
uint8_t lcd_light = 0;
uint8_t IIC_flag = 0xdd;
uint8_t IIC_Flag = 0;
uint8_t hight = 0;
uint8_t Level = 0;
int Adc_Val = 0;
uint8_t Time_1s = 0;
uint8_t Time_200ms = 0;
uint8_t Uart_Buf = 0;
uint8_t RxOver = 0;
uint8_t Send_flag_U = 0;
uint8_t Send_flag_D = 0;
uint8_t Led_flag_2 = 0;
uint8_t Led_flag_3 = 0;
uint8_t Threshold_Buf[3] = {0};
uint8_t IIC_Buf[3] = {30,50,70};
uint8_t Buf[20] = {0};
void Delay_Ms(u32 nTime);
void Key_Proc(uint8_t* p);
void Lcd_Proc(void);
void Level_Deal(uint8_t* p,uint8_t hight,uint8_t* Level);
void Uart2_Proc(uint8_t Uart_Buf);
void Led_Proc(void);
int main(){
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
KEY_Init();
i2c_init();
ADC1_Init();
USART2_Init(9600);
LED_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
i2c_read(&IIC_Flag,0x10,1);
if(IIC_Flag == 0xdd){
i2c_read(Threshold_Buf, 0x00,3);
}
else{
i2c_write(&IIC_flag, 0x10,1);
Delay_Ms(200);
i2c_write(IIC_Buf, 0x00,3);
Delay_Ms(200);
i2c_read(Threshold_Buf, 0x00,3);
}
while(1){
if(Time_1s){
Time_1s = 0;
Adc_Val = (((float)ADC1_Conv() * 3.3 / 4096)+0.001)*100;
hight = (float)Adc_Val / 330 * 100;
}
Key_Proc(Threshold_Buf);
Lcd_Proc();
Level_Deal(Threshold_Buf,hight,&Level);
Uart2_Proc(Uart_Buf);
Led_Proc();
}
}
void Key_Proc(uint8_t* p){
static uint8_t ucKey_Val = 0;
static uint8_t ucKey_Down = 0;
static uint8_t ucKey_Old = 0;
static uint8_t i = 0;
ucKey_Val = KEY_Scan();
ucKey_Down = ucKey_Val & (ucKey_Val ^ ucKey_Old);
ucKey_Old = ucKey_Val;
switch(ucKey_Down){
case 1:{
Delay_Ms(100);
Lcd_flag = !Lcd_flag;
i2c_write(p, 0x00,3);
i = 0;
}break;
case 2:{
if(Lcd_flag == 1){
if(++i == 3)i = 0;
}
}break;
case 3:{
if(Lcd_flag == 1){
p[i] += 5;
if(p[i] > 95)p[i] = 95;
}
}break;
case 4:{
if(Lcd_flag == 1){
p[i] -= 5;
if(p[i] < 5)p[i] = 5;
}
}break;
}
lcd_light = i;
}
void Lcd_Proc(void){
if(Lcd_flag == 0){
LCD_DisplayStringLine(Line1," Liquid Level ");
sprintf((char*)Buf," Height: %dcm ",hight);
LCD_DisplayStringLine(Line3,Buf);
memset(Buf,0,20*sizeof(uint8_t));
sprintf((char*)Buf," ADC: %.2fV ",(float)Adc_Val/100);
LCD_DisplayStringLine(Line5,Buf);
memset(Buf,0,20*sizeof(uint8_t));
sprintf((char*)Buf," Level: %d ",Level);
LCD_DisplayStringLine(Line7,Buf);
memset(Buf,0,20*sizeof(uint8_t));
}
else{
LCD_DisplayStringLine(Line1," Parameter Setup ");
if(lcd_light == 0)LCD_SetBackColor(Green);
sprintf((char*)Buf," Threshold 1: %02ucm ",Threshold_Buf[0]);
LCD_DisplayStringLine(Line3,Buf);
memset(Buf,0,20*sizeof(uint8_t));
LCD_SetBackColor(Blue);
if(lcd_light == 1)LCD_SetBackColor(Green);
sprintf((char*)Buf," Threshold 2: %02ucm ",Threshold_Buf[1]);
LCD_DisplayStringLine(Line5,Buf);
memset(Buf,0,20*sizeof(uint8_t));
LCD_SetBackColor(Blue);
if(lcd_light == 2)LCD_SetBackColor(Green);
sprintf((char*)Buf," Threshold 3: %02ucm ",Threshold_Buf[2]);
LCD_DisplayStringLine(Line7,Buf);
memset(Buf,0,20*sizeof(uint8_t));
LCD_SetBackColor(Blue);
}
}
void Level_Deal(uint8_t* p,uint8_t hight,uint8_t* Level){
static uint8_t Level_Val = 0;
static uint8_t Level_Old = 0;
Level_Old = *Level;
if(hight < p[0] || hight == p[0])*Level = 0;
else if(hight > p[0] && hight < p[1] || hight == p[1])*Level = 1;
else if(hight > p[1] && hight < p[2] || hight == p[2])*Level = 2;
else if(hight > p[2])*Level = 3;
Level_Val = *Level;
if(Level_Val != Level_Old){
if(Level_Val > Level_Old){
Send_flag_U = 1;
}else if(Level_Val < Level_Old){
Send_flag_D = 1;
}
Led_flag_2 = 1;
}
}
void Uart2_Proc(uint8_t Uart_Buf){
if(RxOver == 1){
if(Uart_Buf == 'C'){
sprintf((char*)Buf,"C:H%02d+L%d\r\n",hight,Level);
Led_flag_3 = 1;
}
else if(Uart_Buf == 'S'){
sprintf((char*)Buf,"S:TL%02d+TM%02d+TH%02d\r\n",Threshold_Buf[0],Threshold_Buf[1],Threshold_Buf[2]);
Led_flag_3 = 1;
}
}
if(Send_flag_U == 1){
sprintf((char*)Buf,"A:H%02d+L%d+U\r\n",hight,Level);
}
else if(Send_flag_D == 1){
sprintf((char*)Buf,"A:H%02d+L%d+D\r\n",hight,Level);
}
USART_SendString(USART2,Buf);
memset(Buf,0,20*sizeof(uint8_t));
Send_flag_U = Send_flag_D = RxOver = 0;
}
void USART2_IRQHandler(void){
if(USART_GetITStatus(USART2,USART_IT_RXNE)){
RxOver = 1;
Uart_Buf = USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
}
void Led_Proc(void){
static uint8_t ucLed = 0;
static uint8_t Num = 10;
if(Time_1s == 1){
ucLed ^= 0x01;
}
if(Led_flag_2 == 1){
if(Time_200ms == 1){
if(Num --){
ucLed ^= 0x02;
}
else{
Led_flag_2 = 0;
Num = 10;
}
Time_200ms = 0;
}
}
if(Led_flag_3 == 1){
if(Time_200ms == 1){
if(Num --){
ucLed ^= 0x04;
}
else{
Led_flag_3 = 0;
Num = 10;
}
Time_200ms = 0;
}
}
LED_Disp(ucLed);
}
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
问题: 1、sprintf里写字符变量用%u; 2、LED灯逻辑没有很好,要多记几个算法; 3、E2PROM的掉电存储,注意里面存储的是8位数据,最好用16进制数做判断。(我之前疯狂用int型判断orz,最后还是参考小默的博客才写出来的。。。当然,也有我太懒了,不想去改官方IIC程序的因素。反正,越简单越好) 4、注意按键消抖,官方的程序里可能时间不太够。(不过就B1会抖动,还挺怪的) 参考博客: @小默 haa—— 蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛 ——“电压、频率采集设备”
|