#include<reg51.h>//头文件
#include<intrins.h>
#define DD P2
sbit CS=P1^0; //定义端口
sbit CLK=P1^1;
sbit Do=P1^2;
sbit Di=P1^3;
sbit KEY=P1^7;
sbit buzzer=P0^0;
sbit Rs=P1^4;//LCD定义端口
sbit Rw=P1^5; sbit E=P1^6;
sbit busy_p=ACC^7;
unsigned char CH=0x02,key_count=1;
unsigned char HC[6]={0};
unsigned char BZ=0;
void delay_1ms(unsigned char i) //延时函数1ms
{
unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void delay_10ns(unsigned char i)//延时函数10ns
{
unsigned char j;
while(i--)
for(j=0;j<10; j++);
}
void xiezhiling(unsigned char com,bit p)//写命令函数
{
if(p)
delay_10ns(5);
E=0;
Rs=0;
Rw=0;
DD=com;
delay_10ns(50);
E=1;
delay_1ms(2);
E=0;
delay_10ns(4);
}
void xieshuju(unsigned char DATA)//写数据函数
{
delay_10ns(50);
E=0;
Rs=1;
Rw=0;
DD=DATA;
delay_10ns(50);
E=1;
delay_10ns(50);
E=0;
delay_10ns(4);
}
void init(void)//初始化函数
{
delay_1ms(15);
xiezhiling(0x38,0);
delay_1ms(5);
xiezhiling(0x38,0);
delay_1ms(5);
xiezhiling(0x38,0);
delay_1ms(5);
xiezhiling(0x38,1); //功能设定(8位,2行,5*7点矩阵)
xiezhiling(0x08,1);
xiezhiling(0x01,1); //清除屏幕
xiezhiling(0x06,1); //加1
xiezhiling(0x0c,1);//显示器ON,光标OFF,闪烁OFF
}
unsigned char ADconv(void)//AD转换子程序
{
unsigned char i;
unsigned int data_f=0,data_c=0;
Di=1;
CS=1;
_nop_();
CS=0;
Di=1;
CLK=1;
_nop_();
_nop_();
CLK=0;
_nop_();
_nop_();
CLK=1;
Di=(bit)(0x02&CH);
_nop_();
CLK=0;
_nop_();
_nop_();
CLK=1;
Di=(bit)(0x01&CH);
_nop_();
_nop_();
CLK=0;
Di=1;
CLK=1;
_nop_();
_nop_();
CLK=0;
_nop_();
CLK=1;
for(i=8;i>0;i--)
{
data_f|=Do;
data_f<<=1;
CLK=1;
_nop_();
_nop_();
CLK=0;
_nop_();
}
for(i=8;i>0;i--)
{
data_c<<=1;
data_c|=Do;
_nop_();
CLK=1;
_nop_();
_nop_();
CLK=0;
_nop_();
}
CLK=0;
_nop_();
_nop_();
CLK=1;
_nop_();
_nop_();
CLK=0;
_nop_();
_nop_();
CLK=1;
_nop_();
CS=1;
_nop_();
_nop_();
return data_f;
}
void xs_int(unsigned int shuju)//将一个数据各个位分开并存放在H C[]数组中
{
if(shuju < 10)
BZ = 1;
else if(shuju < 100) BZ = 2;
else if(shuju < 1000) BZ = 3;
else if(shuju < 10000) BZ = 4;
else if(shuju < 65535) BZ = 5;
switch(BZ)
{
case 5:HC[5] = shuju/10000; //百位
case 4:HC[3] = shuju%10000/1000;//十位
case 3:HC[2] = shuju%1000/100;//个位
case 2:HC[1] = shuju%100/10;//小数点后一位
case 1:HC[0] = shuju%10;
break;
default:break;
}
}
void main(void)//主函数
{
unsigned int data_temp=0;
init();
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
while(1)
{
data_temp=ADconv();
if(data_temp>128)//报警数值
{
EA=1;
}
else
{
EA=0;
buzzer=0;
}
P3=~data_temp;
xs_int(196*data_temp);
if(KEY==0)
{
delay_1ms(10);
if(KEY==0)
while(!KEY);
key_count++;
if(key_count==3)
key_count=1;
xiezhiling(0x01,0);
}
if(key_count==1)
{
xiezhiling(0x80,1);//LCD第一行
xieshuju('P');
xieshuju('R');
xieshuju('E');
xieshuju('S');
xieshuju('S');
xieshuju('U');
xieshuju('R');
xieshuju('E');
xieshuju('-');
xieshuju(HC[5]+0x30);
xieshuju(HC[3]+0x30);
xieshuju(HC[2]+0x30);
xieshuju('.');
xieshuju(HC[1]+0x30);
xieshuju('P');
xieshuju('a');
xiezhiling(0x80+0x40,1);//LCD第二行
xieshuju('A');
xieshuju('l');
xieshuju('a');
xieshuju('r');
xieshuju('m');
xieshuju(' ');
xieshuju('S');
xieshuju('t');
xieshuju('a');
xieshuju('t');
xieshuju('u');
xieshuju('s');
xieshuju('-');
xieshuju('-');
xieshuju('O');
xieshuju('N');
}
if(key_count==2)
{
xiezhiling(0x80,1);//LCD第一行
xieshuju('P');
xieshuju('r');
xieshuju('e');
xieshuju('s');
xieshuju('s');
xieshuju('u');
xieshuju('r');
xieshuju('e');
xieshuju(' ');
xieshuju('i');
xieshuju('s');
xieshuju(' ');
xieshuju('O');
xieshuju('K');
xieshuju(' ');
xieshuju(' ');
xiezhiling(0x80+0x40,1);//LCD第二行
xieshuju('m');
xieshuju('a');
xieshuju('d');
xieshuju('e');
xieshuju(' ');
xieshuju('B');
xieshuju('Y');
xieshuju(' ');
xieshuju('y');
xieshuju('u');
xieshuju('a');
xieshuju('n');
xieshuju('s');
xieshuju('h');
xieshuju('u');
xieshuju('o');
xieshuju(' ');
}
}
}
void
timer0() interrupt 1 // 中断方式1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
buzzer=!buzzer;
}
-
一周热门 更多>