PCF8591采集温度源码程序---STC89C52实时用PCF8591采集温度LCD显示

一、创建头文件pcf8591.h代码如下:

#ifndef __FPC8591_H_
#define __FPC8591_H_

#include <reg52.h>

void init_pcf8591(void); //pcf8591初始化
unsigned char adc_pcf8591(void); 
unsigned char Read_D(unsigned char Channel); //pcf8591读取通道模数转换
void Out_A(unsigned char Digital);    //pcf8591数模转换

#endif

二、创建编译文件pcf8591.c代码如下:

#include"fpc8591.h"
#include"i2c.h"

#define fun(x) (int)(5*x/255.0*100+0.5)             //数字电压x转换为模拟电压的公式

void init_pcf8591(void)
{
    I2cStart();
    
    I2cSendByte(0x90); //大家可以看看IAP15F2K61S2的原理图,就知道为什么是0x90了
    
    I2cWaitAck();
    I2cSendByte(0x03); //选择ADC通道3
    I2cWaitAck();
    I2cStop();
}

//接收PCF8591转换过的采样电压值

unsigned char adc_pcf8591(void)
{
    unsigned char temp;
    
    I2cStart();
    I2cSendByte(0x91);
    I2cWaitAck();
    temp = I2cReadByte();
    I2cAck(0);
    I2cStop();
    
    return temp;

}

/*读取某一个通道转换后的数字量*/
unsigned char Read_D(unsigned char Channel)
{
    unsigned char dat;
    I2cStart();
    I2cSendByte(0x90); //器件地址+0
    I2cAck(0);
    I2cSendByte(Channel); //控制字0x01表示通道1
    I2cAck(0);
    I2cStart();
    I2cSendByte(0x91); //器件地址+1,下一个字节要读取
    I2cAck(0);
    dat=I2cReadByte();
    I2cAck(0);
    I2cStop();
    // AD_led=0; //转换成功显示
    return dat;
}
/*DA转换*/
void Out_A(unsigned char Digital)              
{
    I2cStart();
    I2cSendByte(0x90);               //器件地址+0,下一个字节为写入
    I2cAck(0);
    I2cSendByte(0x40);               //设置控制字         0100 0000 允许模拟输出,不自增单端
    I2cAck(0);
    I2cSendByte(Digital);        //将要转换的数字量写入
    I2cAck(0); 
    I2cStop();
    //  DA_led=0;                           //转换成功显示               
}

三、主函数见:https://www.cnblogs.com/wlei5206/p/13023977.html

原文地址:https://www.cnblogs.com/wlei5206/p/13024063.html