FATFS is a small file system designed specifically for embedded systems, featuring the following characteristics:
- 1. Cross-platform: Can be used on various microcontrollers
- 2. Small code size: The core code is about 13KB
- 3. Supports multiple storage media: SD cards, NAND Flash, etc.
- 4. Supports long file names
- 5. Supports multiple partitions
In simple terms, FATFS acts like a “mini Windows Explorer” for microcontrollers, allowing us to create folders and copy/paste files just like on a computer.
Hardware Preparation
For this experiment, we will use:
- • Main control: STM32F103ZET6
- • Storage: CS Chuangshi SD NAND (4GB capacity)
SD NAND is a chip-type SD card that is more stable than ordinary SD cards and cheaper than eMMC. The wiring is very simple, requiring only four wires:
STM32 <-> SD NAND
PC11 <-> CS (Chip Select)
PC12 <-> SCLK (Clock)
PD2 <-> MOSI (Master Out)
PC8 <-> MISO (Master In)
Note: The power supply voltage for SD NAND is 3.3V; do not connect it to 5V, or the chip will be damaged.
Driver Program Development
Although SD NAND is relatively advanced, it is fully compatible with the standard SD card protocol. Therefore, we only need to write the driver according to the SD card method. Here we use SPI mode to communicate with SD NAND, which mainly includes the following functions:
- 1. Low-level SPI transmission and reception function
u8 SDCardReadWriteOneByte(u8 DataTx){u8 i; u8 data = 0; for(i=0; i<8; i++){SDCARD_SCK = 0; if(DataTx & 0x80) SDCARD_MOSI = 1; else SDCARD_MOSI = 0; SDCARD_SCK = 1; DataTx <<= 1; data <<= 1; if(SDCARD_MISO) data |= 0x01;} return data;}
- 2. SD card initialization function
u8 SDCardDeviceInit(void){u8 r1; u16 retry; u8 buf[4];
SDCardSpiInit(); // Initialize SPI interface
// Send at least 74 clock pulses
for(retry=0; retry<10; retry++) SDCardReadWriteOneByte(0xFF);
// Enter IDLE state
do{
r1 = SendSDCardCmd(SDCard_CMD0, 0, 0x95);
}while(r1 != 0x01);
// Identify card type
if(SendSDCardCmd(SDCard_CMD8, 0x1AA, 0x87) == 1)
{
// V2.0 card
//... omitted detailed identification process...
}
// Initialize card
do{
SendSDCardCmd(SDCard_CMD55, 0, 0x01);
r1 = SendSDCardCmd(SDCard_CMD41, 0x40000000, 0x01);
}while(r1);
// Identify card capacity
if(SendSDCardCmd(SDCard_CMD58, 0, 0x01) == 0)
{
//... omitted capacity identification process...
}
return 0; // Initialization successful
}
- 3. Read and write sector function
u8 SDCardReadData(u8* buf, u32 sector, u8 cnt){ // Send read command if(SendSDCardCmd(SDCard_CMD17, sector, 0x01) == 0){ // Wait for data packet header if(SDCardGetAck(0xFE) == 0){ // Loop to read data for(u16 i=0; i<512; i++){*buf = SDCardReadWriteOneByte(0xFF); buf++;} // Ignore CRC SDCardReadWriteOneByte(0xFF); SDCardReadWriteOneByte(0xFF); return 0; // Read successful } } return 1; // Read failed } u8 SDCardWriteData(u8* buf, u32 sector, u8 cnt) { // Send write command if(SendSDCardCmd(SDCard_CMD24, sector, 0x01) == 0) { // Send data header SDCardReadWriteOneByte(0xFE);
}
Microcontroller LCD Touch Screen Driver Technology
LCD touch screens are commonly used human-machine interfaces in microcontroller projects, and mastering their driver technology is crucial for embedded system development. This article will introduce how to drive an LCD touch screen using a microcontroller, including hardware connections, communication protocols, initialization configurations, and other key knowledge points.
Basic Knowledge of LCD Screens
LCD (Liquid Crystal Display) screens mainly consist of a liquid crystal panel, backlight source, and driver IC. Common types of LCD screens include:
- • TFT-LCD: Thin Film Transistor Liquid Crystal Display, rich in color
- • OLED: Organic Light Emitting Diode display, high contrast
- • e-ink: Electronic ink screen, low power consumption
For microcontroller applications, we mainly use TFT-LCD, which has advantages such as moderate cost and high resolution.
Hardware Connection
Taking the common ILI9341 controller as an example, the connection between the LCD screen and the microcontroller mainly has the following methods:
- 1. Parallel interface (8080 interface):
- • Data lines: DB0~DB15 (8-bit or 16-bit)
- 2. SPI interface:
- • Control lines: CS, RS (DC), WR, RD
- 3. FSMC interface (specific to STM32):
- • DC (Data/Command Select)
- • Control lines: NE, NWE, NOE
Note: The choice of interface method depends on the microcontroller’s resources and performance requirements. The parallel interface is fast but occupies many IOs, while the SPI interface saves IOs but is slower.
Communication Protocol
Regardless of which interface is used, communication with the LCD controller follows a similar protocol:
- 1. Write command:
CS low -> RS low -> Write command -> CS high
- 2. Write data:
CS low -> RS high -> Write data -> CS high
- 3. Read data:
CS low -> RS high -> Read data -> CS high
Taking the 8080 parallel interface as an example, we can write basic read and write functions:
void LCD_WriteCmd(uint8_t cmd)
{
LCD_RS_CLR; // RS = 0, indicating write command
LCD_CS_CLR; // Chip select
LCD_WR_CLR; // Write enable
LCD_DataOut(cmd); // Output command
LCD_WR_SET; // Rising edge latches data
LCD_CS_SET; // Release chip select
}
void LCD_WriteData(uint16_t data)
{
LCD_RS_SET; // RS = 1, indicating write data
LCD_CS_CLR;
LCD_WR_CLR;
LCD_DataOut(data);
LCD_WR_SET;
LCD_CS_SET;
}
Initialization Configuration
LCD initialization mainly includes the following steps:
- 1. Reset
- 2. Exit sleep mode
- 3. Configure display direction, color mode, and other parameters
- 4. Clear screen
- 5. Turn on display