本帖最后由 小白baibai 于 2019-8-14 07:59 编辑
#include <reg52.h>
//#define PCF8591 0x90; //器件地址
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^2;
sbit rw=P2^3;
sbit en=P2^4;
sbit SCL=P1^2;
sbit SDA=P1^3;
uchar AD_var;//AD转换放置位
uchar code table[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
/*unsigned int code NTC_Table[71] = {
0xCA,0xCA,0xCA,0xC9,0xC9,0xC9,0xC9,0xC8,
0xC8,0xC8,0xC8,0xC7,0xC7,0xC7,0xC7,0xC7,
0xC6,0xC6,0xC6,0xC6,0xC5,0xC5,0xC5,0xC5,
0xC4,0xC4,0xC4,0xC4,0xC3,0xC3,0xC3,0xC3,
0xC2,0xC2,0xC2,0xC2,0xC1,0xC1,0xC1,0xC1,
0xC0,0xC0,0xC0,0xC0,0xBF,0xBF,0xBF,0xBF,
0xBE,0xBE,0xBE,0xBE,0xBD,0xBD,0xBD,0xBD,
0xBC,0xBC,0xBC,0xBC,0xBB,0xBB,0xBB,0xBB,
0xBA,0xBA,0xBA,0xBA,0xB9,0xB9,0xB9
};*/
void delay_ms(uint z)
{
uint a,b;
for(a=z;a>0;a--)
for(b=110;z>0;z--);
}
void delay_us()//5us
{;;}
void write_com(uchar com)//写指令
{
rs=0;
rw = 0;
P0=com;
delay_ms(5);
en=1;
delay_ms(5);
en=0;
}
void write_dat(uchar dat)//写数据
{
rs=1;
rw = 0;
P0=dat;
delay_ms(5);
en=1;
delay_ms(5);
en=0;
}
void start()//I2C起始信号
{
SCL=1;
SDA=1;
delay_us();
SDA=0;
delay_us();
SCL=0;
delay_us();
}
void stop()//I2C停止信号
{
SCL=1;
SDA=0;
delay_us();
SDA=1;
delay_us();
}
void respons()//应答
{
uchar i=0;
SCL = 1;
delay_us();
while((SDA == 1)&&(i < 255))i++;
SCL = 0;
delay_us();
}
//void no_ack()//主机非应答
//{
// SCL=0;
// SDA=0;
// delay_us();
// SCL=1;
// SDA=1;
// delay_us();
// SCL=0;
// SDA=0;
//}
void write_cm(uchar cm)//写一个字节
{
uchar i,temp;
temp=cm;
for(i=0;i<8;i++)
{
temp<<=1;
SCL=0;
delay_us();
SDA=CY;
delay_us();
SCL=1;
delay_us();
}
SCL=0;
delay_us();
SDA=1;
delay_us();
}
uchar read_cm(void)//读一个字节
{
uchar i,om;
SCL=0;
delay_us();
SDA=1;
for(i=0;i<8;i++)
{
SCL=0;
SDA=0;
SCL=1;
delay_us();
om = (om<<1)|SDA;
SCL=0;
delay_us();
}
delay_us();
return om;
}
uchar lead(uchar lea)//读AD值
{
start();
write_cm(0x90);
respons();
write_cm(lea);
respons();
start();
write_cm(0x91);
respons();
AD_var = read_cm();
respons();
stop();
return(1);
}
//uchar DAC_OUT(uchar DAT)
//{
// start();
// write_cm(0x90);
// respons();
// write_cm(0x40); //DA输出使能
// respons();
// write_cm(DAT);
// respons();
// stop();
// return(1);
//}
void init()
{
// write_com(0x01);
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x80+0x02);
write_dat('.');
}
void main()
{
uchar a,b,c;
init();
while(1)
{
if(lead(0x00))
// if(DAC_OUT(AD_var));
{
a=AD_var/100;
b=AD_var%100/10;
c=AD_var%10;
}
// d=AD_var%100/10;
// e=AD_var%10;
write_com(0x80);
write_dat(table[a]);
write_com(0x80+0x01);
write_dat(table);
write_com(0x80+0x03);
write_dat(table[c]);
// write_com(0x80+0x04);
// write_dat(table[d]);
// write_com(0x80+0x05);
// write_dat(table[e]);
delay_ms(30);
}
}
|