[National Technology Automotive MCU N32A455 Development Board] 1. Development Board Test - Automotive Electronics
Continuing from the previous article, my homework requires the use of an LCD screen, so I will drive the LCD screen first.
【Screen and GPIO selection】
1. There are many screens on hand. A big guy wrote a stm7735 one before, so I "learned" from his post [National Technology Automotive MCU N32A455 Development Board] Evaluation - 4 - Zhongjingyuan TFT_LCD Transplantation + SPI2 Test - Evaluation Center Special Edition . Special thanks to @沛红恩. Mainly because I have too many things on hand recently.
2. GPIO selection, in order to prepare hardware SPI for driving later, considering the speed, I chose SPI1 as the interface. For specific pin selection, see the following macro definition code:
#define LCD_SPI SPI1
#define LCD_SPI_CLK RCC_APB2_PERIPH_SPI1
#define LCD_SPI_SCK_PIN GPIO_PIN_5 /* PA.05 */
#define LCD_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
#define LCD_SPI_SCK_GPIO_CLK RCC_APB2_PERIPH_GPIOA
#define LCD_SPI_MISO_PIN GPIO_PIN_6 /* PA.06 */
#define LCD_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
#define LCD_SPI_MISO_GPIO_CLK RCC_APB2_PERIPH_GPIOA
#define LCD_SPI_MOSI_PIN GPIO_PIN_7 /* PA.07 */
#define LCD_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
#define LCD_SPI_MOSI_GPIO_CLK RCC_APB2_PERIPH_GPIOA
#define LCD_CS_PIN GPIO_PIN_10 /* PE.10 */
#define LCD_CS_GPIO_PORT GPIOE /* GPIOE */
#define LCD_CS_GPIO_CLK RCC_APB2_PERIPH_GPIOE
#define LCD_RES_PIN GPIO_PIN_8 /* PE.8 */
#define LCD_RES_GPIO_PORT GPIOE /* GPIOE */
#define LCD_RES_GPIO_CLK RCC_APB2_PERIPH_GPIOE
#define LCD_BL_PIN GPIO_PIN_9 /* PE.9 */
#define LCD_BL_GPIO_PORT GPIOE /* GPIOE */
#define LCD_BL_GPIO_CLK RCC_APB2_PERIPH_GPIOE
3. In the screen driver, I used a mode that is compatible with software SPI and hardware SPI, and used the macro #define SPI_Soft to switch which mode to use.
4. GPIO initialization. Currently, I only use the output interface, so all 5 IOs are initialized to output 50MHz speed:
void LCD_GPIO_Init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
GPIO_InitStructure.Pin = LCD_CS_PIN | LCD_RES_PIN | LCD_BL_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
#ifdef SPI_Soft
GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.Pin = LCD_SPI_SCK_PIN | LCD_SPI_MISO_PIN | LCD_SPI_MOSI_PIN ;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
#else
RCC_EnableAPB2PeriphClk(LCD_SPI_CLK, ENABLE);
/*!< Configure sFLASH_SPI pins: SCK */
GPIO_InitStructure.Pin = LCD_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sFLASH_SPI pins: MOSI */
GPIO_InitStructure.Pin = LCD_SPI_MOSI_PIN;
GPIO_InitPeripheral(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sFLASH_SPI pins: MISO */
GPIO_InitStructure.Pin = LCD_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
#endif
}
Function to modify SPI output data at the same time:
/******************************************************************************
函数说明:LCD串行数据写入函数
入口数据:dat 要写入的串行数据
返回值: 无
******************************************************************************/
void LCD_Writ_Bus(u8 dat)
{
u8 i;
LCD_CS_Clr();
#ifdef SPI_Soft
for(i=0;i<8;i++)
{
LCD_SCLK_Clr();
if(dat&0x80)
{
LCD_MOSI_Set();
}
else
{
LCD_MOSI_Clr();
}
LCD_SCLK_Set();
dat<<=1;
}
#else
SPI_I2S_TransmitData(LCD_SPI, dat);
while (SPI_I2S_GetStatus(LCD_SPI, SPI_I2S_TE_FLAG) == RESET)
{;}
#endif
LCD_CS_Set();
}
In this way, the work of porting the code is completed. In fact, if it is a single display, you don’t need to initialize CS and BLK. CS is directly connected to ground and BLK is directly connected to VCC. In this way, 4 lines can be used for driving.
5. Create a font library. Open PCtoLCD and create a 24*24 font:
In the settings, select as shown below.
Finally, enter the required Chinese characters in the area shown in the figure below, generate the font template, and then copy the generated content into the font library.
Then write the test code in the main program:
#include "main.h"
#include "log.h"
#include "lcd_init.h"
#include "lcd.h"
int main(void)
{
log_init();
LCD_Init();//LCD初始化
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
LCD_ShowChinese(10,0,"国民技术",RED,WHITE,24,0);
LCD_ShowString(24,30,"N32A455",RED,WHITE,16,0);
while(1)
{
}
}
After downloading to the development board, the effect is as shown below:
But I haven't used SPI to drive the screen well yet, I'll try again later. Attached is the project source code.