本帖最后由 TL-LED 于 2023-6-26 13:02 编辑
这篇来测试下使用【RTT&英飞凌PSoC6评估板】的 SPI方式驱动LCD显示屏。
一、硬件电路图
使用到开发板的Arduino接口
二、软件配置
在rt-thread studio软件下的配置,需要使能spi0接口
三、程序部分
此测试程序测试LCD屏幕刷屏显示,交替显示不同的颜色。
3.1、lcd_test.c
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include "drivers/spi.h"
#define DBG_TAG "LCD"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define LCD_CS GET_PIN(0, 5)
#define LCD_RS GET_PIN(11, 5)
#define LCD_RST GET_PIN(11, 4)
#define LCD_BK GET_PIN(9, 2)
static struct rt_spi_device *lcd_dev;
void senddat(uint16_t dat)
{
uint8_t buf[2];
buf[0]=(uint8_t)(dat>>8);
buf[1]=(uint8_t)dat;
rt_spi_send(lcd_dev, buf, 2);
}
void LCD_Writ_Bus(uint8_t dat)
{
rt_spi_send(lcd_dev, &dat, 1);
}
void LCD_WR_DATA8(uint8_t dat)
{
LCD_Writ_Bus(dat);
}
void LCD_WR_DATA(uint16_t dat)
{
senddat(dat);
}
void LCD_WR_REG(uint16_t dat)
{
rt_pin_write(LCD_RS, PIN_LOW);
senddat(dat);
rt_pin_write(LCD_RS, PIN_HIGH);
}
void lcd_register_write(uint16_t register_id,uint16_t value)
{
LCD_WR_REG(register_id);
LCD_WR_DATA(value);
}
void lcd_gram_write_prepare(void)
{
LCD_WR_REG(0x0022);
}
void lcd_gram_write(uint16_t rgb_code)
{
LCD_WR_DATA(rgb_code);
}
void init_lcd(void)
{
uint16_t i;
//rt_pin_mode(LCD_SPI_SCLK , PIN_MODE_OUTPUT);
// rt_pin_mode(LCD_SPI_MOSI , PIN_MODE_OUTPUT);
// rt_pin_mode(LCD_CS , PIN_MODE_OUTPUT);
rt_pin_mode(LCD_RS , PIN_MODE_OUTPUT);
rt_pin_mode(LCD_RST , PIN_MODE_OUTPUT);
rt_pin_mode(LCD_BK , PIN_MODE_OUTPUT);
rt_pin_write(LCD_BK, PIN_HIGH);
rt_pin_write(LCD_RST, PIN_LOW);
rt_thread_mdelay(100);
rt_pin_write(LCD_RST, PIN_HIGH);
rt_thread_mdelay(100);
lcd_register_write(0x0000,0x0001);
lcd_register_write(0x0003,0xA8A4);
lcd_register_write(0x000C,0x0000);
lcd_register_write(0x000D,0x080C);
lcd_register_write(0x000E,0x2B00);
lcd_register_write(0x001E,0x00B0);
lcd_register_write(0x0001,0x2B3F);
lcd_register_write(0x0002,0x0600);
lcd_register_write(0x0010,0x0000);
lcd_register_write(0x0011,0x6070);
lcd_register_write(0x0005,0x0000);
lcd_register_write(0x0006,0x0000);
lcd_register_write(0x0016,0xEF1C);
lcd_register_write(0x0017,0x0003);
lcd_register_write(0x0007,0x0233);
lcd_register_write(0x000B,0x0000);
lcd_register_write(0x000F,0x0000);
lcd_register_write(0x0041,0x0000);
lcd_register_write(0x0042,0x0000);
lcd_register_write(0x0048,0x0000);
lcd_register_write(0x0049,0x013F);
lcd_register_write(0x004A,0x0000);
lcd_register_write(0x004B,0x0000);
lcd_register_write(0x0044,0xEF00);
lcd_register_write(0x0045,0x0000);
lcd_register_write(0x0046,0x013F);
lcd_register_write(0x0030,0x0707);
lcd_register_write(0x0031,0x0204);
lcd_register_write(0x0032,0x0204);
lcd_register_write(0x0033,0x0502);
lcd_register_write(0x0034,0x0507);
lcd_register_write(0x0035,0x0204);
lcd_register_write(0x0036,0x0204);
lcd_register_write(0x0037,0x0502);
lcd_register_write(0x003A,0x0302);
lcd_register_write(0x003B,0x0302);
lcd_register_write(0x0023,0x0000);
lcd_register_write(0x0024,0x0000);
lcd_register_write(0x0025,0x8000);
lcd_register_write(0x004e,0);
lcd_register_write(0x004f,0);
for(i=50000;i>0;i--);
}
void lcd_cursor_set(uint16_t x,uint16_t y)
{
lcd_register_write(0x004e,x);
lcd_register_write(0x004f,y);
}
/*
void lcd_clear(uint16_t color)
{
uint32_t index=0;
lcd_cursor_set(0,0);
lcd_gram_write_prepare();
//rt_pin_write(LCD_RS, PIN_HIGH);
for(index=0;index<76800;index++){
LCD_WR_DATA(color);
}
}
*/
void lcd_clear(uint16_t Color)
{
unsigned int i, m;
uint32_t index=0;
uint8_t buf[80];
for (i = 0; i < 40; i++)
{
buf[2 * i] = Color >> 8;
buf[2 * i + 1] = Color & 0xff;
}
lcd_cursor_set(0,0);
lcd_gram_write_prepare();
for (i = 0; i < 320; i++)
{
for (m = 0; m < 240;)
{
m += 40;
rt_spi_send(lcd_dev, buf, 80);
}
}
}
void lcd_point_set(uint16_t x,uint16_t y,uint16_t point)
{
if ((x > 240)||(y > 320)){
return;
}
lcd_cursor_set(x,y);
lcd_gram_write_prepare();
lcd_gram_write(point);
}
rt_err_t spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
{
RT_ASSERT(bus_name != RT_NULL);
RT_ASSERT(device_name != RT_NULL);
rt_err_t result = RT_EOK;
struct rt_spi_device *spi_device;
/* attach the device to spi bus*/
spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
RT_ASSERT(spi_device != RT_NULL);
result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, LCD_CS);
if (RT_EOK != result)
{
LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
}
else
{
LOG_I("%s attach to %s done", device_name, bus_name);
}
return result;
}
rt_err_t spi_lcd_init(uint32_t freq)
{
rt_err_t res = RT_EOK;
spi_device_attach(PKG_LCD_SPI_BUS_NAME, PKG_LCD_SPI_DEVICE_NAME, LCD_CS);
lcd_dev = (struct rt_spi_device *)rt_device_find(PKG_LCD_SPI_DEVICE_NAME);
if (lcd_dev != RT_NULL)
{
struct rt_spi_configuration spi_config;
spi_config.data_width = 8;
spi_config.max_hz = freq * 1000 * 1000;
spi_config.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
rt_spi_configure(lcd_dev, &spi_config);
}
else
{
res = -RT_ERROR;
}
return res;
}
3.2、lcd_test.h
#ifndef APPLICATIONS_LCD_TEST_H_
#define APPLICATIONS_LCD_TEST_H_
/* LCD color */
#define WHITE 0xFFFF
#define BLACK 0x0000
#define GREY 0xF7DE
#define BLUE 0x001F
#define BLUE2 0x051F
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
void init_lcd(void);
void lcd_clear(uint16_t color);
rt_err_t spi_lcd_init(uint32_t freq);
#endif /* APPLICATIONS_LCD_TEST_H_ */
3.3、main.c
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include "lcd_test.h"
#define LED_PIN GET_PIN(0, 1)
int main(void)
{
rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
spi_lcd_init(25);
init_lcd();
lcd_clear(WHITE);
for (;;)
{
lcd_clear(GREY);
rt_thread_mdelay(200);
lcd_clear(RED);
rt_thread_mdelay(200);
lcd_clear(GREEN);
rt_thread_mdelay(200);
lcd_clear(YELLOW);
rt_thread_mdelay(200);
lcd_clear(BLUE);
rt_thread_mdelay(200);
}
}
四、程序运行
4.1、硬件连接图
4.2、串口内容
串口命令,查看到SPI设备
4.3、程序运行视频
006