STM32CubeIDE配置+F407ZGT6开发板+2.8寸ILI9341+DHT11
本篇使用STM32F407ZGT6开发板和配套直插式2.8寸ILI9341液晶屏作为硬件环境,过程使用STM32CubeIDE配置RTC、FSMC等,实现时间显示和温湿度监测显示
STM32CubeIDE配置部分
RCC配置
SYS配置
·RTC配置
·时钟树配置
·TIM7部分实现微秒延时
·FSMC配置 请根据各自开发板原理图调整
·USART1配置
·这边选择设置生成.C和.H文件,按CRTL+S保存配置,自动生成
驱动部分:
参考了很多资料,由于卖家给的配件资料,不是最新的HAL库,不适合使用STM32CubeIDE做开发,在写代码的时候遇到了很多问题,现在都已经解决。
由于驱动太长,这里直接发网盘链接
中文字库:链接:https://pan.baidu.com/s/1-IMHDbhSTS3-DhWNPcCkQg
提取码:3n9o
2.8寸液晶屏驱动:链接:https://pan.baidu.com/s/1SwL6XoQcgixf5sqftBZRfg
提取码:win5
汉字显示 驱动代码:链接:https://pan.baidu.com/s/1j0rKi_o8xhCZZwKCXb-NmA
提取码:1ora
dht11:链接:https://pan.baidu.com/s/1gKA4zDHqIedrk9gdAG6p3w
提取码:z7c3
dht11的数据引脚接到了PA3
代码部分:
需要注意的是,由于卖家给的资料比较老,所以要在stm32f4xx_hal.h中添加定义,也可以在main.h(建议)中给定位置添加定义,这样不容易造成更改STM32Cube配置重新生成后,导致stm32f4xx_hal.h中添加的定义被刷掉。这边使用的是stm32f4xx_hal.h中添加,不想改驱动包含的.H文件,主要是懒。
直接上定义:
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s8;
typedef const int32_t sc32; /*!< Read Only */
typedef const int16_t sc16; /*!< Read Only */
typedef const int8_t sc8; /*!< Read Only */
typedef __IO int32_t vs32;
typedef __IO int16_t vs16;
typedef __IO int8_t vs8;
typedef __I int32_t vsc32; /*!< Read Only */
typedef __I int16_t vsc16; /*!< Read Only */
typedef __I int8_t vsc8; /*!< Read Only */
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef const uint32_t uc32; /*!< Read Only */
typedef const uint16_t uc16; /*!< Read Only */
typedef const uint8_t uc8; /*!< Read Only */
typedef __IO uint32_t vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t vu8;
typedef __I uint32_t vuc32; /*!< Read Only */
typedef __I uint16_t vuc16; /*!< Read Only */
typedef __I uint8_t vuc8; /*!< Read Only */
微秒延时使用TIM7
在tim.c,最下面的BEGIN1中添加如下代码
/* USER CODE BEGIN 1 */
void bsp_delay_us(uint16_t us)
{
uint16_t differ=0xffff-us-5;
HAL_TIM_Base_Start(&htim7);
__HAL_TIM_SetCounter(&htim7,differ);
while(differ < 0xffff-5)
{
differ = __HAL_TIM_GetCounter(&htim7);
}
HAL_TIM_Base_Stop(&htim7);
}
/* USER CODE END 1 */
在tim.h中声明
/* USER CODE BEGIN Prototypes */
void bsp_delay_us(uint16_t us);
/* USER CODE END Prototypes */
printf的重定向
在usart.c 最下面的BEGIN1中添加
/* USER CODE BEGIN 1 */
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);
return ch;
}
/* USER CODE END 1 */
·main.c主函数部分
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "dht11.h"
#include "lcd.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
DHT11_Data_TypeDef DHT11_Data;
u8 lcd_id[12]; //存放LCD ID字符�???
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
uint8_t u8DateTime[50];
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
LCD_Init();
POINT_COLOR=BLACK;
sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//使用串口接收温湿度数据
/*if (DHT11_ReadData(&DHT11_Data))
{
//printf("DHT11 Read the success\r\n");
printf("humidity: %d.%d,temperature: %d.%d\r\n",DHT11_Data.humi_int,DHT11_Data.humi_deci,DHT11_Data.temp_int,DHT11_Data.temp_deci);
}
else
{
printf("Read DHT11 ERROR!\r\n");
}
HAL_Delay(3000);*/
//使用液晶屏显示
DHT11_ReadData(&DHT11_Data);//读取DHT11数据
//RTC读取日期时间
/* Get the RTC current Time ,must get time first*/
HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN);
/* Display date Format : yy/mm/dd */
//组合日期时间
sprintf((char*)u8DateTime,"20%02d-%02d-%02d %02d:%02d:%02d\r\n",
sdatestructure.Year,sdatestructure.Month,sdatestructure.Date,
stimestructure.Hours,stimestructure.Minutes,stimestructure.Seconds);
//printf("%s\r\n",u8DateTime);//用串口输出日期时间
POINT_COLOR=BLACK;//设置字体颜色
//液晶屏输出
Draw_Font24B(10,20,MAGENTA,"温 湿 度 监 测");
LCD_ShowString(7,70,220,24,24,u8DateTime);
//Draw_Font24B(5, 150, BLACK, "温 度");
LCD_ShowString(5,150,210,24,24,"Temperature:");
LCD_ShowNum(160,150,DHT11_Data.temp_int,2,24);
LCD_ShowString(185,150,210,24,24,".");
LCD_ShowNum(190,150,DHT11_Data.temp_deci,1,24);
Draw_Font24B(200,152,BLACK,"℃");
//Draw_Font24B(5, 200, BLACK, "湿 度");
LCD_ShowString(5,200,210,24,24,"Humidity:");
LCD_ShowNum(160,200,DHT11_Data.humi_int,2,24);
LCD_ShowString(185,200,210,24,24,".");
LCD_ShowNum(190,200,DHT11_Data.humi_deci,1,24);
LCD_ShowString(208,200,210,24,24,"%");
HAL_Delay(1000);
}
/* USER CODE END 3 */
最终效果:
源码下载:
https://download.csdn.net/download/cj0106/12582855
CSDN下载不了的话,请私信我。