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

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