1.实际电路
2.引脚图
3.地址
高四位为固定地址1001,A2A1A0可编程地址,通过观察实际电路,可知A2A1A0 为000。最低位为读写为,1为读,0为写。
4.控制字
控制寄存器的高半字节用于 允许模拟输出 将模拟输入编程为单端或差分输入
低半字节选择一个由高半字节定义的模拟输入通道。如果自动增量标志(AUTO-INCREMENT FLAG)置1,每次A/D转换后通道号将自动增加。
5.开始 停止信号
void Start()
{
SDA = ;
delay();
SCL = ;
delay();
SDA = ;
delay();
} void Stop()
{
SDA = ;
delay();
SCL = ;
delay();
SDA = ;
delay();
}
6.应答 非应答信号
void Respons()
{
SDA = ;
delay();
SCL = ;
delay();
SCL = ;
delay();
} void NoRespons()
{
SDA = ;
delay();
SCL = ;
delay();
SCL = ;
delay();
}
7.位传输
void write_byte(unsigned char dat)
{
unsigned char i = ;
for(i = ; i < ; i++) {
dat = dat<<;
SCL = ;
delay();
SDA = CY; //类似于8086的PSW的CF位 即左移data溢出位进入CY
delay();
SCL = ;
delay();
}
SCL = ;
delay();
SDA = ;
delay();
} unsigned char read_byte()
{
unsigned char i = , dat;
SCL = ;
delay();
SDA = ;
for(i = ; i < ; i++) {
SCL = ;
delay();
dat = (dat<<) | SDA;
SCL = ;
delay();
}
return dat;
}
8.IIC总线协议 A/D
读模式总线协议
unsigned char readAdc()
{
unsigned char dat;
Start();
write_byte(0x90); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 写信号
Respons();
write_byte(0x40); //控制字 通道0
Respons(); Start();
write_byte(0x91); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 读信号
Respons();
dat = read_byte();
NoRespons();
Stop();
return dat;
}
9.IIC总线协议 D/A
写模式总线协议
void writeDAC(unsigned char dat)
{
Start();
write_byte(0x90); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 写信号
Respons();
write_byte(0x40); //控制字
Respons();
write_byte(dat);
Respons();
Stop();
}
10.功能实现
//IIC PCF8591 A/D D/A
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
sbit SDA = P2^;
sbit SCL = P2^; void delay()
{;;} void delayms(unsigned int n) //误差 -0.651041666667us
{
unsigned char a,b;
unsigned int i;
for(i = ; i < n; i++) {
for(b=;b>;b--)
for(a=;a>;a--);
}
} void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
TR1 = ;
} void Init()
{
SCL = ;
delay();
SDA = ;
delay();
} void Start()
{
SDA = ;
delay();
SCL = ;
delay();
SDA = ;
delay();
} void Stop()
{
SDA = ;
delay();
SCL = ;
delay();
SDA = ;
delay();
} void Respons()
{
SDA = ;
delay();
SCL = ;
delay();
SCL = ;
delay();
} void NoRespons()
{
SDA = ;
delay();
SCL = ;
delay();
SCL = ;
delay();
} void write_byte(unsigned char dat)
{
unsigned char i = ;
for(i = ; i < ; i++) {
dat = dat<<;
SCL = ;
delay();
SDA = CY; //类似于8086的PSW的CF位 即左移data溢出位进入CY
delay();
SCL = ;
delay();
}
SCL = ;
delay();
SDA = ;
delay();
} unsigned char read_byte()
{
unsigned char i = , dat;
SCL = ;
delay();
SDA = ;
for(i = ; i < ; i++) {
SCL = ;
delay();
dat = (dat<<) | SDA;
SCL = ;
delay();
}
return dat;
} unsigned char readAdc()
{
unsigned char dat;
Start();
write_byte(0x90); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 写信号
Respons();
write_byte(0x40); //控制字 通道0
Respons(); Start();
write_byte(0x91); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 读信号
Respons();
dat = read_byte();
NoRespons();
Stop();
return dat;
} void writeDAC(unsigned char dat)
{
Start();
write_byte(0x90); //前四位地址1001 后三位根据硬件000 最后一位R/W(上划线) 写信号
Respons();
write_byte(0x40); //控制字
Respons();
write_byte(dat);
Respons();
Stop();
} void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = ;
}
int main()
{
unsigned char dat;
unsigned char tmpDA = ;
Init();
InitUART(); while()
{
delayms();
dat = readAdc();
SendOneByte(dat); tmpDA++;
if(tmpDA == )
tmpDA = ;
writeDAC(tmpDA);
}
return ;
}
AD直接采集DA出来的电压串口发送 验证成功