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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM