- 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[4];
- /*Column addresses*/
- SPILCD_WriteCmd(0x2A);
- data[0] = (x1 >> 8) & 0xFF;
- data[1] = x1 & 0xFF;
- data[2] = (x2 >> 8) & 0xFF;
- data[3] = x2 & 0xFF;
- SPILCD_WriteData(data[0]);
- SPILCD_WriteData(data[1]);
- SPILCD_WriteData(data[2]);
- SPILCD_WriteData(data[3]);
- /*Page addresses*/
- SPILCD_WriteCmd(0x2B);
- data[0] = (y1 >> 8) & 0xFF;
- data[1] = y1 & 0xFF;
- data[2] = (y2 >> 8) & 0xFF;
- data[3] = y2 & 0xFF;
- SPILCD_WriteData(data[0]);
- SPILCD_WriteData(data[1]);
- SPILCD_WriteData(data[2]);
- SPILCD_WriteData(data[3]);
- /*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);
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] test command
- */
- void shell_lcd(char argc, char *argv)
- {
- int color;
- int x1,y1,x2,y2;
- if (argc > 1)
- {
- if (!strcmp("clear", &argv[argv[1]]))
- {
- if(argc == 3)
- {
- color = lcd_color_find(&argv[argv[2]]);
- }else if(argc == 2)
- {
- color = g_color_b;
- }else goto lcd_end;
- Lcd_Clear(color);
- }else if (!strcmp("fillrect", &argv[argv[1]]))
- {
- if(argc == 6)
- {
- color = g_color_f;
- }else if(argc == 7)
- {
- color = lcd_color_find(&argv[argv[6]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- x2 = atoi(&argv[argv[4]]);
- y2 = atoi(&argv[argv[5]]);
- 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[argv[1]]))
- {
- if(argc == 5)
- {
- color = g_color_f;
- }else if(argc == 6)
- {
- color = lcd_color_find(&argv[argv[5]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- x2 = atoi(&argv[argv[4]]); //r
- if(x2) Lcd_FillCircle(x1,y1,x2,color);
- else printf("r must > 0");
- }else if (!strcmp("vline", &argv[argv[1]]))
- {
- if(argc == 5)
- {
- color = g_color_f;
- }else if(argc == 6)
- {
- color = lcd_color_find(&argv[argv[5]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- y2 = atoi(&argv[argv[4]]);
- if(y2) Lcd_DrawVLine(x1,y1,y1+y2-1,color);
- else printf("h must > 0");
- }else if (!strcmp("hline", &argv[argv[1]]))
- {
- if(argc == 5)
- {
- color = g_color_f;
- }else if(argc == 6)
- {
- color = lcd_color_find(&argv[argv[5]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- x2 = atoi(&argv[argv[4]]);
- if(x2) Lcd_DrawHLine(x1,y1,x1+x2-1,color);
- else printf("w must > 0");
- }else if (!strcmp("circle", &argv[argv[1]]))
- {
- if(argc == 5)
- {
- color = g_color_f;
- }else if(argc == 6)
- {
- color = lcd_color_find(&argv[argv[5]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- x2 = atoi(&argv[argv[4]]); //r
- if(x2) Lcd_DrawCircle(x1,y1,x2,color);
- else printf("r must > 0");
- }else if (!strcmp("rect", &argv[argv[1]]))
- {
- if(argc == 6)
- {
- color = g_color_f;
- }else if(argc == 7)
- {
- color = lcd_color_find(&argv[argv[6]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- x2 = atoi(&argv[argv[4]]);
- y2 = atoi(&argv[argv[5]]);
- 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[argv[1]]))
- {
- if(argc == 4)
- {
- color = g_color_f;
- }else if(argc == 5)
- {
- color = lcd_color_find(&argv[argv[4]]);
- }else goto lcd_end;
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- Lcd_DrawPoint(x1,y1,color);
- }else if (!strcmp("color", &argv[argv[1]]))
- {
- 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[argv[2]]);
- g_color_b = lcd_color_find(&argv[argv[3]]);
- }else goto lcd_end;
- }else if (!strcmp("move", &argv[argv[1]]))
- {
- if(argc == 2)
- {
- printf("x=%d,y=%d\r\n",g_x,g_y);
- }else if(argc == 4)
- {
- g_x = atoi(&argv[argv[2]]);
- g_y = atoi(&argv[argv[3]]);
- }else goto lcd_end;
- }else if (!strcmp("display", &argv[argv[1]]))
- {
- if(argc == 5)
- {
- x1 = atoi(&argv[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- lcd_set_font_color(g_color_f,g_color_b);
- lcd_disp_str_at(x1,y1,&argv[argv[4]]);
- }else goto lcd_end;
- }else if (!strcmp("image", &argv[argv[1]]))
- {
- 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[argv[2]]);
- y1 = atoi(&argv[argv[3]]);
- // //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 [color]\r\n");
- printf("lcd fillrect x1 y1 w h [color]\r\n");
- printf("lcd fillcircle x1 y1 r [color]\r\n");
- printf("lcd hline x1 y1 w [color]\r\n");
- printf("lcd vline x1 y1 h [color]\r\n");
- printf("lcd circle x1 y1 r [color]\r\n");
- printf("lcd rect x1 y1 w h [color]\r\n");
- printf("lcd point x1 y1 [color]\r\n");
- printf("lcd display x y string \r\n");
- printf("lcd move x y \r\n");
- printf("lcd image x y \r\n");
- printf("lcd color f_color b_color \r\n");
- }
- NR_SHELL_CMD_EXPORT(lcd, shell_lcd, "lcd display test");
工程代码: