51单片机驱动PCF8591AD/DA转换,功能:仅进行AD转换, 数码管显示模拟量,显示范围0.0~5.0,使用了
定时器1进行定时和I2C总线通信。
#include<reg52.h>
#define uchar unsigned char
sbit sda=P2^0;
sbit scl=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
sbit rst=P2^4;
uchar c;
uchar a[2];
bit ADflag;
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x80,//最后为小数点
};
void init();
void start();
void stop();
void response();
uchar read_byte();
void write_byte(uchar data1);
uchar ADC(uchar ch);
void display();
void delay_100(int z) ;
void main()
{
uchar tem;
rst=0;
init();
while(1)
{
if(ADflag)//ADflag由定时器1控制
{
ADflag=0;
tem=ADC(2);
a[0]=tem/50;
a[1]=(tem%50)/10;
display();
}
}
}
void init()
{
TMOD=0x10;
TH1=(65535-500)/256;
TL1=(65535-500)%256;
EA=1;
TR1=1;
ET1=1;
}
void timer1() interrupt 3
{
TH1=(65535-500)/256;
TL1=(65535-500)%256;
c++;