描述
<h3 style="line-height: 1.8;">* 1、项目功能介绍</h3>
<hr>
<p style="line-height: 1.8;">基于STC32G12K128和SHT40温湿度传感器制作的环境温度检测仪</p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/oshwhub/a6675f66119042ae81b2cee333f60e49.png" alt="" width="1080" height="607"></p>
<p style="line-height: 1.8;">使用STC32G12K128-35I-LQFP64制作的最小系统板,引出所有可用引脚,板载蜂鸣器、4PIN、7PIN0.96" OLED显示屏接口,SHT40-AD1B-R2温湿度计传感器。温湿度计可以焊接到主板或者对于焊接没有信心的可以焊接一个4PIN 1.27MM弯针座子插入商城封装好的温湿度计模块。</p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/orGgqvEV8614LV9dIvS779RYF2BtaY5DqzI7b9WY.png"></p>
<p style="line-height: 1.8;"> </p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/uzVd8IVHZjM1JnhpMsNMa8rkuWwCNcztJ9GptyuU.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/YW7aBCGYnFwoPX0TVDz1K624NBp5rBJm71XpPJi7.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/1WdhQAB5mN8ImtfNfOE9P2qXV0sMuBiEPXIzCwyJ.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/iRaUL0RW9hOkTGgSDRIZe5ApZBDnKbLTbcIqYxuO.png"><img src="//image.lceda.cn/pullimage/zguxLOBVpdcISmBlPd2VENSWoYyLIUqGy0aHwzRB.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/GQZ3A1WyZu4iI5CrrS1L55necWBR5t0oZAIIziNE.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/oMxM9ucS4CZaRneSkMprorg3PT1Tsk2tO5L7POFR.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/tpiCOQJ08dMi2QzrVjOhnUvsjqvNhZyPIPhF7QSs.png"></p>
<p style="line-height: 1.8;"> </p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/jDnie2kVnKTwpMQe8wTTDqQLuG35rEKdfwlwsyNp.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/zaYMMC7tDQXBs7IsPRSDTMiD1pkUZYQXK3jX3yzi.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/JHcFbIMUKwwuMS8dzu563M8SHGgRtF1lsotq8EzP.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/Ew1UjZZlNF9Eb2rMbeh0C3dfujX5EZAYm9Tjslvi.png"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/gFCmhBvjtSQm6IBYFNFKNnTjlJ6AbfAvmEK5lEwx.png"></p>
<h3 style="line-height: 1.8;">实物展示</h3>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/r5g7OdZdWhZs81C1b6JD8b9Xcu3YcIQdQ6qSHqjx.jpeg" alt="" width="1080" height="1440"></p>
<p style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/Nya6RBoiQYNC7NTkDecCzErS9P5jnugIQ9be6uLM.jpeg" alt="" width="1080" height="810"><img src="//image.lceda.cn/pullimage/rHYCiPIixXPqQvq8LYO3lQ9atY6KMDmxMlzh0UPz.jpeg" alt="" width="1080" height="1440"><img src="//image.lceda.cn/pullimage/bxnDD4ivQea93Ou9Zezn0MPVn8E0nXnZyHDBlL85.jpeg" alt="" width="1080" height="1440"><img src="//image.lceda.cn/pullimage/6dSborEHoLwtqYgzKKWGzMotz8ogjTx1IiFjdCip.jpeg" alt="" width="1080" height="810"></p>
<p style="line-height: 1.8;"> </p>
<h3 style="line-height: 1.8;">示例代码</h3>
<p style="line-height: 1.8;"><code></code></p>
<p><code>#include <STC32G.H><br>#include <intrins.h></code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// OLED 相关定义</code><br><code>#define OLED_ADDR 0x78</code><br><code>#define OLED_CMD 0x00</code><br><code>#define OLED_DATA 0x40</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// SHT40 相关定义</code><br><code>#define SHT40_ADDR 0x44</code><br><code>#define SHT40_MEASURE_HIGH_PRECISION_CMD 0xFD</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// 定义 OLED 控制引脚</code><br><code>sbit OLED_SCL = P2^5;</code><br><code>sbit OLED_SDA = P2^4;</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// 延时函数</code><br><code>void Delay(unsigned int t)</code><br><code>{</code><br><code> while (t--)</code><br><code> _nop_();</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// I2C 起始信号</code><br><code>void I2C_Start()</code><br><code>{</code><br><code> OLED_SDA = 1;</code><br><code> OLED_SCL = 1;</code><br><code> Delay(5);</code><br><code> OLED_SDA = 0;</code><br><code> Delay(5);</code><br><code> OLED_SCL = 0;</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// I2C 停止信号</code><br><code>void I2C_Stop()</code><br><code>{</code><br><code> OLED_SDA = 0;</code><br><code> OLED_SCL = 1;</code><br><code> Delay(5);</code><br><code> OLED_SDA = 1;</code><br><code> Delay(5);</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// I2C 发送一个字节</code><br><code>void I2C_SendByte(unsigned char dat)</code><br><code>{</code><br><code> unsigned char i;</code><br><code> for (i = 0; i < 8; i++)</code><br><code> {</code><br><code> OLED_SDA = dat & 0x80;</code><br><code> dat <<= 1;</code><br><code> OLED_SCL = 1;</code><br><code> Delay(5);</code><br><code> OLED_SCL = 0;</code><br><code> Delay(5);</code><br><code> }</code><br><code> OLED_SCL = 1;</code><br><code> Delay(5);</code><br><code> OLED_SCL = 0;</code><br><code> Delay(5);</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// I2C 接收一个字节</code><br><code>unsigned char I2C_ReceiveByte()</code><br><code>{</code><br><code> unsigned char i, dat = 0;</code><br><code> OLED_SDA = 1;</code><br><code> for (i = 0; i < 8; i++)</code><br><code> {</code><br><code> OLED_SCL = 1;</code><br><code> Delay(5);</code><br><code> dat <<= 1;</code><br><code> if (OLED_SDA)</code><br><code> dat |= 0x01;</code><br><code> OLED_SCL = 0;</code><br><code> Delay(5);</code><br><code> }</code><br><code> return dat;</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// OLED 写命令</code><br><code>void OLED_WriteCmd(unsigned char cmd)</code><br><code>{</code><br><code> I2C_Start();</code><br><code> I2C_SendByte(OLED_ADDR);</code><br><code> I2C_SendByte(OLED_CMD);</code><br><code> I2C_SendByte(cmd);</code><br><code> I2C_Stop();</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// OLED 写数据</code><br><code>void OLED_WriteData(unsigned char dat)</code><br><code>{</code><br><code> I2C_Start();</code><br><code> I2C_SendByte(OLED_ADDR);</code><br><code> I2C_SendByte(OLED_DATA);</code><br><code> I2C_SendByte(dat);</code><br><code> I2C_Stop();</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// OLED 初始化</code><br><code>void OLED_Init()</code><br><code>{</code><br><code> OLED_WriteCmd(0xAE); // 关闭显示</code><br><code> OLED_WriteCmd(0xD5); // 设置显示时钟分频比/振荡器频率</code><br><code> OLED_WriteCmd(0x80);</code><br><code> OLED_WriteCmd(0xA8); // 设置多路复用比</code><br><code> OLED_WriteCmd(0x3F);</code><br><code> OLED_WriteCmd(0xD3); // 设置显示偏移</code><br><code> OLED_WriteCmd(0x00);</code><br><code> OLED_WriteCmd(0x40); // 设置起始行</code><br><code> OLED_WriteCmd(0x8D); // 电荷泵设置</code><br><code> OLED_WriteCmd(0x14);</code><br><code> OLED_WriteCmd(0x20); // 设置内存地址模式</code><br><code> OLED_WriteCmd(0x02);</code><br><code> OLED_WriteCmd(0xA1); // 设置段重映射</code><br><code> OLED_WriteCmd(0xC8); // 设置 COM 扫描方向</code><br><code> OLED_WriteCmd(0xDA); // 设置 COM 引脚硬件配置</code><br><code> OLED_WriteCmd(0x12);</code><br><code> OLED_WriteCmd(0x81); // 设置对比度控制</code><br><code> OLED_WriteCmd(0xCF);</code><br><code> OLED_WriteCmd(0xD9); // 设置预充电周期</code><br><code> OLED_WriteCmd(0xF1);</code><br><code> OLED_WriteCmd(0xDB); // 设置 VCOMH 调节级别</code><br><code> OLED_WriteCmd(0x40);</code><br><code> OLED_WriteCmd(0xA4); // 全局显示开启/关闭</code><br><code> OLED_WriteCmd(0xA6); // 设置正常/反显显示</code><br><code> OLED_WriteCmd(0xAF); // 开启显示</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// SHT40 发送命令</code><br><code>void SHT40_SendCommand(unsigned char cmd)</code><br><code>{</code><br><code> I2C_Start();</code><br><code> I2C_SendByte(SHT40_ADDR);</code><br><code> I2C_SendByte(cmd);</code><br><code> I2C_Stop();</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>// SHT40 读取数据</code><br><code>unsigned int SHT40_ReadData()</code><br><code>{</code><br><code> unsigned int data = 0;</code><br><code> unsigned char temp[6];</code><br><code> I2C_Start();</code><br><code> I2C_SendByte(SHT40_ADDR + 1);</code><br><code> temp[0] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(1);</code><br><code> temp[1] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(1);</code><br><code> temp[2] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(1);</code><br><code> temp[3] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(1);</code><br><code> temp[4] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(1);</code><br><code> temp[5] = I2C_ReceiveByte();</code><br><code> I2C_SendAck(0);</code><br><code> I2C_Stop();</code><br><code> data = (temp[0] << 8) | temp[1];</code><br><code> return data;</code><br><code>}</code></p>
<p style="line-height: 1.8;"><code></code></p>
<p><code>void main()</code><br><code>{</code><br><code> unsigned int temperature, humidity;</code><br><code> float temp_value, humi_value;</code><br><code> OLED_Init();</code><br><code> OLED_WriteCmd(0x21); // 设置列地址范围</code><br><code> OLED_WriteCmd(0x00);</code><br><code> OLED_WriteCmd(0x7F);</code><br><code> OLED_WriteCmd(0x22); // 设置页地址范围</code><br><code> OLED_WriteCmd(0x00);</code><br><code> OLED_WriteCmd(0x07);</code><br><code> while (1)</code><br><code> {</code><br><code> SHT40_SendCommand(SHT40_MEASURE_HIGH_PRECISION_CMD);</code><br><code> Delay(10000);</code><br><code> temperature = SHT40_ReadData();</code><br><code> humidity = SHT40_ReadData();</code><br><code> temp_value = -45 + 175 * temperature / 65535.0;</code><br><code> humi_value = -6 + 125 * humidity / 65535.0;</code><br><code> OLED_WriteCmd(0x00);</code><br><code> OLED_WriteCmd(0x00);</code><br><code> OLED_WriteData('T');</code><br><code> OLED_WriteData(':');</code><br><code> OLED_WriteData((int)temp_value / 10 + '0');</code><br><code> OLED_WriteData((int)temp_value % 10 + '0');</code><br><code> OLED_WriteData('.');</code><br><code> OLED_WriteData(((int)(temp_value * 10)) % 10 + '0');</code><br><code> OLED_WriteData('C');</code><br><code> OLED_WriteData(' ');</code><br><code> OLED_WriteData('H');</code><br><code> OLED_WriteData(':');</code><br><code> OLED_WriteData((int)humi_value / 10 + '0');</code><br><code> OLED_WriteData((int)humi_value % 10 + '0');</code><br><code> OLED_WriteData('.');</code><br><code> OLED_WriteData(((int)(humi_value * 10)) % 10 + '0');</code><br><code> OLED_WriteData('%');</code><br><code> }</code><br><code>}</code></p>
<p style="line-height: 1.8;"> </p>
评论(1)