本次工程为一个乌龙,布置的任务为读取RS485温湿度变送器的数据,经查阅资料得知,内部核心芯片为sht30,故想到解决方案与实际出入很多。然后笔者求知若渴,不断上网找寻资料,发现很多人都做出成品,但不开源,有的需要支付RMB,有的还需要一键三连,于是,笔者决定自己编写代码。(代码少,而且简单)

 一.硬件

 笔者使用的是普中51单片机集成开发盒,sht30模块,LCD1602模块。

在此提一嘴,虽然使用的为普中集成开发盒,但可移植性还是在的,也就是说换个任意开发板,都可以套用。

二.程序代码 

 此次工程属实简单,不多逼逼,直接见代码。

#include <reg52.h>
#include <intrins.h>

#define LCD1602_DB P0   // 数据总线
sbit SCL = P2^1;        // I2C时钟线
sbit SDA = P2^0;        // I2C数据线
sbit RS = P2^6;         // LCD1602 RS引脚
sbit RW = P2^5;         // LCD1602 RW引脚
sbit EN = P2^7;         // LCD1602 EN引脚

void Delay1ms(unsigned int count) {
    unsigned int i, j;
    for(i = 0; i < count; i++)
        for(j = 0; j < 110; j++);
}

void I2C_Start() {
    SDA = 1;
    SCL = 1;
    _nop_();
    _nop_();
    SDA = 0;
    _nop_();
    _nop_();
    SCL = 0;
}

void I2C_Stop() {
    SDA = 0;
    SCL = 1;
    _nop_();
    _nop_();
    SDA = 1;
}

void I2C_SendByte(unsigned char dat) {
    unsigned char i;
    for(i = 0; i < 8; i++) {
        SDA = dat & 0x80;
        SCL = 1;
        _nop_();
        _nop_();
        SCL = 0;
        dat <<= 1;
    }
}

unsigned char I2C_ReadByte() {
    unsigned char i, dat;
    SDA = 1;
    for(i = 0; i < 8; i++) {
        SCL = 1;
        _nop_();
        _nop_();
        dat = (dat << 1) | SDA;
        SCL = 0;
    }
    return dat;
}

void LCD1602_WriteCmd(unsigned char cmd) {
    RS = 0;
    RW = 0;
    LCD1602_DB = cmd;
    EN = 1;
    Delay1ms(5);
    EN = 0;
}

void LCD1602_WriteData(unsigned char dat) {
    RS = 1;
    RW = 0;
    LCD1602_DB = dat;
    EN = 1;
    Delay1ms(5);
    EN = 0;
}

void LCD1602_Init() {
    LCD1602_WriteCmd(0x38);
    Delay1ms(5);
    LCD1602_WriteCmd(0x0c);
    Delay1ms(5);
    LCD1602_WriteCmd(0x06);
    Delay1ms(5);
    LCD1602_WriteCmd(0x01);
    Delay1ms(5);
}

void LCD1602_DisplayString(unsigned char x, unsigned char y, unsigned char *str) {
    unsigned char addr;
    if(y == 0) {
        addr = 0x80 + x;
    } else {
        addr = 0xc0 + x;
    }
    LCD1602_WriteCmd(addr);
    while(*str != '\0') {
        LCD1602_WriteData(*str);
        str++;
    }
}

void main() {
    unsigned char temperature[5], humidity[5];
    
    I2C_Start();
    I2C_SendByte(0x44);
    I2C_SendByte(0x2f);
    I2C_Stop();
    Delay1ms(50);
    
    LCD1602_Init();
    
    while(1) {
        I2C_Start();
        I2C_SendByte(0x45);
        I2C_SendByte(0x00);
        I2C_SendByte(0x00);
        I2C_Stop();
        Delay1ms(260);
        
        I2C_Start();
        I2C_SendByte(0x45);
        humidity[0] = I2C_ReadByte();
        I2C_SendByte(0xff);
        humidity[1] = I2C_ReadByte();
        I2C_SendByte(0x2f);
        temperature[0] = I2C_ReadByte();
        I2C_SendByte(0xff);
        temperature[1] = I2C_ReadByte();
        I2C_Stop();
        
        LCD1602_DisplayString(0, 0, "Temp:");
        LCD1602_DisplayString(6, 0, temperature);
        LCD1602_DisplayString(0, 1, "Humidity:");
        LCD1602_DisplayString(9, 1, humidity);
        
        Delay1ms(1000);  // 延时1秒
    }
}

IIC通讯协议要搞清楚,时序控制是关键,对于LCD并没有调用库,直接从地址写,可能是由于笔者个人习惯,从底层写,才更容易移植,不会出现差异化,接线就不附上了,因为笔者没有做实物,但基本功能已经实现。 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐