#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^2;
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit cs=P3^4;
sbit clk=P1^0;
sbit dio=P1^1;
uchar code table[]=" volt=";
uchar num;
//延时函数
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//lcd写指令函数和写数据函数
void write_com(uchar com)
{
lcdrs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//lcd初始化函数
void init()
{
lcdrw=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
//读adc0832数据
uchar dac0832()
{
uchar i,dat;
cs=1;
clk=0;
cs=0;
clk=1;
dio=1;
clk=0;//第一个脉冲下降沿
clk=1;
dio=1;
clk=0;//第二个脉冲下降沿
clk=1;
dio=0;
clk=0;//第三个脉冲下降沿
dio=1;
clk=1;//第四个脉冲上升沿
for(i=0;i<8;i++)
{
clk=1;
clk=0;
dat<<=1;
dat|=(uchar)dio;
}
cs=1;
return dat;
}
void main()
{
uint AD_val;//读出转换数据
uchar Int,Dec;//Int存整数,Dec存小数
init();
while(1)
{
AD_val=dac0832();
Int=(AD_val)/51;
Dec=(AD_val%51)*100/51;
for(num=0;num<9;num++)
{
write_data(table[num]);
delay(4);
}
write_data(0x30+Int);
write_data(0x2e);
write_data(0x30+Dec/10);
write_data(0x30+Dec%10);
write_data(0x56);
write_com(0x02);
}
}
评论2
最新资源