
简介这是基于STM32F103C8T6最小系统板驱动ILI9341 2.8寸TFT液晶屏的完整Keil工程面向入门STM32、需要快速完成屏幕显示的嵌入式开发者和学生。由于C8T6是48脚封装、没有FSMC控制器工程没有使用串行屏模块而是用GPIO模拟16位并口时序把A端口0~15作为数据线再配合读写控制脚完成LCD初始化、画点、画矩形、填充和颜色显示等基础函数触摸输入暂未实现但利于集中学习液晶屏驱动原理和GPIO时序。压缩包共182个文件约3.16MB包含C源码、H头文件、启动汇编s以及uvproj/uvopt工程文件、编译链接生成的o、lst、hex、map等.s为启动文件c/h为驱动与应用代码hex可直接烧录目录结构清晰适合直接打开编译和烧录到开发板验证。已有5078人学习下载。对想要理解STM32模拟并口驱动LCD、移植ILI9341底层初始化代码的读者这套资源提供了完整可跑的工程
参考也可据此排查并口接线和时序问题。 不知道你现在手里是不是也有一块吃灰的2.8寸TFT LCD液晶屏。当年我第一块ILI9341屏幕就是这么来的——买了STM32F103C8T6最小系统板顺手加了一块2.8寸TFT LCD觉得不过就是个显示屏嘛点亮还不是分分钟的事。结果连续折腾了三个晚上白屏、花屏、斜条纹全都遇了一遍最后才搞明白问题出在哪。做STM32F103C8T6驱动ILI9341这件事本身并不复杂难的是网上教程默认你“该懂但其实不懂”的细节太多。本文把我从SPI引脚映射、ILI9341初始化序列、窗口设置到刷屏性能优化和白屏排错的实际经验完整写出来适合刚用C8T6点亮液晶屏、对寄存器一头雾水的朋友。不用再翻几百页数据手册也能跑通但你会在读完之后知道每一条命令到底在干什么。1. 这套经典组合的选型逻辑C8T6为什么要配ILI93411.1 一个48脚小板和一块屏的典型搭档STM32F
103C8T6属于F1系列里的“最小系统板之王”Cortex-M3内核72MHz主频64KB Flash20KB SRAMLQFP48封装。虽然引出的GPIO不算多但SPI、USART、I2C这些常用外设一个不缺。ILI9341则是一颗极其常见的TFT LCD驱动芯片最大支持240x320分辨率16位色RGB565下色彩表现足够细腻喜欢折腾的还可以支持到18位色。这两者组合起来刚好是一套“资源不多但正好够用”的经典搭配——C8T6的SPI1跑在18~36MHzILI9341的SPI接口完全吃得下引脚占用少驱动代码量也小特别适合作为入门的第一块屏幕。我见过不少朋友一上来就纠结“要不要换个大屏、要不要上F407”。如果只是学显示逻辑、做仪表盘或者信息展示C8T6加ILI9341的绰绰有余。等到之后需要复杂图形渲染或视频播放再考虑换平台也不迟。1.2 硬件清单与模块选型的坑我这边准备的东西非常常
规STM32F103C8T6最小系统板一块蓝色pill板最常见2.8寸ILI9341 TFT LCD模块一个ST-Link V2下载器一个杜邦线若干3.3V和5V供电都要备好有的屏模块背光部分需要5V这里有一个非常容易踩的坑同样是2.8寸屏接口可能完全不同。市面上常见两种一种是16位8080并口排针一大排需要十几个GPIO才能驱动另一种是SPI接口几根线就能控制。C8T6引脚资源本身不富裕如果你拿到的是8080并口版本同时还要外接其他传感器引脚会非常紧张。所以买之前一定要问清楚或者看原理图确认是SPI接口。另外部分模块会带触摸功能XPT2046芯片触摸部分又额外占两个引脚。如果只是做显示可以忽略触摸相关引脚但要注意有些模块的触摸IC和屏幕共用供电与复位线上电后不初始化触摸IC不影响屏幕显示这部分可以不接。2. 先把ILI9341当一块可寻址的画板理解命令与GRAM机制2.1 驱动芯片就是屏
幕自带的大脑很多教程上来就扔给你一大段初始化数组然后说“照着写就行”。这确实能跑通但一旦遇到白屏、花屏、颜色不对你完全不知道从哪排查。所以我建议先从原理层搞清楚ILI9341到底是个什么东西。ILI9341内部有一块240x320的GRAM每一个地址对应屏幕上的一个物理像素点。它就像一个自带画板的画师MCU不需要逐一点亮每个像素只需要通过SPI告诉他三件事第一从哪个位置开始画第二画多大一块区域第三往这块区域里填充什么颜色数据。剩下的像素排列工作全部由ILI9341自己完成。所以驱动ILI9341的核心其实是和它打交道发送一串配置命令让它进入你想要的显示模式然后不断往GRAM里填数据。2.2 DC引脚决定了SPI线上走的是命令还是数据SPI是一种四线通信协议但对ILI9341来说还需要额外一个DC引脚也叫RS、D/C用来区分当前字节是“命令”还是“数据”。这是整个驱动中最容易忽略却最关键的信
号。DC拉低SPI上的字节被当作命令比如0x2A代表“设置列地址”。DC拉高SPI上的字节被当作数据比如列地址的具体数值。如果没有这个DC引脚屏幕就无法区分进来的是指令还是像素数据。实际代码里写命令和写数据就是两个长得几乎一样的函数唯一的区别就是DC引脚的高低电平时序。2.3 初始化序列几条关键命令的含义初始化序列是很多新手“劝退”的地方一长串寄存器配置看着就像天书。其实大部分命令用不到能影响显示效果的核心命令就那么几个命令名称关键参数说明0x01Software Reset软件复位必须延时等待0x11Sleep Out退出睡眠模式必须延时等待0x36Memory Access Control控制扫描方向、RGB顺序0x3APixel Format Set像素格式0x55表示RGB565 16bit0x29Display ON打开显示其中0x36最常被单独拎出来讲。它的参数决定屏幕的横竖方
向和颜色顺序比如0x00通常是竖屏、0x60或0xA0是横屏bit3置1表示BGR颜色顺序。不同模块的默认扫描方向不一样这也是同一个初始化代码在不同模块上出现“上下颠倒、左右镜像”的原因。2.4 写一个像素的最小流程设置窗口加填色任何显示操作到最后都会归结为一个动作往GRAM写颜色数据。而写数据前必须先用三件事框定范围——列地址、页地址、写内存命令。列地址范围0x2A命令 4字节数据起始列高8位、低8位结束列高8位、低8位 页地址范围0x2B命令 4字节数据起始行高8位、低8位结束行高8位、低8位 写GRAM0x2C命令之后连续送入像素数据只要理解了这套流程你就能自己扩展出画点、画线、填充矩形、显示图片这些功能。所谓“画点”不过是连续执行两次设置窗口加上一次写像素而“全屏填充”只是把窗口设为整个屏幕然后连续发送十几万字节的颜色数据。3. 引脚连接与上电前检查照着接就能点亮3.1 我用的
这套引脚分配C8T6上自带SPI1外设这是最方便的选择因为SPI1的引脚可以映射到标准位置PA5是SCKPA6是MISOPA7是MOSI。我实际使用的引脚映射如下功能MCU引脚备注屏幕SCKPA5SPI1_SCK屏幕MOSIPA7SPI1_MOSI屏幕MISOPA6SPI1_MISO只读寄存器时用可不接屏幕CSPA4软件控制片选屏幕DCPA3命令/数据选择屏幕RSTPA2屏幕硬件复位背光BL3.3V或PWM引脚直接接高电平点亮VCC3.3V有的模块内部有LDO可接5V看清标注CS、DC、RST这三个引脚只要不和外设冲突可以随便换。SCK/MOSI则要用SPI1对应的引脚如果你想用SPI2那就要换到PB13和PB15了。3.2 上电前必须确认的三件事第一件事电平匹配。ILI9341的逻辑电压一般是2.8V到3.3VC8T6的IO输出也是3.3V理论上直连没问题。但有些模块为了兼容Arduin
o板上带了电平转换或者LDO这时候供电电压反而要看模块标注——有的VCC必须接5V才能让内部稳压器工作接3.3V会出现“背光亮但屏幕无内容”的假死状态。第二件事共地。MCU和屏幕模块的GND必须连在一起这是最基础但很容易漏掉的一步。我曾经因为接了一根不太牢靠的杜邦线导致GND接触不良屏幕时好时坏排查了半天最后发现只是地线松了。第三件事背光默认不一定亮。有的模块背光引脚上默认有上拉电阻上电就亮有的必须你手动拉高。调驱动之前先把BL引脚接3.3V确保背光能亮否则你会误以为是屏幕坏了实际上只是没开背光。3.3 为什么我坚持让你先飞线而不是直接画PCB如果你只是想把屏幕点亮跑通代码我强烈建议先用杜邦线连接而不是一上来就打板。C8T6驱动ILI9341的SPI时钟一般在18MHz左右这个频率下杜邦线勉强够用但线长超过20厘米就容易出现波形畸变导致花屏。飞线的好处是你随时可以缩短线长、重新分配引脚、用
示波器夹在线上量波形。等程序完全跑通、确认引脚分配合理之后再画PCB才是稳妥的流程。4. 代码落地从SPI配置到第一个像素的完整链路4.1 SPI外设初始化我用的是STM32标准外设库HAL库的配置思路完全一样只是API名称不同。核心是把SPI1配置为主模式、8位数据、MSB先行、SPI Mode 0CPOL0CPHA0时钟分频选择4分频也就是18MHz。SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode SPI_Mode_Master; SPI_InitStructure.SPI_DataSize SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL
SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit SPI_FirstBit_MSB; SPI_Init(SPI1, SPI_InitStructure); SPI_Cmd(SPI1, ENABLE);关于分频系数这里多说一句SPI1挂在APB2总线上时钟72MHz2分频就是36MHz这在理论上已经达到ILI9341 SPI接口的上限附近。但实际用杜邦线连接时36MHz很容易出现信号反射和串扰导致花屏。我调试时先降到18MHz4分频把功能跑通确定没问
题后再慢慢往上升这也是一个稳妥的调试思路。4.2 写命令与写数据函数的时序拆分底层最重要的两个函数就是写命令和写数据。实现上就是在发送字节之前控制DC引脚的电平。void ILI9341_WriteCmd(uint8_t cmd) { LCD_DC_LOW(); // 命令模式 LCD_CS_LOW(); // 片选拉低选中屏幕 SPI1_SendByte(cmd); LCD_CS_HIGH(); } void ILI9341_WriteData(uint8_t dat) { LCD_DC_HIGH(); // 数据模式 LCD_CS_LOW(); SPI1_SendByte(dat); LCD_CS_HIGH(); }注意DC信号必须要在片选拉低之前稳定下来否则屏幕可能把第一个字节误判成错误类型。这个时序问题比较隐蔽——初始化序列对时序不敏感时偶尔能跑通但显示大量像素数据时就会偶发花屏。
4.3 设置窗口与填充矩形理解了2.4节的流程后代码就非常直接了。设置窗口的函数负责把列地址和行地址都告诉屏幕void ILI9341_SetWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { ILI9341_WriteCmd(0x2A); ILI9341_WriteData(x0 8); ILI9341_WriteData(x0 0xFF); ILI9341_WriteData(x1 8); ILI9341_WriteData(x1 0xFF); ILI9341_WriteCmd(0x2B); ILI9341_WriteData(y0 8); ILI9341_WriteData(y0 0xFF); ILI9341_WriteData(y1 8); ILI9341_WriteData(y1 0xFF);
ILI9341_WriteCmd(0x2C); }填充矩形时先把窗口设好然后连续发送颜色数据。因为颜色是16位的RGB565所以每个像素需要发送两个字节。void ILI9341_FillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color) { uint32_t i; uint32_t total (uint32_t)w * h; ILI9341_SetWindow(x0, y0, x0 w - 1, y0 h - 1); LCD_DC_HIGH(); LCD_CS_LOW(); for (i 0; i total; i) { SPI1_SendByte(color 8); SPI1_SendByte(color 0xFF); } LCD_CS_HIGH(); }为什么窗口设置这么重要因为
ILI9341的GRAM写入指针是连续递增的。如果屏幕当前扫描方向是“从左到右、从上到下”那么写完一行后指针自动跳到下一行开头。你不设置窗口它就会按默认顺序一直写当你只想在屏幕中间画一个框时数据就会错位显示出来的就是一整屏乱码或斜条纹。花屏问题里有相当比例都出在窗口没设对上。4.4 显示字符字库本质上是像素点阵数据显示字符和ILI9341本身没有关系它只是把字符当成一张小图来填充。ASCII字库本质上就是一个二维数组数组里每个元素是0或11代表该像素点亮0代表熄灭。以16x24的ASCII字库为例每个字符占用24个字节每行2字节共24行。显示字符时遍历这个二维数组遇到1就填前景色遇到0就跳过去或者填背景色。字库来源可以是取模软件生成也可以直接从网上找现成的ASC16等文件。有一个很关键的认知字库并不是ILI9341的一部分它只是存放在MCU Flash里的一段数据你完全可以自己用取模软件生
本文来源:http://www.lpqz.cn/news/58256
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Introduction: This is a complete Keil project for driving an ILI9341 2.8-inch TFT LCD screen with an STM32F103C8T6 minimum system board, intended for embedded developers and students who are new to STM32 and need to get a display working quickly. Because the C8T6 comes in a 48-pin package and has no FSMC controller, the project does not use a serial screen module; instead it uses GPIO to emulate 16-bit parallel-port timing, with Port A pins 0~15 as the data lines, together with read/write control pins, to implement basic functions such as LCD initialization, drawing pixels, drawing rectangles, filling, and color display. Touch input is not implemented yet, but this helps keep the focus on learning LCD driver principles and GPIO timing. The archive contains 182 files in total, about 3.16 MB, including C source files, H header files, the startup assembly .s file, as well as uvproj/uvopt project files, and the .o, .lst, .hex, and .map files produced by compiling and linking. The .s is the startup file, c/h are the driver and application code, and the hex can be flashed directly. The directory structure is clear, making it suitable for opening, compiling, and flashing straight to a development board for verification. 5,078 people have studied and downloaded it already. For readers who want to understand driving an LCD with an STM32 emulated parallel port and porting the low-level ILI9341 initialization code, this resource provides a complete, runnable project.
Reference: you can also use this to troubleshoot parallel-port wiring and timing issues. I wonder whether you also have a 2.8-inch TFT LCD screen gathering dust on hand right now. That's how I got my first ILI9341 screen back in the day—I bought an STM32F103C8T6 minimum system board and casually added a 2.8-inch TFT LCD, thinking it was nothing more than a display, so lighting it up would be a piece of cake. As a result, I wrestled with it for three nights in a row and ran through white screen, garbled screen, and diagonal stripes before finally figuring out where the problem was. Driving an ILI9341 with an STM32F103C8T6 is not complicated in itself; the hard part is that online tutorials take for granted far too many details that you 'should know but actually don't.' In this article I write out in full my hands-on experience—from SPI pin mapping and the ILI9341 initialization sequence to window settings, screen-refresh performance optimization, and white-screen troubleshooting. It's for friends who have just lit up an LCD with a C8T6 and are completely lost when it comes to registers. You can get it running without flipping through hundreds of pages of datasheets, yet after reading it you'll know exactly what every command does. 1. The selection logic behind this classic pairing: why match a C8T6 with an ILI9341? 1.1 A typical pairing of a 48-pin small board and a screen:
The STM32F103C8T6 belongs to the F1 series as the 'king of minimum system boards,' featuring a Cortex-M3 core, 72 MHz main frequency, 64 KB Flash, 20 KB SRAM, and an LQFP48 package. Although it doesn't bring out a huge number of GPIOs, none of the common peripherals—SPI, USART, I2C—are missing. The ILI9341 is an extremely common TFT LCD driver chip, supporting up to 240x320 resolution; at 16-bit RGB565 color the image quality is detailed enough, and tinkerers can even support 18-bit color. Together, the two form a classic pairing of 'not many resources, but just enough'—the C8T6's SPI1 runs at 18~36 MHz, which the ILI9341's SPI interface can fully handle, with few pins used and a small driver codebase, making it especially suited as a first screen for beginners. I've seen plenty of friends who right from the start agonize over 'whether to switch to a bigger screen or move up to an F407.' If all you want is to learn display logic, build a dashboard, or show information, a C8T6 plus an ILI9341 is more than adequate. When you later need complex graphics rendering or video playback, it won't be too late to consider changing platforms. 1.2 Hardware checklist and module-selection pitfalls: the things I prepared are pretty standard.
One STM32F103C8T6 minimum system board (the blue pill board being the most common), one 2.8-inch ILI9341 TFT LCD module, one ST-Link V2 programmer, a handful of dupont wires, and both 3.3V and 5V power supplies prepared—some display modules need 5V for the backlight. Here is a very easy pitfall to fall into: two 2.8-inch screens may have completely different interfaces. There are two common types on the market: one is a 16-bit 8080 parallel interface with a big row of pins, requiring more than a dozen GPIOs to drive; the other is an SPI interface that needs only a few wires. The C8T6 isn't exactly flush with pins, so if you get the 8080 parallel version and also need to connect other sensors, pins will be very tight. So before buying, be sure to ask clearly or check the schematic to confirm it's the SPI interface. Also, some modules include touch functionality via an XPT2046 chip, which takes two more pins. If you only need display, you can ignore the touch pins—but note that on some modules the touch IC shares power and reset lines with the screen, and leaving the touch IC uninitialized after power-on does not affect display, so those pins can be left unconnected. 2. First, treat the ILI9341 as an addressable canvas—understanding commands and the GRAM mechanism. 2.1 The driver chip is the screen's built-in brain.
Many tutorials simply throw a long initialization array at you and say 'just copy it.' That does get things running, but the moment you hit a white screen, garbled screen, or wrong colors, you have no idea where to start troubleshooting. So I suggest first understanding, at the principle level, what exactly the ILI9341 is. Inside the ILI9341 there is a 240x320 GRAM, where each address corresponds to one physical pixel on the screen. It's like a painter with a built-in canvas: the MCU doesn't need to light up each pixel individually; it only needs to tell the chip three things over SPI—first, where to start drawing; second, how large an area to draw; third, what color data to fill that area with. All the remaining pixel-arrangement work is done by the ILI9341 itself. So the core of driving an ILI9341 is really interacting with it: send a string of configuration commands to put it into the display mode you want, then keep feeding data into the GRAM. 2.2 The DC pin determines whether the SPI line carries commands or data. SPI is a four-wire communication protocol, but for the ILI9341 an extra DC pin—also called RS or D/C—is needed to distinguish whether the current byte is a 'command' or 'data.' This is the most easily overlooked yet most critical signal in the entire driver.
Pull DC low and the bytes on SPI are treated as commands—for example, 0x2A means 'set column address.' Pull DC high and the bytes are treated as data—for example, the specific column-address values. Without this DC pin, the screen cannot tell whether what comes in is an instruction or pixel data. In the actual code, the write-command and write-data functions look almost identical; the only difference is the level and timing of the DC pin. 2.3 The initialization sequence—meanings of several key commands. The initialization sequence is where many beginners get scared off: a long string of register configurations that looks like gibberish. In fact most commands are unused; the core commands that affect the display are just a few. Command name, key parameters, description: 0x01 Software Reset — software reset, must wait with a delay; 0x11 Sleep Out — exit sleep mode, must wait with a delay; 0x36 Memory Access Control — controls scan direction and RGB order; 0x3A Pixel Format Set — pixel format, 0x55 means RGB565 16-bit; 0x29 Display ON — turn on display. Among these, 0x36 is the one most often singled out for discussion. Its parameters determine the screen's orientation
and color order: for example, 0x00 is usually portrait, 0x60 or 0xA0 is landscape, and bit 3 set to 1 indicates BGR color order. Different modules have different default scan directions, which is why the same initialization code produces 'upside-down' or 'left-right mirrored' displays on different modules. 2.4 The minimal flow for writing one pixel: set a window, then fill. Any display operation ultimately boils down to one action: writing color data into the GRAM. Before writing data, three things must define the range—column address, page address, and the write-memory command. Column address range: command 0x2A, 4 bytes of data—start column high 8 bits, low 8 bits, end column high 8 bits, low 8 bits. Page address range: command 0x2B, 4 bytes of data—start row high 8 bits, low 8 bits, end row high 8 bits, low 8 bits. Write GRAM: after command 0x2C, feed pixel data continuously. Once you understand this flow, you can expand on your own to draw pixels, draw lines, fill rectangles, and display images. The so-called 'draw a point' is nothing more than performing the window-setting procedure twice in a row plus one pixel write, while 'full-screen fill' simply sets the window to the entire screen and then sends hundreds of thousands of bytes of color data continuously. 3. Pin connections and pre-power-on checks—wire it up and it lights up. 3.1 The pin assignment I used
The C8T6 has a built-in SPI1 peripheral, which is the most convenient choice because SPI1 pins can be mapped to standard positions: PA5 is SCK, PA6 is MISO, PA7 is MOSI. The pin mapping I actually used is as follows. Function / MCU pin / notes: screen SCK — PA5 — SPI1_SCK; screen MOSI — PA7 — SPI1_MOSI; screen MISO — PA6 — SPI1_MISO, can be left unconnected when only reading registers; screen CS — PA4 — software-controlled chip select; screen DC — PA3 — command/data select; screen RST — PA2 — hardware reset; backlight BL — 3.3V or a PWM pin, tie high directly to light up; VCC — 3.3V, some modules have an internal LDO and can take 5V, check the markings. The three pins CS, DC, and RST can be swapped freely as long as they don't conflict with peripherals. SCK/MOSI, however, must use the pins corresponding to SPI1; if you want to use SPI2, switch to PB13 and PB15. 3.2 Three things to confirm before powering on. First, level matching. The ILI9341's logic voltage is generally 2.8V to 3.3V, and the C8T6's IO output is also 3.3V, so in theory a direct connection is fine. But some modules, in order to be compatible with Arduino boards,
carry level shifters or LDOs on board, in which case the supply voltage should follow the module's markings—some modules require VCC to be 5V for the internal regulator to work; connect 3.3V and you'll get a 'backlight on but no content' frozen state. Second, common ground. The GND of the MCU and the display module must be tied together—this is the most basic step and yet the easiest to miss. I once had a loose dupont wire causing poor GND contact, so the screen worked intermittently; after a long troubleshooting session I found it was just a loose ground wire. Third, the backlight isn't necessarily on by default. Some modules have a pull-up resistor on the backlight pin and light up as soon as power is applied; others require you to drive it high manually. Before tuning the driver, connect the BL pin to 3.3V to make sure the backlight can light up; otherwise you'll think the screen is broken when in fact the backlight simply isn't on. 3.3 Why I insist you fly-wire first instead of drawing a PCB right away. If you just want to light up the screen and get the code running, I strongly suggest using dupont wires rather than jumping straight to a fabricated board. The SPI clock for a C8T6 driving an ILI9341 is typically around 18 MHz; at this frequency dupont wires are barely adequate, but once the wires exceed 20 cm, waveform distortion easily causes a garbled screen. The benefit of flying wires is that you can shorten them at any time, reassign pins, and use
an oscilloscope clipped onto the wires to measure waveforms. Only after the program is fully working and the pin assignment is confirmed reasonable should you draw a PCB—that's the sound workflow. 4. From code to implementation: the complete chain from SPI configuration to the first pixel. 4.1 SPI peripheral initialization. I used the STM32 standard peripheral library; the approach with the HAL library is exactly the same, only the API names differ. The core is to configure SPI1 as master mode, 8-bit data, MSB first, SPI Mode 0 (CPOL 0, CPHA 0), with the clock prescaler set to divide by 4, i.e. 18 MHz. SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode SPI_Mode_Master; SPI_InitStructure.SPI_DataSize SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL
SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit SPI_FirstBit_MSB; SPI_Init(SPI1, SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); A word about the prescaler: SPI1 hangs off the APB2 bus with a 72 MHz clock, so divide by 2 gives 36 MHz, which in theory is already close to the upper limit of the ILI9341's SPI interface. But in practice, with dupont wires, 36 MHz easily causes signal reflection and crosstalk, leading to a garbled screen. When debugging I first drop to 18 MHz (divide by 4) to get the functionality working and confirm no problems,
then gradually raise it—also a sound debugging approach. 4.2 The write-command and write-data functions—splitting the timing. The two most important low-level functions are write command and write data. They're implemented by controlling the DC pin level before sending a byte. void ILI9341_WriteCmd(uint8_t cmd) { LCD_DC_LOW(); // command mode LCD_CS_LOW(); // chip select low to select the screen SPI1_SendByte(cmd); LCD_CS_HIGH(); } void ILI9341_WriteData(uint8_t dat) { LCD_DC_HIGH(); // data mode LCD_CS_LOW(); SPI1_SendByte(dat); LCD_CS_HIGH(); } Note that the DC signal must settle before chip select is pulled low, otherwise the screen may misjudge the first byte as the wrong type. This timing issue is fairly subtle—when the initialization sequence isn't sensitive to timing it may work occasionally, but when displaying large amounts of pixel data you'll get intermittent garbled screens.
4.3 Setting a window and filling a rectangle. Once you understand the flow in section 2.4, the code is very straightforward. The window-setting function is responsible for telling the screen both the column address and the row address: void ILI9341_SetWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { ILI9341_WriteCmd(0x2A); ILI9341_WriteData(x0 8); ILI9341_WriteData(x0 0xFF); ILI9341_WriteData(x1 8); ILI9341_WriteData(x1 0xFF); ILI9341_WriteCmd(0x2B); ILI9341_WriteData(y0 8); ILI9341_WriteData(y0 0xFF); ILI9341_WriteData(y1 8); ILI9341_WriteData(y1 0xFF);
ILI9341_WriteCmd(0x2C); } When filling a rectangle, first set the window, then send color data continuously. Since colors are 16-bit RGB565, each pixel requires two bytes. void ILI9341_FillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color) { uint32_t i; uint32_t total (uint32_t)w * h; ILI9341_SetWindow(x0, y0, x0 w - 1, y0 h - 1); LCD_DC_HIGH(); LCD_CS_LOW(); for (i 0; i total; i) { SPI1_SendByte(color 8); SPI1_SendByte(color 0xFF); } LCD_CS_HIGH(); } Why is window setting so important? Because
the ILI9341's GRAM write pointer increments continuously. If the screen's current scan direction is 'left to right, top to bottom,' then after finishing a row the pointer automatically jumps to the start of the next row. If you don't set a window, it will keep writing in the default order; when you only want to draw a box in the middle of the screen, the data gets misplaced and what you see is a full screen of gibberish or diagonal stripes. A considerable share of garbled-screen problems come from an incorrectly set window. 4.4 Displaying characters—a font is essentially pixel-dot-matrix data. Displaying characters has nothing to do with the ILI9341 itself; it just treats a character as a small image to fill in. An ASCII font is essentially a two-dimensional array in which each element is 0 or 1—1 means the pixel is lit, 0 means it's off. Take a 16x24 ASCII font as an example: each character occupies 24 bytes, 2 bytes per row, for 24 rows. To display a character, traverse this two-dimensional array; when you hit a 1, fill the foreground color, and when you hit a 0, skip it or fill the background color. The font data can be generated by font-extraction software or simply found online as ready-made files such as ASC16. One key point to keep in mind: a font is not part of the ILI9341—it's just a piece of data stored in the MCU's Flash, and you can generate it yourself with font-extraction software.
本文来源:http://www.lpqz.cn/news/58256
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。