【上海航芯 ACM32F070开发板+触控功能评估板】06.在LCD实时显示SHT30温湿度数据
[复制链接]
ACM32F070系列MCU带有2路硬件I2C接口,并且在CoreBoard扩展板上板载一颗SHT30湿湿度传感器,我们可以通过硬件I2C接口来读取SHT30的温度、湿度数据,将其结果显示的LCD液晶屏上,实现一个实时温湿度监测的功能。
I2C主要特性如下:
I2C 主设备功能;
I2C 从设备功能:
- 可编程的 I2C 从设备地址;
- 可编程的 NACK/ACK 回复;
速率支持 100K 标准模式/400K 快速模式/1M 快速加模式;
支持 7bit 设备地址;
支持主机仲裁模式;
原理图:
SHT30程序实现:
/*******************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] SHT30.c
* [url=home.php?mod=space&uid=1315547]@author[/url] King
* [url=home.php?mod=space&uid=252314]@version[/url] V1.00
* [url=home.php?mod=space&uid=311857]@date[/url] 21-Oct-2022
* [url=home.php?mod=space&uid=159083]@brief[/url] ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __SHT30_C__
/* Includes ------------------------------------------------------------------*/
#include "SHT30.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum
{
/* 软件复位命令 */
SOFT_RESET_CMD = 0x30A2,
CLEAR_STATES_CMD = 0x3041,
/*
单次测量模式
命名格式:Repeatability_CS_CMD
CS:Clock stretching
*/
HIGH_ENABLED_CMD = 0x2C06,
MEDIUM_ENABLED_CMD = 0x2C0D,
LOW_ENABLED_CMD = 0x2C10,
HIGH_DISABLED_CMD = 0x2400,
MEDIUM_DISABLED_CMD = 0x240B,
LOW_DISABLED_CMD = 0x2416,
/*
周期测量模式
命名格式:Repeatability_MPS_CMD
MPS:measurement per second
*/
HIGH_0_5_CMD = 0x2032,
MEDIUM_0_5_CMD = 0x2024,
LOW_0_5_CMD = 0x202F,
HIGH_1_CMD = 0x2130,
MEDIUM_1_CMD = 0x2126,
LOW_1_CMD = 0x212D,
HIGH_2_CMD = 0x2236,
MEDIUM_2_CMD = 0x2220,
LOW_2_CMD = 0x222B,
HIGH_4_CMD = 0x2334,
MEDIUM_4_CMD = 0x2322,
LOW_4_CMD = 0x2329,
HIGH_10_CMD = 0x2737,
MEDIUM_10_CMD = 0x2721,
LOW_10_CMD = 0x272A,
/* 周期测量模式读取数据命令 */
READOUT_FOR_PERIODIC_MODE = 0xE000,
} SHT30_CMD;
/* Private define ------------------------------------------------------------*/
#define SHT30_SLAVE_ADDRESS (0x44<<1)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef I2C_Handle;
/* Private variables ---------------------------------------------------------*/
MultiTimer SHT30_MultiTimer;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_Handle;
/* I2C1 */
/* I2C2 */
if(hi2c->Instance == I2C2)
{
System_Module_Enable(EN_I2C2);
System_Module_Enable(EN_GPIOCD);
/* I2C2 SDA PortD Pin7 */
/* I2C2 SCL PortD Pin6 */
GPIO_Handle.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Alternate = GPIO_FUNCTION_1;
HAL_GPIO_Init(GPIOD, &GPIO_Handle);
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(I2C2_IRQn);
/* Enable External Interrupt */
NVIC_EnableIRQ(I2C2_IRQn);
}
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t SHT30_Reset(I2C_HandleTypeDef I2C_Handle)
{
HAL_StatusTypeDef Status;
uint8_t CMD_Buffer[2];
CMD_Buffer[0] = SOFT_RESET_CMD >> 0x08;
CMD_Buffer[1] = SOFT_RESET_CMD & 0xFF;
Status = HAL_I2C_Master_Transmit(&I2C_Handle, SHT30_SLAVE_ADDRESS, CMD_Buffer, 2, 100);
System_Delay_MS(500);
CMD_Buffer[0] = CLEAR_STATES_CMD >> 0x08;
CMD_Buffer[1] = CLEAR_STATES_CMD & 0xFF;
Status = HAL_I2C_Master_Transmit(&I2C_Handle, SHT30_SLAVE_ADDRESS, CMD_Buffer, 2, 100);
System_Delay_MS(200);
return Status;
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t SHT30_Read_Dat(I2C_HandleTypeDef I2C_Handle, uint8_t *Data)
{
uint8_t CMD_Buffer[2];
CMD_Buffer[0] = HIGH_ENABLED_CMD >> 0x08;
CMD_Buffer[1] = HIGH_ENABLED_CMD & 0xFF;
HAL_I2C_Master_Transmit(&I2C_Handle, SHT30_SLAVE_ADDRESS, CMD_Buffer, 2, 100);
System_Delay_MS(100);
return HAL_I2C_Master_Receive(&I2C_Handle, SHT30_SLAVE_ADDRESS, Data, 6, 100);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void SHT30_MultiTimerCallback(MultiTimer *timer, void *userData)
{
float Temperature;
float Humidity;
uint16_t tem = 0, hum = 0;
uint8_t SHT30_RxBuffer[99];
SHT30_Read_Dat(I2C_Handle, SHT30_RxBuffer);
tem = SHT30_RxBuffer[0];
tem <<= 0x08;
tem |= SHT30_RxBuffer[1];
hum = SHT30_RxBuffer[3];
hum <<= 0x08;
hum |= SHT30_RxBuffer[4];
/* T = -45 + 175 * tem / (2^16 - 1) */
Temperature = (175.0 * (float)tem / 65535.0 - 45.0);
/* RH = hum * 100 / (2^16 - 1) */
Humidity = (100.0 * (float)hum / 65535.0);
printf("\r\nTemperature : %0.1f, Humidity : %0.1f", Temperature, Humidity);
Temperature *= 10;
Humidity *= 10;
LCD_DisplayNumber1(0, '0' + (((uint16_t)Temperature / 1000) % 10), 0);
LCD_DisplayNumber1(1, '0' + (((uint16_t)Temperature / 100 ) % 10), 0);
LCD_DisplayNumber1(2, '0' + (((uint16_t)Temperature / 10 ) % 10), 1);
LCD_DisplayNumber1(3, '0' + (((uint16_t)Temperature / 1 ) % 10), 0);
LCD_DisplayNumber2(0, '0' + (((uint16_t)Humidity / 1000) % 10), 0);
LCD_DisplayNumber2(1, '0' + (((uint16_t)Humidity / 100 ) % 10), 0);
LCD_DisplayNumber2(2, '0' + (((uint16_t)Humidity / 10 ) % 10), 1);
LCD_DisplayNumber2(3, '0' + (((uint16_t)Humidity / 1 ) % 10), 0);
MultiTimerStart(&SHT30_MultiTimer, 250, SHT30_MultiTimerCallback, "SHT30");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void SHT30_Init(void)
{
I2C_Handle.Instance = I2C2;
I2C_Handle.Init.I2C_Mode = I2C_MODE_MASTER;
I2C_Handle.Init.Tx_Auto_En = TX_AUTO_EN_ENABLE;
I2C_Handle.Init.Clock_Speed = CLOCK_SPEED_STANDARD;
I2C_Handle.Init.No_Stretch_Mode = NO_STRETCH_MODE_NOSTRETCH;
HAL_I2C_Init(&I2C_Handle);
SHT30_Reset(I2C_Handle);
MultiTimerStart(&SHT30_MultiTimer, 250, SHT30_MultiTimerCallback, "SHT30");
}
/*********************************END OF FILE**********************************/
LCD程序实现:
在之前显示数字的基础上添加了一个显示单位以及其它剩余符号的函数
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void LCD_DisplayUnit(uint8_t Index, uint8_t Enable)
{
char TAB[16][4] =
{
"S1 ", "S2 ", "S3 ", "S4 ", "S5 ", "S6 ", "S7 ", "S8 ",
"S9 ", "S10", "S11", "S12", "S13", "S14", "S15", "S16",
};
uint8_t COMn = 0xFF, SEGn = 0xFF;
LCD_SearchName(TAB[Index], &COMn, &SEGn);
if((COMn != 0xFF) && (SEGn != 0xFF))
{
LCD_WriteBit(COMn, SEGn, Enable);
}
}
运行结果:
软件工程:
|