基于IIC總線的AD轉(zhuǎn)換模塊PCF8591
uchar read_adc(uchar channel)
{
uchar temp;
start();
send_byte(addw);
ack();
send_byte(0x40|channel);
ack();
start();
send_byte(addr);
ack();
temp=read_byte();
Noack();
stop();
return temp;
}
void init()
{
scl=1;
sda=1;
}
/*********1602程序********************/
void write_com(uchar com)
{
rs=0;
rw=0;
en=0;
P0=com;
delayms(2);
en=1;
delayms(2);
en=0;
}
void write_dat(uchar dat)
{
rs=1;
rw=0;
en=0;
P0=dat;
delayms(2);
en=1;
delayms(2);
en=0;
}
void zfc(uchar *p)
{
while(*p!=0)
write_dat(*p++);
}
void init_1602()
{
write_com(0x38);
delayms(2);
write_com(0x0c);
delayms(2);
write_com(0x06);
delayms(2);
write_com(0x01);
write_com(0x80);
zfc("Channel 0 :");
write_com(0x80+0x40);
zfc("Channel 1 :");
}
void display()
{
uchar a0,a1,a2,b0,b1,b2;
a0=Ch0value*196/10000;
a1=Ch0value*196%10000/1000;
a2=Ch0value*196%1000/100;
b0=Ch1value*196/10000;
b1=Ch1value*196%10000/1000;
b2=Ch1value*196%1000/100;
write_com(0x80+11);
write_dat(0x30+a0);
write_dat(.);
write_dat(0x30+a1);
write_dat(0x30+a2);
write_dat(V);
write_com(0x80+0x40+11);
write_dat(0x30+b0);
write_dat(.);
write_dat(0x30+b1);
write_dat(0x30+b2);
write_dat(V);
}
void main()
{
init_1602();
init();
while(1)
{
Ch0value=read_adc(0);
delayms(50);
Ch1value=read_adc(1);
delayms(50);
display();
}
}
評(píng)論