
做温控器面板时,先用数码管加按键凑合,后来想上真彩TFT画温度曲线加触摸按钮。裸操ST7789,SPI一个像素一个像素灌,屏闪得厉害、手指戳上去半秒才反应还经常点歪。把LVGL搬上来才明白:画界面和刷像素是两码事。
TFT屏背后有驱动芯片(ST7789、ILI9341、SSD1309这类),内部有GRAM。MCU只要通过SPI或8080并口把颜色写进GRAM,驱动芯片自己负责刷到玻璃上。颜色用RGB565,一个像素2字节,红5位绿6位蓝5位,比RGB888省三分之一带宽。显存大小等于屏宽乘屏高乘2字节:240x240约112KB,320x240约150KB,480x272约255KB。STM32F103只有20KB SRAM,F407也才128KB,整屏塞不下,这是第一道坎:显存放哪。
LVGL是开源GUI库,建对象(标签、按钮、滑块、图表),它在给的一块buffer上画,定时调lv_timer_handler处理动画和重绘,脏区域通过flush_cb回调搬到屏上。以SPI接口ST7789 1.3寸240x240为例,CubeMX配SPI1为Full-Duplex Master,ST7789用Mode 0,NSS选Software,DC/RES/BL各设一个GPIO_Output,SPI时钟分频先/8跑通。不需要LTDC,小屏SPI足够。代码底层用DC脚区分命令与数据,lcd_write_cmd拉低DC,lcd_write_data拉高DC;lcd_init拉复位、发厂家初始化序列、0x3A设RGB565、0x29开显示。
LVGL核心三样:buffer、flush回调、心跳。用10行部分缓冲disp_buf[TFT_W*10]约4800字节省内存;my_flush_cb把脏区域通过SPI搬到GRAM后必须调lv_disp_flush_ready,否则LVGL不再派活。主循环里lv_tick_inc(5)每5ms喂心跳并周期调lv_timer_handler。
十个真实翻车点:一是整屏buffer开在栈上爆RAM导致HardFault,小RAM芯片用部分缓冲或外挂SRAM;二是DC脚搞反导致白屏;三是SPI模式不对,ST7789多数要Mode 0;四是刷新率上不去动画花屏,提速三招用DMA、提高SPI时钟、只刷脏区;五是lv_timer_handler没周期调用界面不动;六是flush_cb忘调lv_disp_flush_ready界面冻住;七是颜色格式字节序不对,0x3A设0x55选16位并确认RGB565字节序;八是回调里干重活栈溢出;九是触摸没接没校准,电阻屏要XPT2046校准、电容屏走I2C中断;十是背光没开或常亮烧屏,用PWM调背光。
适合带真彩界面的嵌入式产品(温控面板、仪器显示、手持设备),不适合纯字符显示或SRAM极小又不想外挂显存的芯片。铁律三条:显存大小先算账、DC脚严格区分命令数据、lv_timer_handler与flush_cb里的lv_disp_flush_ready必须都在。
本文来源:http://www.hqwc.cn/a/1235699.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
When building a thermostat panel, we first made do with a seven-segment display and buttons, and later wanted to move to a true-colour TFT to draw temperature curves and add touch buttons. Driving the ST7789 directly and pushing pixels one by one over SPI caused severe flicker, and a touch would take half a second to respond and often missed the target. Only after bringing in LVGL did we understand: drawing an interface and refreshing pixels are two different things.
Behind a TFT screen there is a driver chip (such as the ST7789, ILI9341 or SSD1309) with internal GRAM. The MCU only needs to write colours into the GRAM over SPI or an 8080 parallel port, and the driver chip takes care of refreshing the glass. Colours use RGB565, two bytes per pixel — 5 bits red, 6 bits green, 5 bits blue — saving one third of the bandwidth compared with RGB888. The framebuffer size equals screen width × screen height × 2 bytes: 240x240 is about 112KB, 320x240 about 150KB and 480x272 about 255KB. The STM32F103 has only 20KB of SRAM and the F407 just 128KB, so a full screen cannot fit — this is the first hurdle: where to put the framebuffer.
LVGL is an open-source GUI library: you create objects (labels, buttons, sliders, charts), it draws on a buffer you give it, you periodically call lv_timer_handler to handle animations and redraws, and dirty regions are transferred to the screen through the flush_cb callback. Taking a 1.3-inch 240x240 ST7789 with SPI as an example: in CubeMX configure SPI1 as Full-Duplex Master, use Mode 0 for the ST7789, set NSS to Software, configure DC/RES/BL each as a GPIO_Output, and start with the SPI clock prescaler at /8 to get it working. No LTDC is needed — SPI is sufficient for a small screen. At the low level the code uses the DC pin to distinguish commands from data: lcd_write_cmd pulls DC low and lcd_write_data pulls DC high; lcd_init toggles reset, sends the manufacturer's initialisation sequence, sets RGB565 with 0x3A and turns the display on with 0x29.
The three core things in LVGL are: buffer, flush callback and heartbeat. Use a 10-line partial buffer disp_buf[TFT_W*10] of about 4,800 bytes to save memory; after my_flush_cb transfers the dirty region to GRAM over SPI, it must call lv_disp_flush_ready, otherwise LVGL will stop assigning work. In the main loop, feed the heartbeat with lv_tick_inc(5) every 5ms and call lv_timer_handler periodically.
Ten real failure points: first, allocating a full-screen buffer on the stack blows up RAM and causes a HardFault — on small-RAM chips use a partial buffer or external SRAM; second, getting the DC pin backwards causes a white screen; third, a wrong SPI mode — most ST7789 units need Mode 0; fourth, low refresh rate causing garbled animation, with three speed-up tricks: use DMA, raise the SPI clock, and refresh only dirty regions; fifth, not calling lv_timer_handler periodically leaves the UI frozen; sixth, forgetting lv_disp_flush_ready in flush_cb also freezes the UI; seventh, wrong colour byte order — set 0x3A to 0x55 for 16-bit and confirm the RGB565 byte order; eighth, doing heavy work inside a callback causes stack overflow; ninth, touch not connected or not calibrated — resistive screens need XPT2046 calibration and capacitive screens use I2C interrupts; tenth, backlight not enabled or left on permanently causing burn-in — use PWM to control the backlight.
Suitable for embedded products with true-colour interfaces (thermostat panels, instrument displays, handheld devices), but not for character-only displays or chips with very little SRAM where you do not want external framebuffer memory. Three iron rules: calculate the framebuffer size first, strictly separate commands from data on the DC pin, and make sure both lv_timer_handler and lv_disp_flush_ready inside flush_cb are present.
本文来源:http://www.hqwc.cn/a/1235699.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。