#include "stc89x_90x.h"
#include "LCD1602.h"
#include "ADC0832.h"
#include "intrins.h"
sbit k1=P1^4;
sbit k2=P1^5;
sbit k3=P1^6;
sbit beep=P3^5;
sbit beep2=P3^2;
sbit beep3=P3^3;
sbit beep4=P3^4;
sbit led=P3^6;
unsigned char temp1[]={"Potency: **%"}; //显示酒精浓度
unsigned char temp2[]={"early:**% "}; //显示浓度预警值
unsigned char temp3[]={"Loading... "};
unsigned char key,a;
unsigned char dat=50; //酒精浓度预警初始位
long int m;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
/********
void concentration()
酒精浓度计算
*********/
void concentration()
{
unsigned char count; //定义8位的cout
long int a; //定义长整型变量
count=ADC_acquisition(); //将ADC0832读出的值赋给count
a=count*50;
count=(float)(a/256);
if(count<13)
count=(float)(13); //当电压值小于1.4V时,将电压值固定在1.4V
else
count=(float)(count-13)*3.5; //计算酒精浓度为一个浮点型变量
/* 这一句没有用上一句count为u8类型已经完成了类型转换
count=(char)count;
*/
if(count>=100) //当浓度大于100%时,将浓度固定在100%
{
count=100;
temp1[8]=count/100+'0';
}
else temp1[8]=' ';
temp1[9]=count%100/10+'0'; //将浓度值赋给数组
temp1[10]=count%100%10+'0';
m=count;
}
/************
void loading()
初始加载(预热)函数
*************/
void loading()
{
unsigned char s=10;
while(s--)
{
WriteInstruction(0x80);
a=0;
while(temp3[a]!='\0')
{
WriteData(temp3[a]);
a++;
}
}
Delay1000ms();Delay1000ms();
Delay1000ms();Delay1000ms();
while(1)
{
concentration();
if(m==0)
{
temp3[7]=' ';
temp3[8]='O';
temp3[9]='K';
}
WriteInstruction(0x80);
a=0;
while(temp3[a]!='\0')
{
WriteData(temp3[a]);
a++;
}
if(m==0)
{
Delay1000ms();
break;
}
}
}
/********
void independent()
预警选择位,k2为增加,k3为减少
*********/
void independent()
{
if(k2==0)
{
delay(1000);
if(k2==0)
{
while(k2==0);
dat++;
if(dat==100)dat=99;
}
}
if(k3==0)
{
delay(1000);
if(k3==0)
{
while(k3==0);
dat--;
if(dat==0)dat=1;
}
}
temp2[6]=dat/10+'0';
temp2[7]=dat%10+'0';
}
/**************
void first_line()
显示第一行,即为酒精浓度
***************/
void first_line()
{
WriteInstruction(0x80);
a=0;
while(temp1[a]!='\0')
{
WriteData(temp1[a]);
a++;
}
delay(1000);
}
/*************
void second_line()
显示第二行,即为酒精浓度预警值
**************/
void second_line()
{
independent();
temp2[6]=dat/10+'0';
temp2[7]=dat%10+'0';
WriteInstruction(0x80+0x40);
a=0;
while(temp2[a]!='\0')
{
WriteData(temp2[a]);
a++;
}
delay(1000);
}
/*******
void main()
主函数
********/
void main()
{
unsigned char n=0;
key=0;
Init();
loading();
Init();
while(1)
{
concentration(); //酒精浓度检测位
first_line(); //显示第一行酒精浓度
if(k1==0)
{
delay(1000);
if(k1==0)
{
while(k1==0);
if(key==0)key=1;
else key=0;
}
}
if(key==1)
{
independent();
second_line();
n=1;
}
if(key==0)
{
if(n==1)
{
WriteInstruction(0x01);
}
n=0;
}
if(dat<=m)
{
led=0;
beep=0;
}
else
{
led=1;
beep=1;
beep2=1;
beep3=1;
beep4=1;
}
}
}
- 1
- 2
- 3
- 4
前往页