实现LM401模组(stm32wle5)基于SPI接口驱动墨水屏显示字符
利用 SPI1 PA5 6 7接口
MCU:【易智联 LM401-pro-kit】
墨水屏:2.66 英寸 微雪电子墨水屏
代码移植参考官方,【基于STM32实现】,由于官方使用的是 STM32F103ZET6
,易智联模组是 STM32WLe5
,进行代码移植
用户可以可使用两路 SPI
最高频率16 Mbit/s
,其中一路支持 I2S
其中内部射频模块已经使用了一路SPI 和 MCU (Arm Cortex-M4)
通讯,如图所示。SPI1 和 SPI2S2 实例是通用型,而 SUBGHZSPI 实例专用于
专门用于 Sub-GHz 无线电控制。
引脚 | 描述 |
---|---|
PB3 PA1 PA5 | SPI1_SCK |
PB4 PA6 | SPI1_MISO |
PB5 PA7 | SPI1_MOSI |
PA4 PB2 PA15 | SPI1_NSS |
PB9 PA9 PB12 | SPI2_NSS |
PC1 PA10 PA12 PB15 | SPI2_MOSI |
PC2 PA5 PB14 PA11 | SPI2_MISO |
PA8 PA9 PB10 PB13 | SPI2_SCK |
驱动代码库,参考: 【STM32WLxx_HAL_Driver】
stm32wlxx_hal_conf.h
配置文件中,使能 SPI 模块 ,#define HAL_SPI_MODULE_ENABLED
stm32wlxx_hal_spi.h
stm32wlxx_hal_spi.c
在 STM32Cube IDE 中进行 SPI 引脚配置:
其他四个引脚配置,一个输入,三个输出
配置完成后,自动生成代码,会生成以下几个文件:
gpio.c
gpio.h
mian.h
: GPIO 口的初始化
spi.c
spi.h
stm32wlxx_hal_spi.h
stm32wlxx_hal_spi.c
:SPI接口的初始化
其中 gpio.c
文件会生成配置的普通IO口 spi.c
文件会生成你在 IDE可视化配置的 SPI 引脚,其中SPI.c
文件如下:
#include "spi.h"
void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
/* SPI1 clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
PA11 ------> SPI1_MISO
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspDeInit 0 */
/* USER CODE END SPI1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI1_CLK_DISABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
PA11 ------> SPI1_MISO
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_11);
/* USER CODE BEGIN SPI1_MspDeInit 1 */
/* USER CODE END SPI1_MspDeInit 1 */
}
}
下载官方代码移植: 程序下载
将 user 目录底下的文件拷贝到你的工程下
在 工程中添加 include路径
E-Paper API解析
剩下的就是一些细节的修改,比如 lnclude stm32fxx
改为 stm32wlxx
,这里不做赘述,
//Create a new image cache
UBYTE *BlackImage;
/* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */
UWORD Imagesize = ((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1)) * EPD_2IN66_HEIGHT;
if((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {
APP_PPRINTF("Failed to apply for black memory...\r\n");
return -1;
}
//APP_PPRINTF("Paint_NewImage\r\n");
Paint_NewImage(BlackImage, EPD_2IN66_WIDTH, EPD_2IN66_HEIGHT, 270, WHITE);
Paint_SelectImage(BlackImage);
Paint_Clear(WHITE);
Paint_DrawString_EN(10, 0, "xxxxAIoT", &Font24, BLACK, WHITE);
//APP_PPRINTF("Drawing:BlackImage\r\n");
Paint_DrawString_EN(10, 50, "LM401 E-Paper Test by xxxx", &Font12, WHITE, BLACK);
Paint_DrawString_EN(10, 70, "temperature:", &Font12, WHITE, BLACK);
Paint_DrawNum( 100, 70, epaperTemperature, &Font12, BLACK, WHITE);
Paint_DrawString_EN(10, 90, "humility:", &Font12, WHITE, BLACK);
Paint_DrawNum(80, 90, (epaperTemperature/2+7 ), &Font12,BLACK , WHITE);
EPD_2IN66_Display(BlackImage);
DEV_Delay_ms(4000);
APP_PPRINTF("Goto Sleep...\r\n");
EPD_2IN66_Sleep();
free(BlackImage);
BlackImage = NULL;
// close 5V
APP_PPRINTF("close 5V, Module enters 0 power consumption ...\r\n");
DEV_Module_Exit();
描述:动态采集温度值,设置 30s 刷新一次数据。
完整源码,请下载:
https://download.csdn.net/download/qq_39217004/87625828