/***********************************************************/
#include<reg51.h> //头文件
#include<LCD1602.h>
#include<ADC0832.h>
#include<EEPROM.h>
#include<INTERRUPT.h>
#define uchar unsigned char
#define uint unsigned int
/*****************灯、蜂鸣器、按键引脚定义******************/
sbit led=P1^7; //LED灯(红)
sbit buzz=P2^3; //蜂鸣器
sbit key_set=P1^0; //设置键
sbit key_jia=P1^3; //加键
sbit key_jian=P1^6; //减键
/*****************全局变量定义******************************/
#define K_MG_MV 2
#define IN_V 500
long ALValue;
uchar K_ZERO;
uint jiujia_up,zuijia_up;
uint set_f;
uchar num;
/***********************************************************
函数名称:void fixed_display()
函数作用:固定显示部分
参数说明:
************************************************************/
void fixed_display()
{
lcd1602_write_character(0,1,"ALcohol content:");
lcd1602_write_character(8,2,"PPM");
}
/***********************************************************
函数名称:void display1()
函数作用:显示实际浓度值
参数说明:
************************************************************/
void display1()
{
fixed_display();
if(K_ZERO+2>ADC_date)
K_ZERO=ADC_date;
ALValue=(long)IN_V*(ADC_date-K_ZERO)/255;
if(ALValue<0)
ALValue=0;
ALValue=ALValue*K_MG_MV;
LCD_disp_char(5,2,ASCII[ALValue/100]);
LCD_disp_char(6,2,ASCII[ALValue%100/10]);
LCD_disp_char(7,2,ASCII[ALValue%10]);
if(zuijia_up<=AVlalue)
{
led=0;
buzz=0;
}
else
if(jiujia_up<=ALValue)
{
led=0;
buzz=1;
}
else
{
led=1;
buzz=1;
}
}
/***********************************************************
函数名称:void display2(void)
函数作用:显示设置时酒驾、醉驾上限值
参数说明:
************************************************************/
void display2(void)
{
num++;
if(num==254)
num=0;
lcd1602_write_character(0,1,"Jiujia Limit:");
lcd1602_write_character(0,2,"Ziujia Limit:");
if(num%2==0)
{
LCD_disp_char(13,1,ASCII[jiujia_up/100]);
LCD_disp_char(14,1,ASCII[jiujia_up%100/10]);
LCD_disp_char(15,1,ASCII[jiujia_up%10]);
LCD_disp_char(13,1,ASCII[ziujia_up/100]);
LCD_disp_char(13,1,ASCII[ziujia_up%100/10]);
LCD_disp_char(13,1,ASCII[ziujia_up%10]);
}
else
{
if(set_f==1)
{
LCD_disp_char(13,1,ASCII[13]);
LCD_disp_char(14,1,ASCII[13]);
LCD_disp_char(15,1,ASCII[13]);
}
else
{
LCD_disp_char(13,1,ASCII[jiujia_up/100]);
LCD_disp_char(14,1,ASCII[jiujia_up%100/10]);
LCD_disp_char(15,1,ASCII[jiujia_up%10]);
}
if(set_f==2)
{
LCD_disp_char(13,2,ASCII[13]);
LCD_disp_char(14,2,ASCII[13]);
LCD_disp_char(15,2,ASCII[13]);
}
else
{
LCD_disp_char(13,1,ASCII[ziujia_up/100]);
LCD_disp_char(13,1,ASCII[ziujia_up%100/10]);
LCD_disp_char(13,1,ASCII[ziujia_up%10]);
}
}
}
/***********************************************************
函数名称:void delayms(uint ms)
函数作用:毫秒延时函数
参数说明:
************************************************************/
void delayms(uint ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
/***********************************************************
函数名称:void scan(void)
函数作用:按键检测及处理
参数说明:
************************************************************/
void scan(void)
{
//设置键,不支持连按
if(key_set==00)
{
delayms(7);
if(key_set==0)
{
led=1;
buzz=1;
if(set_f==0)
{
LCD_write_command(0x01);
delay_n40us(100);
display2();
}
set_f++;
if(set_f==3)
{
set_f=0;
EEPROM_delete(0x2002);
EEPROM_write(0x2002,jiujia_up/100);
EEPROM_write(0x200a,jiujia_up%100);
EEPROM_delete(0x2202);
EEPROM_write(0x2202,zuijia_up/100);
EEPROM_write(0x220a,zuijia_up%100);
LCD_write_command(0x01);
delay_n40us(100);
fixed_display();
}
}
while(!key_set);
}
//加值键,支持连续
if(key_jia==0&&set_f!=0)
{
delayms(7);
if(key_jia==0set_f==1)
{
if(jiujia_up<999&&jiujia_up<zuijia_up-1)
jiujia_up++;
display2();
}
if(key_jia==0&&set_f==2)
{
if(zuijia_up<999)
zuijia_up++;
display2();
}
}
//减值键,支持连续
if(key_jian==0&&set_f!=0)
{
delayms(7);
if(key_jian==0set_f==1)
{
if(jiujia_up!=0)
jiujia_up--;
display2();
}
if(key_jia==0&&set_f==2)
{
if(zuijia_up!=0&&zuijia_up>jiujia_up+1)
zuijia_up--;
display2();
}
}
}
/***********************************************************
函数名称:void main()
函数作用:主函数
参数说明:
************************************************************/
void main()
{
uchar i=0;
T0_init();
LCD_init();
/*EEPROM_delete(0x2002);
EEPROM_write(0x2002,80/100);
EEPROM_write(0x200a,80%100);
jiujia_up=EEPROM_read(0x2002);
jiujia_up=jiujia_up*100+EEPROM_read(0x200a);
/*EEPROM_delete(0x2202);
EEPROM_write(0x2202,150/100);
EEPROM_write(0x220a,150%100);
ziujia_up=EEPROM_read(0x2202);
ziujia_up=ziujia_up*100+EEPROM_read(0x220a);
while(!wait_f)
{
lcd1602_write_character(0,1,"Please wait for!");
LCD_disp_char(6,2,ASCII[(60-time)/10]);
LCD_disp_char(7,2,ASCII[(60-time)%10]);
LCD_disp_char(9,2,s);
}//*/
ADC0832_read(0);
K_ZERO=ADC_date;
while(1)
{
/*ADC0832_read(0);
LCD_disp_char(0,2,ASCII[K_ZERO/100]);
LCD_disp_char(1,2,ASCII[K_ZERO%100/10]);
LCD_disp_char(2,2,ASCII[K_ZERO%10]);//*/
scan();
if(set_f==0)
{
i++;
if(i>50)
{
ADC0832_read(0);
i=0;
}
display1();
}
else
display2();
}
}
|