#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit cs=P3^5;
sbit clk=P3^3;
sbit da
ti=P3^4;
sbit dato=P3^4;
sbit lcden=P2^3;
sbit lcdrs=P2^5;
sbit dula =P2^6;
sbit wela=P2^7;
uchar dat=0x00;
uchar count=0x00;
uchar numu;
uchar CH;
uchar dis[]={0x01,0x02,0x03,0x04};
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code tablewe[]={0xfd,0xfb,0xf7,0xf0,0xdf,0xfe};
uchar table[3];
//adc0832初始化函数,用途读入数据
uchar adc0832(uchar CH)
{ uchar i,test,adval;
adval=0x00;
test=0x00;
clk=0;
dati=1;
_nop_();cs=0; _nop_();clk=1;_nop_();
if(CH==0x00)
{ clk=0;
dati=1;
_nop_();
clk=1;
_nop_();
clk=0;
dati=0;
_nop_();
clk=1;
_nop_();
}
else {
clk=0;
dati=1;
_nop_();
clk=1;
_nop_();
clk=1;
_nop_();
clk=0;
dati=1;
_nop_();
clk=1;
_nop_();
}
clk=0;
dati=1;
for(i=0;i<8;i++)
{_nop_();
adval<<=1;
clk=1;
_nop_();
clk=0;
if(dato) adval|=0x01;
else adval|=0x00;
}
for(i=0;i<8;i++)
{test>>=1;
if(dato) test|=0x80;
else test|=0x00;
_nop_();
clk=1;
_nop_();
clk=0;
}
if(adval==test)
dat=test;_nop_();
cs=1;
dato=1;clk=1;return dat;}
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//写命令操作函数
void write_com(uchar com)
{
lcdrs =0;
P0=com;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
//写数据操作函数
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
//对显示频进行初始化函数
void init()
{
dula=0;
wela =0;
lcden =0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void delay(void)//延时函数
{uint r;
for(r=10;r<500;r++)
;
}
//原取数字函数
void convdata(uchar i)
{ dis[0]=i/51;
dis[1]=(i%51)*10/51;
dis[2]=((i%51)*10%51)*10/51;
}
//显示函数
void display(void)
{
table[0]=tab[dis[0]];
delay();
table[1]='.';
table[2]=tab[dis[1]];
delay();
P2=0x08;
table[3]=tab[dis[2]];
delay();
write_com(80);
for(numu=0;numu<16;numu++)
{
if (numu==8||numu==11||numu==10||numu==9)
{
write_data(table[numu-8]);
delay1(5);
}
else write_data(' ');
}
}
//主函数
void main(void)
{ P2=0xff;
P0=0xff;
delay();
delay();
CH=0x00;
TMOD=0x01;
TH0=15535/256;
TL0=15536%256;
IE=0x82;
TR0=1;
init();
while(1)
{dat=adc0832(CH);
convdata(dat);
display();
}}
void timero(void) interrupt 1
{ TMOD=0x01;
TMOD=0x01;
TH0=15535/256;
TL0=15536%256;
IE=0x82;
TR0=1;
count++;
if(count==0x01)
{count=0x00;dat=adc0832(CH);}}
-
http://bbs.elecfans.com/jishu_401238_1_1.html
一周热门 更多>