STC89C52实时用PCF8591采集温度四位数码管显示

一、主函数如下:

#include <reg52.h>    //此文件中定义了单片机的一些特殊功能寄存器
#include"fpc8591.h"
#include"i2c.h"
#include "74hc595.h"


/*******************************************************************************
* 函 数 名       : main
* 函数功能         : 主函数
* 输    入       : 无
* 输    出         : 无
*******************************************************************************/
void main()
{
    unsigned int temp; 
    while(1)
    {
        temp=Read_D(0);//pcf8591通道0采集温度
        LED4_data(temp);
        LED4_Display();
    }        
}

二、现象如下:

三、四位数码管显示部分程序:

void LED_OUT(uchar X)
{
    uchar i;
    for(i=8;i>=1;i--)
    {
        if (X&0x80) DIO=1; else DIO=0;
        X<<=1;
        HCSCLK = 0;
        HCSCLK = 1;
    }
}

四位数码管显示完整程序见:https://www.cnblogs.com/wlei5206/p/13036832.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/13036420.html