#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define PCF8591 0x90 //PCF8591 地址
sbit SCL=P2^0;//时钟脉冲
sbit SDA=P2^1;//双向输入输出数据端
void delay(uint cnt)
{
while(--cnt);
}
void init()
{
SCL=1;
delay(2);
SDA=1;
delay(2);
}
void Start_I2c()
{
SDA=1;
delay(1);
SCL=1;
delay(5);
SDA=0;
delay(5);
SCL=0;
delay(2);
}
void respons ( )
{
uchar i=0;
SCL=1;
delay(3);
while((SDA==1)&&(i<255))
i++;
SCL=0;
delay(5);
}
void Stop_I2c()
{
SDA=0;
delay(1);
SCL=1;
delay(5);
SDA=1;
delay(4);
}
void write(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
delay(5);
SDA=CY;
delay(2);
SCL=1;
delay(3);
}
SCL=0;
delay(2);
SDA=1;
delay(2);
}
void DACconversion(unsigned char sla,unsigned char c, unsigned char Val)
{
Start_I2c(); //启动总线
write(sla);
respons ( ); //发送器件地址
write(c);
respons ( ); //发送控制字节
write(Val);
respons ( ); //发送DAC的数值
Stop_I2c(); //结束总线
}
void main()
{
init();
while(1)
{
DACconversion(0x90,0x40, 0xff);
}
} |