|
#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BUZZER=P2^3; //蜂鸣器io口
sbit LR=P2^4; //剩余油量过低LED红灯警告 端口
sbit LG=P2^5; //剩余油量过高LED绿灯警告 端口
sbit EOC=P3^0;
char *t[]={" ","#","##","###","####","#####","######","#######","########","#########","##########"};
unsigned char code Str1[]=" Welcome ! ";
unsigned char code Str2[]=" Please wait ";
void delay_10us(uint n); //10微秒的延时子程序
void delay_50us(uint n); //50微秒的延时子程序
void write_com(uchar com); //写指令函数
void write_data(uchar dat);//输入数据函数
void set_xy(uchar x,uchar y); //设置显示位置
void display(uchar x,uchar y,uchar *s); // 显示一个字符串函数
void ADC0808(); //ADC0808转换函数
void TMOD_Init(); //定时器初始化函数
void guodi(); //产生蜂鸣器所需震荡LED红
void guogao(); //产生蜂鸣器所需震荡LED绿
void contrast(x); //比较函数
void main()
{
TMOD_Init(); //初始化定时器
display(0x00, Str1);
display(0x40, Str2);
BUZZER=1;
LR=1;
LG=1;
while(1)
{
uchar n,x,s[4];
int i;
ADC0808(); //不断地进行数模转化
contrast(x);
switch(i)
{
case 0: display(0,1,*t),guodi();
case 1: display(0,1,*t);
case 2: display(0,1,*t);
case 3: display(0,1,*t);
case 4: display(0,1,*t);
case 5: display(0,1,*t);
case 6: display(0,1,*t);
case 7: display(0,1,*t);
case 8: display(0,1,*t);
case 9: display(0,1,*t),guogao();
}
for(n=0;n<16;n++)
{
write_data(s);
}
}
}
void delay_10us(uint n) //10微秒的延时子程序
{
uint i,j;
for(i=n;i>0;i--)
for(j=2;j>0;j--);
}
void delay_50us(uint n) //50微秒的延时子程序
{
uint i,j;
for(i=n;i>0;i--)
for(j=22;j>0;j--);
}
void TMOD_Init()
{
TMOD=0X01;
TH0=0xfc;
TL0=0x18;
EA=1;
ET0=1;
TR0=0;
}
void write_com(uchar com)//写指令
{
E=0;
RS=0;
RW=0;
P0=com;
delay_50us(10);
E=1;
delay_50us(20);
E=0;
}
void write_data(uchar dat)//输入数据
{
E=0;
RS=1;
RW=0;
P0=dat; //送数据
delay_50us(10);
E=1;
delay_50us(20);
E=0;
}
void init(void)
{
delay_50us(300); //延时15ms,换算15ms等于300us
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void LCD_init(void) //初始化LCD1602
{
delay_10us(20);
write_com(0x38); //设置8位格式,2行,5x7
delay_10us(5);
write_com(0x0c); //整体显示,关光标,不闪烁
delay_10us(5);
write_com(0x06); //设定输入方式,增量不移位
delay_10us(5);
write_com(0x01); //清除屏幕显示
delay_50us(40);
}
void LCD_set_xy(uchar x,uchar y) //设置显示位置
{
uchar address;
if(y==1)
{
address=0x80+x; //第一行位置
} else {
address=0xc0+x; //第二行位置
}
delay_10us(5);
write_com(address);
}
void display(uchar x,uchar y,uchar *s) //显示一个字符串函数
{
set_xy(x,y);
delay_10us(5);
while(*s!='\0')
{
write_data(*s);
s++;
}
}
void ADC0808() //adc0808始
{
uchar temp;
uint voldata;
uchar dispbuf[4];
while(1)
{
do
{;}
while(~EOC); //转换是否完成
temp = ADC0808; //读出转换结果
voldata = temp*1.0/255*1000;
dispbuf[3] = voldata%10;
dispbuf[2] = voldata/10%10;
dispbuf[1] = voldata/100%10;
dispbuf[0] = voldata/1000;
}
} //ADC止
void guodi() //产生蜂鸣器所需震荡LED红
{
BUZZER=~BUZZER;
LR=~LR;
}
void guogao() //产生蜂鸣器所需震荡LED绿
{
BUZZER=~BUZZER;
LG=~LG;
}
void contrast(x) //比较函数
{
uint i;
if(0<x<100)i=0;
else if(100<x<200)i=1;
else if(200<x,300)i=2;
else if(300<x<400)i=3;
else if(400<x<500)i=4;
else if(500<x<600)i=5;
else if(600<x<700)i=6;
else if(700<x<800)i=7;
else if(800<x<900)i=8;
else if(900<x<1000)i=9;
}
|
|