【AT-START-F423测评】+ spi接口lcd显示
本次研究SPI接口的lcd显示测试。自己做的一个lcd模块,使用arduino接口插在板子上。引脚使用
LCD SCK==PA5
LCD MISO = PA6
LCD MOSI = PA7
LCD CS = PA15
LCD DC = PC7
LCD BLK = PA9
开发板接口。
lcd模块接口
实物如上图。
下面初始化SPI.
void drv_spi_gpio_init(void)
{
gpio_init_type gpio_initstructure;
spi_init_type spi_init_struct;
crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_initstructure);
/* spi1 sck pin */
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_5;
gpio_init(GPIOA, &gpio_initstructure);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE5, GPIO_MUX_5);
/* spi1 miso pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_6;
gpio_init(GPIOA, &gpio_initstructure);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE6, GPIO_MUX_5);
/* spi1 mosi pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOA, &gpio_initstructure);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_5);
spi_default_para_init(&spi_init_struct);
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_4;
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(SPI1, &spi_init_struct);
spi_enable(SPI1, TRUE);
}SPI读写
uint8_t spi_writebyte(uint8_t index,uint8_t TxData)
{
uint8_t re;
while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET);
spi_i2s_data_transmit(SPI1, TxData);
while(spi_i2s_flag_get(SPI1, SPI_I2S_RDBF_FLAG) == RESET);
re = spi_i2s_data_receive(SPI1);
return re;
}lcd使用的IO初始化
void spilcd_gpio_init(void)
{
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_initstructure);
/* lcd CS=PA15,BLK=PA9 */
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_out_type= GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_15 | GPIO_PINS_9;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOA, &gpio_initstructure);
//DC = PC7
gpio_initstructure.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOC, &gpio_initstructure);
drv_spi_gpio_init();
}spi接口驱动
static void SPILCD_WriteCmd(uint8_t Data)
{
LCD_DC_CLR();
LCD_CS_CLR();
spi_writebyte(0,Data);
LCD_CS_SET();
}
static void SPILCD_WriteData(uint8_t Data)
{
LCD_DC_SET();
LCD_CS_CLR();
spi_writebyte(0,Data);
LCD_CS_SET();
}
static void SPILCD_WriteMultiData(const uint8_t *pData, uint32_t NumItems)
{
uint32_t i=NumItems;
uint32_t Data;
LCD_DC_SET();
LCD_CS_CLR();
spi_writebuff(0,(uint8_t *)pData,NumItems);
LCD_CS_SET();
}
static void SPILCD_SetRegin(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
uint8_t data;
/*Column addresses*/
SPILCD_WriteCmd(0x2A);
data = (x1 >> 8) & 0xFF;
data = x1 & 0xFF;
data = (x2 >> 8) & 0xFF;
data = x2 & 0xFF;
SPILCD_WriteData(data);
SPILCD_WriteData(data);
SPILCD_WriteData(data);
SPILCD_WriteData(data);
/*Page addresses*/
SPILCD_WriteCmd(0x2B);
data = (y1 >> 8) & 0xFF;
data = y1 & 0xFF;
data = (y2 >> 8) & 0xFF;
data = y2 & 0xFF;
SPILCD_WriteData(data);
SPILCD_WriteData(data);
SPILCD_WriteData(data);
SPILCD_WriteData(data);
/*Memory write*/
SPILCD_WriteCmd(0x2C);
}
void spilcd_draw_bitmap(uint16_t x,uint16_t y,uint16_t w,uint16_t h,const uint8_t *pbuff)
{
uint16_t x1,y1,x2,y2;
uint32_t send_size = w * h * 2;
x1 = x;
y1 = y;
x2 = x+w-1;
y2 = y+h-1;
SPILCD_SetRegin(x1,y1,x2,y2);
SPILCD_WriteMultiData(pbuff, send_size);
LCD_CS_SET();
}
主函数调用lcd初始化,并显示。
spilcd_init();
Lcd_Clear(LCD_RED);
Delay_ms(1000);
Lcd_Clear(LCD_GREEN);
Delay_ms(1000);
Lcd_Clear(LCD_BLUE);
Delay_ms(1000);
Lcd_Clear(LCD_WHITE);
Delay_ms(1000);
通过shell控制显示效果。
#if UART_SHELL == NR_MICRO_SHELL
#include "nr_micro_shell.h"
extern const unsigned char gImage_1[];
/* GLCD RGB color definitions */
#define GLCD_COLOR_BLACK 0x0000/* 0, 0, 0 */
#define GLCD_COLOR_NAVY 0x000F/* 0, 0, 128 */
#define GLCD_COLOR_DARK_GREEN 0x03E0/* 0, 128, 0 */
#define GLCD_COLOR_DARK_CYAN 0x03EF/* 0, 128, 128 */
#define GLCD_COLOR_MAROON 0x7800/* 128, 0, 0 */
#define GLCD_COLOR_PURPLE 0x780F/* 128, 0, 128 */
#define GLCD_COLOR_OLIVE 0x7BE0/* 128, 128, 0 */
#define GLCD_COLOR_LIGHT_GREY 0xC618/* 192, 192, 192 */
#define GLCD_COLOR_DARK_GREY 0x7BEF/* 128, 128, 128 */
#define GLCD_COLOR_BLUE 0x001F/* 0, 0, 255 */
#define GLCD_COLOR_GREEN 0x07E0/* 0, 255, 0 */
#define GLCD_COLOR_CYAN 0x07FF/* 0, 255, 255 */
#define GLCD_COLOR_RED 0xF800/* 255, 0, 0 */
#define GLCD_COLOR_MAGENTA 0xF81F/* 255, 0, 255 */
#define GLCD_COLOR_YELLOW 0xFFE0/* 255, 255, 0 */
#define GLCD_COLOR_WHITE 0xFFFF/* 255, 255, 255 */
static uint16_t g_color_f = GLCD_COLOR_WHITE,g_color_b = GLCD_COLOR_BLACK;
static int16_t g_x=0,g_y=0;
static uint16_t lcd_color_find(char *pstr)
{
if (!strcmp("black", pstr)) return GLCD_COLOR_BLACK;
else if (!strcmp("white", pstr)) return GLCD_COLOR_WHITE;
else if (!strcmp("red", pstr)) return GLCD_COLOR_RED;
else if (!strcmp("yellow", pstr)) return GLCD_COLOR_YELLOW;
else if (!strcmp("green", pstr)) return GLCD_COLOR_GREEN;
else if (!strcmp("blue", pstr)) return GLCD_COLOR_BLUE;
else if (!strcmp("purple", pstr)) return GLCD_COLOR_PURPLE;
else if (!strcmp("light", pstr)) return GLCD_COLOR_LIGHT_GREY;
else if (!strcmp("dark", pstr)) return GLCD_COLOR_DARK_GREY;
else return atoi(pstr);
}
/**
* @brief test command
*/
void shell_lcd(char argc, char *argv)
{
int color;
int x1,y1,x2,y2;
if (argc > 1)
{
if (!strcmp("clear", &argv]))
{
if(argc == 3)
{
color = lcd_color_find(&argv]);
}else if(argc == 2)
{
color = g_color_b;
}else goto lcd_end;
Lcd_Clear(color);
}else if (!strcmp("fillrect", &argv]))
{
if(argc == 6)
{
color = g_color_f;
}else if(argc == 7)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
x2 = atoi(&argv]);
y2 = atoi(&argv]);
if(x2 && y2) Lcd_Fill(x1,y1,x1+x2-1,y1+y2-1,color);
else printf("w h must > 0");
}else if (!strcmp("fillcircle", &argv]))
{
if(argc == 5)
{
color = g_color_f;
}else if(argc == 6)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
x2 = atoi(&argv]);//r
if(x2)Lcd_FillCircle(x1,y1,x2,color);
else printf("r must > 0");
}else if (!strcmp("vline", &argv]))
{
if(argc == 5)
{
color = g_color_f;
}else if(argc == 6)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
y2 = atoi(&argv]);
if(y2)Lcd_DrawVLine(x1,y1,y1+y2-1,color);
else printf("h must > 0");
}else if (!strcmp("hline", &argv]))
{
if(argc == 5)
{
color = g_color_f;
}else if(argc == 6)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
x2 = atoi(&argv]);
if(x2)Lcd_DrawHLine(x1,y1,x1+x2-1,color);
else printf("wmust > 0");
}else if (!strcmp("circle", &argv]))
{
if(argc == 5)
{
color = g_color_f;
}else if(argc == 6)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
x2 = atoi(&argv]);//r
if(x2) Lcd_DrawCircle(x1,y1,x2,color);
else printf("r must > 0");
}else if (!strcmp("rect", &argv]))
{
if(argc == 6)
{
color = g_color_f;
}else if(argc == 7)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
x2 = atoi(&argv]);
y2 = atoi(&argv]);
if(x2 && y2) Lcd_DrawRect(x1,y1,x1+x2-1,y1+y2-1,color);
else printf("w h must > 0");
}else if (!strcmp("point", &argv]))
{
if(argc == 4)
{
color = g_color_f;
}else if(argc == 5)
{
color = lcd_color_find(&argv]);
}else goto lcd_end;
x1 = atoi(&argv]);
y1 = atoi(&argv]);
Lcd_DrawPoint(x1,y1,color);
}else if (!strcmp("color", &argv]))
{
if(argc == 2)
{
printf("f_color=%04X,b_color=%04X\r\n",g_color_f,g_color_b);
}else if(argc == 4)
{
g_color_f = lcd_color_find(&argv]);
g_color_b = lcd_color_find(&argv]);
}else goto lcd_end;
}else if (!strcmp("move", &argv]))
{
if(argc == 2)
{
printf("x=%d,y=%d\r\n",g_x,g_y);
}else if(argc == 4)
{
g_x = atoi(&argv]);
g_y = atoi(&argv]);
}else goto lcd_end;
}else if (!strcmp("display", &argv]))
{
if(argc == 5)
{
x1 = atoi(&argv]);
y1 = atoi(&argv]);
lcd_set_font_color(g_color_f,g_color_b);
lcd_disp_str_at(x1,y1,&argv]);
}else goto lcd_end;
}else if (!strcmp("image", &argv]))
{
if(argc == 2)
{
// //Lcd_DrawImage(g_x,g_y,g_x+39,g_y+39,(uint8_t *)gImage_1);
}else if(argc == 4)
{
x1 = atoi(&argv]);
y1 = atoi(&argv]);
// //Lcd_DrawImage(x1,y1,x1+39,y1+39,(uint8_t *)gImage_1);
}else goto lcd_end;
}else goto lcd_end;
}else goto lcd_end;
return;
lcd_end:
printf("usage: lcd\r\n");
printf("lcd clear \r\n");
printf("lcd fillrect x1 y1 wh\r\n");
printf("lcd fillcircle x1 y1 r \r\n");
printf("lcd hline x1 y1 w \r\n");
printf("lcd vline x1 y1 h\r\n");
printf("lcd circle x1 y1 r \r\n");
printf("lcd rect x1 y1 wh\r\n");
printf("lcd point x1 y1 \r\n");
printf("lcd display xy string \r\n");
printf("lcd move xy \r\n");
printf("lcd image xy \r\n");
printf("lcd color f_colorb_color \r\n");
}
NR_SHELL_CMD_EXPORT(lcd, shell_lcd, "lcd display test");
工程代码:
**** Hidden Message *****
学习一下SPI驱动 屏幕的板子在哪里高的? 单片小菜 发表于 2023-11-13 17:16
屏幕的板子在哪里高的?
自己画的板 楼主这个屏下面左边那个大黑色的是什么?没看出来是什么 forgot 发表于 2023-11-17 09:15
楼主这个屏下面左边那个大黑色的是什么?没看出来是什么
2轴的遥杆 主要涉及到如何使用SPI接口连接LCD显示屏 根据LCD驱动器的数据手册和接口协议,编写相应的SPI驱动程序 在AT-START-F423开发板上,我们可以使用SPI接口连接LCD显示屏进行显示。 对于SPI接口的LCD显示,AT-START-F423可以通过SPI接口与LCD驱动器进行通信,实现LCD的显示功能。 SPI接口需要配置为Master模式,并设置相应的时钟分频因子、数据位宽、相位和极性等参数。 使用SPI接口发送指令和数据,实现LCD显示屏的文本、图片等内容的显示。 学习学习 好帖,谢谢分享 好帖,谢谢分享 学习 硬件驱显SPI屏 不错 学习了 学习学习,谢谢分享
页:
[1]