AD转换:
#include
#include
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit dout=P2^3;
sbit addr=P2^4;
sbit cs=P2^5;
sbit clk=P2^6;
sbit eoc=P2^7;
void delay(int t)
{
while(t--);
}
void write_com(unsigned char com)
{
e=0;
rs=0;
rw=0;
P1=com;
delay(50);
e=1;
delay(50);
e=0;
delay(50);
}
void write_data(unsigned char dat)
{
e=0;
rs=1;
rw=0;
P1=dat;
delay(50);
e=1;
delay(50);
e=0;
delay(50);
}
void start()
{
delay(200);
write_com(0x38);
delay(200);
write_com(0x38);
delay(200);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);;
write_com(0x0c);
}
void lcd(char *p)
{
unsigned char zm1[]=" Voltage value:";
int j;
//start();
write_com(0x80);
for(j=0;j<16;j++)
{
write_data(zm1[j]);
//delay(200);
}
write_com(0x80+0x40);
for(j=0;j<10;j++)
{
write_data(p[j]);
//delay(200);
}
}
void shuju(char ad_addr,unsigned char *p)
{float res;
unsigned result;
char i,m;
ad_addr<<=4;
cs=0;
eoc=1;
result=0;
for(i=0;i<10;i++) //完成通道的选择
{
addr=(bit)(ad_addr&0x80);
clk=1;
_nop_();
result<<=1;
m=dout;
result=result|m;
clk=0;
_nop_();
ad_addr<<=1;
}
cs=1;
res=result/204.6;
/**(p+4)=(int)res+0x30;
*(p+6)=(int)(res*10)%10+0x30; //两种形式都能用
*(p+7)=(int)(res*100)%10+0x30;
*(p+8)=(int)(res*1000)%10+0x30;
*(p+9)='v';
*(p+5)='.'; */
p[4]=(int)res+0x30;
p[6]=(int)(res*10)%10+0x30;
p[7]=(int)(res*100)%10+0x30;
p[8]=(int)(res*1000)%10+0x30;
p[9]='v';
p[5]='.';
}
void main()
{ unsigned char zm2[16];
start();
lcd(zm2);
while(1)
{
shuju(1,zm2); //选择通道adin:1
lcd(zm2);
}
}
阅读(1093) | 评论(0) | 转发(1) |