基于IIC總線的AD轉(zhuǎn)換模塊PCF8591
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define addw 0x90
#define addr 0x91
sbit scl=P2^0;
sbit sda=P2^1;
sbit rs=P2^6;
sbit rw=P2^5;
sbiten=P2^7;
uchar Ch0value,Ch1value;
void delayms(int i)
{int x,y;
for (x=i;x>0;x--)
for(y=110;y>0;y--);
}
void usdelay()
{
_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
}
void start()//開始信號(hào)
{//時(shí)鐘線在高電平時(shí),數(shù)據(jù)線由高變低即為開始信號(hào)由高變低即為開始信號(hào)
sda=1;
usdelay();
scl=1;
usdelay();
sda=0;//數(shù)據(jù)線由高變低即為開始信號(hào)
usdelay();
scl=0;
}
void stop()//結(jié)束信號(hào)
{//時(shí)鐘線在高電平時(shí),數(shù)據(jù)線由低變高即為結(jié)束信號(hào)
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
sda=0;//這句可有可無
scl=0;//這句可有可無
}
void ack()//應(yīng)答信號(hào)
{uchar i;
scl=1;
usdelay();
while((sda==1)&&(i<200))i++;
scl=0;
usdelay();
}
void Noack() //不應(yīng)答
{
sda=1;//時(shí)鐘線高電平時(shí),數(shù)據(jù)線高,即為不應(yīng)答
usdelay();
scl=1;
usdelay();
scl=0;
usdelay();
}
void send_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
scl=0;
usdelay();
sda=(bit)(dat&0x80); //數(shù)據(jù)傳送是由高到低
//(bit)為強(qiáng)制轉(zhuǎn)換命令,把數(shù)據(jù)變?yōu)榉?即1的一位數(shù)據(jù)庫
usdelay();
scl=1;
usdelay();
dat<<=1;
}
scl=0;
usdelay();
sda=1;//釋放總線
usdelay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
usdelay();
sda=1; //釋放總線
usdelay();
for(i=0;i<8;i++)
{
scl=1;
usdelay();
k=(k<<1)|sda;//判斷8次,移位7次
scl=0;
usdelay();
}
return k;
}
評(píng)論