ST7796S 驱动
时间: 2023-12-29 09:26:33 浏览: 186
ST7796S是一款液晶显示驱动器芯片,常用于驱动TFT LCD显示屏。它支持最大分辨率为320x480,并具有丰富的显示功能接口选项。
以下是一个使用ST7796S驱动器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define ST7796S_WIDTH 320
#define ST7796S_HEIGHT 480
#define ST7796S_CMD 0x00
#define ST7796S_DATA 0x01
#define ST7796S_RST_PIN 25
#define ST7796S_DC_PIN 24
#define ST7796S_CS_PIN 8
void st7796s_write_command(uint8_t cmd) {
digitalWrite(ST7796S_DC_PIN, ST7796S_CMD);
wiringPiSPIDataRW(0, &cmd, 1);
}
void st7796s_write_data(uint8_t data) {
digitalWrite(ST7796S_DC_PIN, ST7796S_DATA);
wiringPiSPIDataRW(0, &data, 1);
}
void st7796s_init() {
pinMode(ST7796S_RST_PIN, OUTPUT);
pinMode(ST7796S_DC_PIN, OUTPUT);
pinMode(ST7796S_CS_PIN, OUTPUT);
digitalWrite(ST7796S_RST_PIN, HIGH);
delay(10);
digitalWrite(ST7796S_RST_PIN, LOW);
delay(10);
digitalWrite(ST7796S_RST_PIN, HIGH);
delay(10);
st7796s_write_command(0x11); // Sleep Out
delay(120);
st7796s_write_command(0x36); // Memory Data Access Control
st7796s_write_data(0x48); // BGR, MX, MY
st7796s_write_command(0x3A); // Interface Pixel Format
st7796s_write_data(0x55); // 16-bit color
st7796s_write_command(0x29); // Display On
}
void st7796s_set_pixel(uint16_t x, uint16_t y, uint16_t color) {
st7796s_write_command(0x2A); // Column Address Set
st7796s_write_data(x >> 8);
st7796s_write_data(x & 0xFF);
st7796s_write_data((x + 1) >> 8);
st7796s_write_data((x + 1) & 0xFF);
st7796s_write_command(0x2B); // Page Address Set
st7796s_write_data(y >> 8);
st7796s_write_data(y & 0xFF);
st7796s_write_data((y + 1) >> 8);
st7796s_write_data((y + 1) & 0xFF);
st7796s_write_command(0x2C); // Memory Write
st7796s_write_data(color >> 8);
st7796s_write_data(color & 0xFF);
}
int main() {
wiringPiSetup();
wiringPiSPISetup(0, 8000000);
st7796s_init();
for (uint16_t y = 0; y < ST7796S_HEIGHT; y++) {
for (uint16_t x = 0; x < ST7796S_WIDTH; x++) {
uint16_t color = ((x / 10) << 11) | ((y / 10) << 5);
st7796s_set_pixel(x, y, color);
}
}
return 0;
}
```
这段代码使用了wiringPi库来控制树莓派的GPIO引脚和SPI接口,实现了在ST7796S驱动的TFT LCD显示屏上绘制彩色的垂直条纹。
阅读全文