STC89C52實時用PCF8591采集溫度LCD顯示


一、主函數如下:

#include <reg52.h>    //此文件中定義了單片機的一些特殊功能寄存器
#include "lcd1602.h"
#include"fpc8591.h"
#include"i2c.h"


/*******************************************************************************
* 函 數 名       : main
* 函數功能         : 主函數
* 輸    入       : 無
* 輸    出         : 無
*******************************************************************************/
void main()
{
    unsigned int temp; 
    int_1602();//lcd1602初始化
    while(1)
    {
        temp=Read_D(0);//pcf8591通道0采集溫度
        display(temp);//lcd1602顯示
    }        
}

二、現象如下:

三、LCD1602顯示部分程序

void display(unsigned int temp)            //顯示溫度
{
    uchar rev_data[16]={" -temperature!- "};
    com_1602(0x80);
    for(i=0;i<16;i++)                //發送數據第一行
    {
        dat_1602(rev_data[i]);
    }
    temp*=10;
    com_1602(0xc0);//第二行
    dat_1602('T');
    dat_1602('H');
    dat_1602('=');
    dat_1602(temp/100%10+0x30);
    dat_1602(temp/10%10+0x30);
    dat_1602('.');
    dat_1602(temp%10+0x30);
    dat_1602(0xdf);
    dat_1602('C');
}

LCD1602顯示完整程序見:https://www.cnblogs.com/wlei5206/p/13024011.html

 

四、PCF8591模數部分程序

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;
}

PCF8591模數完整程序見:https://www.cnblogs.com/wlei5206/p/13024063.html

 

五、IIC驅動部分程序

unsigned char I2cSendByte(unsigned char dat)
{
    unsigned char a=0,b=0;//最大255,一個機器周期為1us,最大延時255us。        
    for(a=0;a<8;a++)//要發送8位,從最高位開始
    {
        SDA=dat>>7;     //起始信號之后SCL=0,所以可以直接改變SDA信號
        dat=dat<<1;
        Delay10us();
        SCL=1;
        Delay10us();//建立時間>4.7us
        SCL=0;
        Delay10us();//時間大於4us        
    }
    SDA=1;
    Delay10us();
    SCL=1;
    while(SDA)//等待應答,也就是等待從設備把SDA拉低
    {
        b++;
        if(b>200)     //如果超過2000us沒有應答發送失敗,或者為非應答,表示接收結束
        {
            SCL=0;
            Delay10us();
            return 0;
        }
    }
    SCL=0;
    Delay10us();
     return 1;        
}

unsigned char I2cReadByte()
{
    unsigned char a=0,dat=0;
    SDA=1;            //起始和發送一個字節之后SCL都是0
    Delay10us();
    for(a=0;a<8;a++)//接收8個字節
    {
        SCL=1;
        Delay10us();
        dat<<=1;
        dat|=SDA;
        Delay10us();
        SCL=0;
        Delay10us();
    }
    return dat;        
}

IIC驅動完整程序見:https://www.cnblogs.com/wlei5206/p/13024116.html

 


免責聲明!

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



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