用DHT11,显示屏幕为oled,用keil开发,请提供相关代码
时间: 2024-02-01 21:13:55 浏览: 74
keil的相关代码
以下是使用DHT11传感器和OLED显示屏的Keil代码示例:
```
#include <stdio.h>
#include <string.h>
#include "stm32f1xx_hal.h"
#include "ssd1306.h"
#include "fonts.h"
#define DHT11_PIN GPIO_PIN_5
#define DHT11_PORT GPIOA
static void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
void Delay_us(uint32_t us)
{
uint32_t ticks = us * (SystemCoreClock / 1000000U);
while (ticks--);
}
void DHT11_Start(void)
{
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET);
Delay_us(18000);
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_SET);
Delay_us(40);
}
uint8_t DHT11_Check_Response(void)
{
uint8_t response = 0;
uint16_t timeout = 0xFFFF;
while (!response && timeout--)
{
if (!HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN))
{
response = 1;
while (!HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN));
}
}
return response;
}
uint8_t DHT11_Read_Byte(void)
{
uint8_t byte = 0;
for (uint8_t i = 0; i < 8; i++)
{
byte <<= 1;
while (HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN));
Delay_us(30);
if (HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN))
{
byte |= 1;
}
while (!HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN));
}
return byte;
}
void DHT11_Read_Data(uint8_t *humidity, uint8_t *temperature)
{
uint8_t data[5];
memset(data, 0, sizeof(data));
DHT11_Start();
if (DHT11_Check_Response())
{
for (uint8_t i = 0; i < 5; i++)
{
data[i] = DHT11_Read_Byte();
}
if ((data[0] + data[1] + data[2] + data[3]) == data[4])
{
*humidity = data[0];
*temperature = data[2];
}
}
}
I2C_HandleTypeDef hi2c1;
void OLED_Init(void)
{
HAL_Delay(100);
ssd1306_Init(&hi2c1);
ssd1306_Fill(Black);
ssd1306_UpdateScreen(&hi2c1);
}
void OLED_Show_String(uint8_t x, uint8_t y, char *str)
{
ssd1306_SetCursor(x, y);
ssd1306_WriteString(str, Font_7x10, White);
ssd1306_UpdateScreen(&hi2c1);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
OLED_Init();
uint8_t humidity, temperature;
while (1)
{
DHT11_Read_Data(&humidity, &temperature);
char str[20];
sprintf(str, "Humidity: %d%%", humidity);
OLED_Show_String(0, 0, str);
sprintf(str, "Temp: %dC", temperature);
OLED_Show_String(0, 15, str);
HAL_Delay(5000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
__HAL_RCC_USART1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_HandleTypeDef huart1;
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = USART_WORDLENGTH_8B;
huart1.Init.StopBits = USART_STOPBITS_1;
huart1.Init.Parity = USART_PARITY_NONE;
huart1.Init.Mode = USART_MODE_TX_RX;
huart1.Init.HwFlowCtl = USART_HWCONTROL_NONE;
huart1.Init.OverSampling = USART_OVERSAMPLING_16;
if (HAL_USART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStruct);
}
```
请注意,此代码示例仅供参考。 您需要根据自己的硬件和需求进行修改。
阅读全文