[National Technology Low Power Series N32L43x Review] 05. SPI drive TFT screen & TF card to achieve Chinese and English mixed display
[Copy link]
The National N32L43X series MCU has two hardware SPI interfaces, and we can use one of them to drive the SPI interface TFT display; although this series of MCUs does not have an SDIO interface to operate the TF card, the TF card can work in SPI mode, so our other SPI can be used to drive the TF card and implement file reading operations by transplanting the FatFs file system.
The SPI interface of the N32L43X series MCU can work in master or slave mode, supports full-duplex and simplex high-speed communication modes, has hardware CRC calculation and can be configured in multi-master mode, and has the following features:
- Full-duplex and simplex synchronous transmission
- Support master mode, slave mode, multi-master mode
- 8-bit or 16-bit data frame format
- Data bit order programmable
- NSS management by software or hardware
- Programmable clock polarity and phase
- Sending and receiving support hardware CRC calculation and verification
- Support DMA operation
We use the SPI1 interface to drive the TF card and the SPI2 interface to drive the TFT display. The specific pin usage allocation is shown in the following table:
Multiplexing function
|
Pins
|
Remap
|
SPI1_NSS
|
PB6
|
AF4
|
SPI1_SCK
|
PB3
|
AF1
|
SPI1_MISO
|
PB4
|
AF1
|
SPI1_MOSI
|
PB5
|
AF0
|
Multiplexing function
|
Pins
|
Remap
|
SPI2_NSS
|
PB12
|
AF0
|
SPI2_SCK
|
PB13
|
AF0
|
SPI2_MISO
|
PB14
|
AF0
|
SPI2_MOSI
|
PB15
|
AF0
|
Implementing functions
The TF card is driven by the SPI1 interface of the N32L43X series MCU. The data reading operation of the font file GBK_FONT.bin stored in the TF card is realized by transplanting the FatFs file system. The TFT display is driven by the SPI2 interface. The graphic data is extracted by combining the Image2Lcd tool to display the graphics on the TFT display. The English font dot matrix data stored in the MCU FLASH and the GBK Chinese font dot matrix data stored in the TF card are used to realize the mixed display of Chinese and English on the TFT. The main implementation code is as follows:
Read GBK Chinese character font dot matrix data
void FatFs_GetGBK(const char *str, uint8_t *Buffer)
{
FIL File;
FRESULT Result;
UINT br = 0;
uint8_t GBKH = str[0];
uint8_t GBKL = str[1];
uint32_t Offset = 0;
if(GBKL < 0x7F) Offset = ((GBKH - 0x81) * 190 + GBKL - 0x40);
else Offset = ((GBKH - 0x81) * 190 + GBKL - 0x41);
Result = f_open(&File, "/GBK_FONT.bin", FA_OPEN_ALWAYS | FA_READ);
if(Result != RES_OK)
{
printf("\r\nf_open Fail! Result = %d\r\n", Result);
}
else
{
f_lseek(&File, Offset * 32);
Result = f_read(&File, Buffer, 32, &br);
f_close(&File);
}
}
Show image
void LCD_DrawImage(void)
{
uint16_t Color = 0;
uint32_t Index = 8;
for(uint32_t i = 0; i < 30; i++)
{
for(uint32_t j = 0; j < 113; j++)
{
Color = gImage_LOGO[Index++];
Color <<= 8;
Color |= gImage_LOGO[Index++];
TFT_DrawPoint(64 + j, 30 + i, Color);
}
}
Index = 8;
for(uint32_t i = 0; i < 40; i++)
{
for(uint32_t j = 0; j < 135; j++)
{
Color = gImage_EEWORLD[Index++];
Color <<= 8;
Color |= gImage_EEWORLD[Index++];
TFT_DrawPoint(52 + j, 100 + i, Color);
}
}
}
Display Chinese and English
void TFT_DrawEN(uint16_t StartX, uint16_t StartY, char ch)
{
uint8_t Data = 0;
uint16_t Color = 0;
for(uint8_t i = 0; i < 16; i++)
{
Data = ASCII_1608[ch - 32][i];
for(uint8_t j = 0; j < 8; j++)
{
if((Data >> j) & 0x01) Color = TFT_Forecolor;
else Color = BACKCOLOR;
TFT_DrawPoint(StartX + j, StartY + i, Color);
}
}
}
void TFT_DrawCN(uint16_t StartX, uint16_t StartY, const char *str)
{
uint8_t Buffer[32];
uint8_t Array[16][16];
uint8_t Point[16][16];
FatFs_GetGBK(str, Buffer);
for(uint8_t i = 0; i < 8; i++)
{
for(uint8_t j = 0; j < 4; j++)
{
uint8_t Data = Buffer[i*4+j];
for(uint8_t k = 0; k < 4; k++)
{
if(Data & (0x08 >> (k-0))) Array[i*2+0][j*4+k-0] = 1;
else Array[i*2+0][j*4+k-0] = 0;
}
for(uint8_t k = 4; k < 8; k++)
{
if(Data & (0x80 >> (k-4))) Array[i*2+1][j*4+k-4] = 1;
else Array[i*2+1][j*4+k-4] = 0;
}
}
}
for(uint8_t i = 0; i < 16; i++)
{
for(uint8_t j = 0; j < 16; j++)
{
Point[i][j] = Array[j][15-i];
}
}
for(uint8_t i = 0; i < 16; i++)
{
for(uint8_t j = 0; j < 16; j++)
{
if(Point[15-i][j])
{
TFT_DrawPoint(StartX+i, StartY+j, TFT_Forecolor);
}
else
{
TFT_DrawPoint(StartX+i, StartY+j, BACKCOLOR);
}
}
}
}
void TFT_ShowLOG(uint16_t StartX, uint16_t StartY, const char *str)
{
while(*str != '\0')
{
if(*str < 0x7F)
{
if(StartX > (240 - 8))
{
StartX = 0; StartY += 16;
}
if(StartY > (320 - 16))
{
StartX = 0; StartY = 0;
TFT_ClearScreen(BACKCOLOR);
}
TFT_DrawEN(StartX, StartY, *str);
StartX += 0x08;
str += 0x01;
}
else
{
if(StartX > (240 - 16))
{
StartX = 0; StartY += 16;
}
if(StartY > (320 - 16))
{
StartX = 0; StartY = 0;
TFT_ClearScreen(BACKCOLOR);
}
TFT_DrawCN(StartX, StartY, str);
StartX += 0x10;
str += 0x02;
}
}
}
For other codes such as SPI configuration, TFT display driver and configuration, TF card read and write operations, FATFS file system transplantation, etc., please refer to the software engineering source code in the attachment.
Operation effect
appendix
Software engineering source code:
Template_TFT.zip
(2.54 MB, downloads: 35)
|