STM32 LCD中英文字符显示学习笔记
本实验基于STM32 LCD英文字符显示学习笔记、STM32 SPI Flash学习笔记这两个程序编写
开发板:野火指南者(STM32F103VE)
STM32库版本:STM32F10x_StdPeriph_Lib_V3.5.0
IDE:KEIL5(代码编写很不方便,只在编译的时候用到)
代码编写工具:Source Insight 4.0(跟读代码、编写代码的最佳工具,会对本实验项目中文显示有点问题,main.c中会提到)
硬件原理图:
1. 新建user_spi_flash.h、user_spi_flash.c、user_fsmc_lcd.h、user_fsmc_lcd.c、main.c 5个文件,并从
STM32官方库的例子中将stm32f10x_it.c、stm32f10x_it.h、stm32f10x_conf.h拷贝到自己的工程目录下。
2. 在user_spi_flash.h中添加如下代码
1 #ifndef __USER_SPI_FLASH_H 2 #define __USER_SPI_FLASH_H 3 4 #include "stm32f10x.h" 5 #include "stdio.h" 6 7 8 9 #define TIMEOUT 0x00010000 10 #define PageSize 256 11 #define DUMMYDATA 0x00 12 13 void user_SPI_GPIO_Config(void); 14 void user_SPI_Config(void); 15 void user_SPI_Flash_CS(int status); 16 void user_SPI_Flash_Write_Contrl(int status); 17 uint8_t user_SPI_Flash_Read_Write_Byte(uint8_t data); 18 int user_timeout(uint16_t SPI_I2S_FLAG, int id); 19 void user_SPI_Flash_Write_PageData(uint8_t * data, uint32_t address, int num); 20 void user_SPI_Flash_Write_NData(uint8_t * data, uint32_t address, uint32_t num); 21 void user_SPI_Flash_Erase_Sector(uint32_t address); 22 void user_testData_Config(uint8_t * data, uint32_t num); 23 void user_SPI_Flash_Read_NData(uint8_t * RevData, uint32_t address, uint32_t num); 24 25 26 27 28 #endif
3. 在user_spi_flash.c中添加如下代码
1 #include "user_spi_flash.h" 2 3 ////配置SPI GPIO口 4 void user_SPI_GPIO_Config(void) 5 { 6 GPIO_InitTypeDef SPI_MOSI_PA7,SPI_MISO_PA6,SPI_CS_PC0,SPI_CLK_PA5; 7 8 //使能对应PIN所在的GPIO口时钟 9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 10 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 11 12 //配置使用到的PIN 13 SPI_MOSI_PA7.GPIO_Mode = GPIO_Mode_AF_PP; 14 SPI_MOSI_PA7.GPIO_Pin = GPIO_Pin_7; 15 SPI_MOSI_PA7.GPIO_Speed = GPIO_Speed_50MHz; 16 17 SPI_MISO_PA6.GPIO_Mode = GPIO_Mode_IN_FLOATING; 18 SPI_MISO_PA6.GPIO_Pin = GPIO_Pin_6; 19 20 SPI_CS_PC0.GPIO_Mode = GPIO_Mode_Out_PP; 21 SPI_CS_PC0.GPIO_Pin = GPIO_Pin_0; 22 SPI_CS_PC0.GPIO_Speed = GPIO_Speed_50MHz; 23 24 SPI_CLK_PA5.GPIO_Mode = GPIO_Mode_AF_PP; 25 SPI_CLK_PA5.GPIO_Pin = GPIO_Pin_5; 26 SPI_CLK_PA5.GPIO_Speed = GPIO_Speed_50MHz; 27 28 //初始化GPIO口对应的寄存器 29 GPIO_Init(GPIOA, &SPI_MOSI_PA7); 30 GPIO_Init(GPIOA, &SPI_MISO_PA6); 31 GPIO_Init(GPIOC, &SPI_CS_PC0); 32 GPIO_Init(GPIOA, &SPI_CLK_PA5); 33 34 //Flash在读写前,要处于非选择状态,需要读写时再打开, 35 //否则出现SPI无法正常读写,所以最好在初始化GPIO_InitTypeDef结构体时将其处于非选择状态 36 user_SPI_Flash_CS(DISABLE); 37 38 39 } 40 41 42 //配置SPI 43 void user_SPI_Config(void) 44 { 45 46 SPI_InitTypeDef SPI_Config; 47 48 //使能对应的SPI时钟 49 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 50 51 //配置SPI 52 SPI_Config.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; 53 SPI_Config.SPI_CPHA = SPI_CPHA_2Edge; 54 SPI_Config.SPI_CPOL = SPI_CPOL_High; 55 SPI_Config.SPI_CRCPolynomial = 7; 56 SPI_Config.SPI_DataSize = SPI_DataSize_8b; 57 SPI_Config.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 58 SPI_Config.SPI_FirstBit = SPI_FirstBit_MSB; 59 SPI_Config.SPI_Mode = SPI_Mode_Master; 60 SPI_Config.SPI_NSS = SPI_NSS_Soft; 61 62 //初始化SPI 63 SPI_Init(SPI1, &SPI_Config); 64 65 //使能SPI 66 SPI_Cmd(SPI1, ENABLE); 67 68 } 69 70 71 72 //FLASH片选函数,低电平有效 73 void user_SPI_Flash_CS(int status) 74 { 75 if(status == DISABLE) 76 { 77 GPIOC->BSRR = GPIO_Pin_0; 78 } 79 else 80 { 81 GPIOC->BRR = GPIO_Pin_0; 82 } 83 } 84 85 86 87 //FLASH写使能控制函数 88 void user_SPI_Flash_Write_Contrl(int status) 89 { 90 if(status == DISABLE) 91 { 92 user_SPI_Flash_Read_Write_Byte(0x04); 93 } 94 else 95 { 96 user_SPI_Flash_Read_Write_Byte(0x06); 97 } 98 } 99 100 101 //FLASH单个字节读写函数,其它函数都需要调用此函数,特别注意的是读写需要写在一起,否则无法正常工作, 102 //具体原因不知,不能工作的写法如随后注释的代码 103 uint8_t user_SPI_Flash_Read_Write_Byte(uint8_t data) 104 { 105 if(user_timeout(SPI_I2S_FLAG_TXE, 1) == -1) 106 { 107 return -1; 108 } 109 110 SPI_I2S_SendData(SPI1, data); 111 112 if(user_timeout(SPI_I2S_FLAG_RXNE, 2) == -1) 113 { 114 return -1; 115 } 116 117 return SPI_I2S_ReceiveData(SPI1); 118 } 119 120 /* 121 122 int user_SPI_Write_Byte(uint8_t data) 123 { 124 125 if(timeout(SPI_I2S_FLAG_TXE, 3) == 0) 126 { 127 return 0; 128 } 129 130 131 132 SPI_I2S_SendData(SPI1, data); 133 134 return 1; 135 136 } 137 138 139 int user_SPI_Read_Byte(uint8_t data) 140 { 141 if(timeout(SPI_I2S_FLAG_RXNE, 4) == 0) 142 { 143 return 0; 144 } 145 146 147 return SPI_I2S_ReceiveData(SPI1); 148 149 } 150 151 */ 152 153 154 155 //读取操作等待时间函数,如超时则进行相应的处理 156 int user_timeout(uint16_t SPI_I2S_FLAG, int id) 157 { 158 int time = TIMEOUT; 159 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG) == RESET) 160 { 161 if(time == 0) 162 { 163 printf("timeout! the error id is %d\n",id); 164 return -1; 165 } 166 time--; 167 } 168 169 return 0; 170 } 171 172 173 //页数据写入函数 174 void user_SPI_Flash_Write_PageData(uint8_t * data, uint32_t address, int num) 175 { 176 if(num > PageSize) 177 { 178 printf("the data is too match! the max write 256 data!\n"); 179 num = PageSize; 180 } 181 182 183 user_SPI_Flash_CS(ENABLE); 184 //user_SPI_Flash_Write_Contrl(ENABLE); //写使能函数,在此并没有什么影响 185 user_SPI_Flash_Read_Write_Byte(0x02); 186 187 user_SPI_Flash_Read_Write_Byte(address >> 16); 188 user_SPI_Flash_Read_Write_Byte(address >> 8); 189 user_SPI_Flash_Read_Write_Byte(address); 190 191 while(num--) 192 { 193 user_SPI_Flash_Read_Write_Byte(*data); 194 data++; 195 //address++; 196 } 197 198 user_SPI_Flash_Read_Write_Byte(0x05); 199 while(0x01 == user_SPI_Flash_Read_Write_Byte(DUMMYDATA)) 200 { 201 202 } 203 204 //user_SPI_Flash_Write_Contrl(DISABLE); 205 user_SPI_Flash_CS(DISABLE); 206 207 208 } 209 210 //写入N个数据到FLASH,但是这里最大也就只能到256,跟页写入函数一样, 211 //此函数的意义,个人认为主要是进行写地址对齐,当然真的要写入大于256的数据应该也是可以的,但是 212 //应该比较麻烦一点,个人想到的是定义一个二维数组 213 void user_SPI_Flash_Write_NData(uint8_t * data, uint32_t address, uint32_t num) 214 { 215 216 int count_address = 0, count_pageData = 0, count_morePageData = 0; 217 218 count_address = address % PageSize; //不满一页的地址,0为地址已对齐 219 count_pageData = num / PageSize; //多少整页数据 220 count_morePageData = num % PageSize; //不满一页的数据 221 222 //地址是否对齐 223 if(count_address == 0) 224 { 225 if(count_morePageData == 0) //是否有不满一页的数据 226 { 227 while(count_pageData--) //将整页数据先写入 228 { 229 user_SPI_Flash_Write_PageData(data, address, PageSize); 230 address += PageSize; 231 data += PageSize; 232 } 233 } 234 else 235 { 236 if(count_pageData > 0) 237 { 238 while(count_pageData--) 239 { 240 user_SPI_Flash_Write_PageData(data, address, PageSize); 241 address += PageSize; 242 data += PageSize; 243 } 244 } 245 246 user_SPI_Flash_Write_PageData(data, address, count_morePageData); 247 } 248 } 249 else 250 { 251 user_SPI_Flash_Write_PageData(data, address, PageSize - count_address); 252 address += PageSize - count_address; 253 data += PageSize - count_address; 254 255 if(count_morePageData == 0) 256 { 257 while(count_pageData--) 258 { 259 user_SPI_Flash_Write_PageData(data, address, PageSize); 260 address += PageSize; 261 data += PageSize; 262 } 263 } 264 else 265 { 266 if(count_pageData > 0) 267 { 268 while(count_pageData--) 269 { 270 user_SPI_Flash_Write_PageData(data, address, PageSize); 271 address += PageSize; 272 data += PageSize; 273 274 } 275 276 } 277 278 user_SPI_Flash_Write_PageData(data, address, count_morePageData - count_address); 279 } 280 } 281 282 283 284 } 285 286 287 //从FLASH读取数据,个数不受限制 288 void user_SPI_Flash_Read_NData(uint8_t * RevData, uint32_t address, uint32_t num) 289 { 290 291 user_SPI_Flash_CS(ENABLE); 292 //user_SPI_Flash_Write_Contrl(ENABLE); 读数据不能打开写使能 293 294 user_SPI_Flash_Read_Write_Byte(0x03); 295 user_SPI_Flash_Read_Write_Byte((address & 0xFF0000) >> 16); 296 user_SPI_Flash_Read_Write_Byte((address & 0xFF00) >> 8); 297 user_SPI_Flash_Read_Write_Byte(address & 0xFF); 298 299 while(num--) 300 { 301 *RevData = user_SPI_Flash_Read_Write_Byte(DUMMYDATA); 302 RevData++; 303 304 } 305 306 //user_SPI_Flash_Write_Contrl(DISABLE); 与前面对应,没有打开就没有关闭 307 user_SPI_Flash_CS(DISABLE); 308 309 } 310 311 312 313 314 315 void user_SPI_Flash_Erase_Sector(uint32_t address) 316 { 317 user_SPI_Flash_CS(ENABLE); 318 user_SPI_Flash_Write_Contrl(ENABLE); 319 320 user_SPI_Flash_Read_Write_Byte(0x20); //扇区擦除指令 321 322 user_SPI_Flash_Read_Write_Byte(address >> 16); 323 user_SPI_Flash_Read_Write_Byte(address >> 8); 324 user_SPI_Flash_Read_Write_Byte(address); 325 326 user_SPI_Flash_Read_Write_Byte(0x05); //读取状态寄存器指令 327 while(0x01 == user_SPI_Flash_Read_Write_Byte(DUMMYDATA)); //读取FLASH状态寄存器,若为完成则一直循环,等待操作完成 328 329 user_SPI_Flash_Write_Contrl(DISABLE); 330 user_SPI_Flash_CS(DISABLE); 331 332 }
4. 在user_fsmc_lcd.h中添加如下代码
1 #ifndef __USER_FSMC_LCD_H 2 #define __USER_FSMC_LCD_H 3 4 #include "stm32f10x.h" 5 6 #define LCD_CMD ((uint32_t)0x60000000) 7 #define LCD_DATA ((uint32_t)0x60020000) 8 9 #define TEXTCOLOR 0xF800 10 #define BACKGROUND 0x0000 11 #define FONT_ADDRESS 387*4096 //固化在Flash中的中文字模起始地址 12 13 14 void time_delay(uint32_t t); //时间延时函数 15 void user_lcd_display_string(char * pchar); //显示英文字符串函数 16 void user_lcd_fill_color(uint32_t fill_lcd, uint16_t color); //整个屏幕填充的颜色函数 17 void user_lcd_clear(void); //清屏函数,将屏幕配置为黑色 18 void user_lcd_display_char(uint16_t x, uint16_t y, char ch); //单个字符显示函数 19 void user_View_Window(uint16_t lcd_x, uint16_t lcd_y, uint16_t lcd_width, uint16_t lcd_height); //初始化整个屏幕 20 void user_LCD_Init(void); //LCD初始化函数 21 void user_LCD_Scan(void); //LCD扫描函数,这里配置为模式六,即X--240, Y--320,X从左到右,Y从上到下扫描 22 void user_LCD_DATA(uint16_t data); //LCD数据写入函数 23 void user_LCD_CMD(uint16_t cmd); //LCD命令操作函数 24 void user_LCD_REG_Config(void); //LCD对应的IC控制寄存器配置函数,这个配置与LCD上控制芯片相关,如何配置应该是向供应商获取的 25 void user_FSMC_Config(void); //FSMC配置函数 26 void user_LCD_BL(FunctionalState Status); //LCD背光信号控制函数 27 void user_LCD_RST(FunctionalState Status); //LCD复位信号控制函数 28 void user_LCD_GPIO_Config(void); //LCD相关GPIO PIN初始化函数 29 void user_lcd_display_char_CN(uint16_t x, uint16_t y, uint16_t ch); 30 void user_lcd_display_string_EN_CN(char * pchar); 31 void user_Get_CHAR_CN(uint8_t * pchar, uint16_t ch); 32 33 #endif
5. 在user_fsmc_lcd.c中添加如下代码
1 #include "user_fsmc_lcd.h" 2 #include "user_spi_flash.h" 3 4 5 6 //存放英文字模的数组,宽度8bit像素,高度16bit像素 7 const uint8_t ASCII8sx16_Table [ ] = { 8 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 9 0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x08,0x00,0x08,0x18,0x00,0x00,0x00, 10 0x00,0x00,0x00,0x34,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 11 0x00,0x00,0x00,0x00,0x16,0x24,0x7f,0x24,0x24,0x24,0x7e,0x24,0x24,0x00,0x00,0x00, 12 0x00,0x00,0x00,0x08,0x3e,0x68,0x48,0x68,0x1c,0x16,0x12,0x12,0x7c,0x10,0x10,0x00, 13 0x00,0x00,0x00,0x61,0xd2,0x96,0x74,0x08,0x10,0x16,0x29,0x49,0xc6,0x00,0x00,0x00, 14 0x00,0x00,0x00,0x00,0x3c,0x64,0x64,0x38,0x72,0x4a,0xce,0x46,0x7f,0x00,0x00,0x00, 15 0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 16 0x00,0x00,0x00,0x04,0x08,0x18,0x10,0x30,0x30,0x30,0x30,0x10,0x10,0x18,0x0c,0x04, 17 0x00,0x00,0x00,0x20,0x10,0x08,0x08,0x0c,0x04,0x04,0x04,0x0c,0x08,0x18,0x10,0x20, 18 0x00,0x00,0x00,0x08,0x0a,0x34,0x1c,0x6a,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 19 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x7f,0x18,0x18,0x18,0x00,0x00,0x00, 20 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x08,0x30,0x00, 21 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0x00,0x00,0x00,0x00, 22 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00, 23 0x00,0x00,0x00,0x02,0x06,0x04,0x0c,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x00,0x00, 24 0x00,0x00,0x00,0x00,0x3c,0x66,0x42,0x47,0x5b,0x73,0x42,0x66,0x3c,0x00,0x00,0x00, 25 0x00,0x00,0x00,0x00,0x18,0x78,0x48,0x08,0x08,0x08,0x08,0x08,0x7e,0x00,0x00,0x00, 26 0x00,0x00,0x00,0x00,0x3c,0x46,0x06,0x06,0x04,0x08,0x10,0x20,0x7e,0x00,0x00,0x00, 27 0x00,0x00,0x00,0x00,0x7c,0x06,0x06,0x04,0x3c,0x02,0x02,0x06,0x7c,0x00,0x00,0x00, 28 0x00,0x00,0x00,0x00,0x0c,0x1c,0x14,0x24,0x64,0x44,0xff,0x04,0x04,0x00,0x00,0x00, 29 0x00,0x00,0x00,0x00,0x7e,0x60,0x60,0x60,0x7e,0x02,0x02,0x06,0x7c,0x00,0x00,0x00, 30 0x00,0x00,0x00,0x00,0x1e,0x30,0x60,0x48,0x76,0x42,0x42,0x62,0x3c,0x00,0x00,0x00, 31 0x00,0x00,0x00,0x00,0x7e,0x02,0x06,0x04,0x0c,0x08,0x18,0x10,0x30,0x00,0x00,0x00, 32 0x00,0x00,0x00,0x00,0x3c,0x62,0x42,0x36,0x1c,0x66,0x42,0x42,0x3c,0x00,0x00,0x00, 33 0x00,0x00,0x00,0x00,0x3c,0x66,0x42,0x42,0x66,0x1a,0x02,0x04,0x78,0x00,0x00,0x00, 34 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00, 35 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x08,0x30,0x00, 36 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x18,0x30,0x60,0x10,0x0c,0x06,0x00,0x00,0x00, 37 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x7e,0x00,0x00,0x00,0x00,0x00, 38 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x18,0x04,0x06,0x0c,0x10,0x20,0x00,0x00,0x00, 39 0x00,0x00,0x00,0x30,0x1c,0x06,0x06,0x06,0x18,0x10,0x00,0x10,0x10,0x00,0x00,0x00, 40 0x00,0x00,0x00,0x1c,0x22,0x41,0x41,0xdd,0xb5,0xa5,0xa5,0xaf,0x94,0xc0,0x40,0x3c, 41 0x00,0x00,0x00,0x00,0x18,0x1c,0x34,0x24,0x26,0x62,0x7e,0x43,0xc1,0x00,0x00,0x00, 42 0x00,0x00,0x00,0x00,0x7c,0x46,0x42,0x46,0x7c,0x42,0x42,0x42,0x7c,0x00,0x00,0x00, 43 0x00,0x00,0x00,0x00,0x1e,0x20,0x40,0x40,0x40,0x40,0x40,0x60,0x3e,0x00,0x00,0x00, 44 0x00,0x00,0x00,0x00,0x7c,0x46,0x42,0x43,0x43,0x43,0x42,0x46,0x78,0x00,0x00,0x00, 45 0x00,0x00,0x00,0x00,0x7e,0x60,0x60,0x60,0x7e,0x60,0x60,0x60,0x7e,0x00,0x00,0x00, 46 0x00,0x00,0x00,0x00,0x7e,0x60,0x60,0x60,0x7e,0x60,0x60,0x60,0x60,0x00,0x00,0x00, 47 0x00,0x00,0x00,0x00,0x1e,0x60,0x40,0x40,0xce,0x42,0x42,0x62,0x3e,0x00,0x00,0x00, 48 0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x42,0x7e,0x42,0x42,0x42,0x42,0x00,0x00,0x00, 49 0x00,0x00,0x00,0x00,0x7e,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,0x00, 50 0x00,0x00,0x00,0x00,0x7c,0x04,0x04,0x04,0x04,0x04,0x04,0x44,0x78,0x00,0x00,0x00, 51 0x00,0x00,0x00,0x00,0x42,0x44,0x48,0x50,0x70,0x58,0x4c,0x44,0x42,0x00,0x00,0x00, 52 0x00,0x00,0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3e,0x00,0x00,0x00, 53 0x00,0x00,0x00,0x00,0x62,0x66,0x67,0x5f,0x5b,0x5b,0xc1,0xc1,0xc1,0x00,0x00,0x00, 54 0x00,0x00,0x00,0x00,0x62,0x62,0x72,0x52,0x5a,0x4a,0x4e,0x46,0x46,0x00,0x00,0x00, 55 0x00,0x00,0x00,0x00,0x3c,0x62,0x43,0xc3,0xc3,0xc3,0x43,0x62,0x3c,0x00,0x00,0x00, 56 0x00,0x00,0x00,0x00,0x7c,0x46,0x42,0x42,0x46,0x78,0x40,0x40,0x40,0x00,0x00,0x00, 57 0x00,0x00,0x00,0x00,0x3c,0x62,0x43,0xc3,0xc3,0xc3,0x43,0x62,0x3c,0x18,0x0f,0x00, 58 0x00,0x00,0x00,0x00,0x7c,0x66,0x62,0x66,0x7c,0x6c,0x64,0x66,0x62,0x00,0x00,0x00, 59 0x00,0x00,0x00,0x00,0x3e,0x60,0x40,0x60,0x1c,0x06,0x02,0x02,0x7c,0x00,0x00,0x00, 60 0x00,0x00,0x00,0x00,0x7f,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00, 61 0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x62,0x3c,0x00,0x00,0x00, 62 0x00,0x00,0x00,0x00,0xc1,0x43,0x42,0x62,0x26,0x24,0x34,0x1c,0x18,0x00,0x00,0x00, 63 0x00,0x00,0x00,0x00,0xc1,0xc1,0x41,0x49,0x5b,0x5b,0x76,0x66,0x66,0x00,0x00,0x00, 64 0x00,0x00,0x00,0x00,0x43,0x66,0x34,0x18,0x18,0x1c,0x24,0x66,0xc3,0x00,0x00,0x00, 65 0x00,0x00,0x00,0x00,0xc1,0x42,0x66,0x34,0x1c,0x18,0x18,0x18,0x18,0x00,0x00,0x00, 66 0x00,0x00,0x00,0x00,0x7e,0x02,0x04,0x0c,0x18,0x10,0x20,0x60,0x7e,0x00,0x00,0x00, 67 0x00,0x00,0x00,0x1c,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1c, 68 0x00,0x00,0x00,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x0c,0x04,0x06,0x02,0x00,0x00, 69 0x00,0x00,0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c, 70 0x00,0x00,0x00,0x00,0x18,0x1c,0x24,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 71 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff, 72 0x00,0x00,0x00,0x20,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 73 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x06,0x02,0x3e,0x42,0x46,0x7a,0x00,0x00,0x00, 74 0x00,0x00,0x00,0x40,0x40,0x40,0x5c,0x62,0x42,0x42,0x42,0x42,0x7c,0x00,0x00,0x00, 75 0x00,0x00,0x00,0x00,0x00,0x00,0x1e,0x20,0x60,0x40,0x60,0x20,0x3e,0x00,0x00,0x00, 76 0x00,0x00,0x00,0x02,0x02,0x02,0x3e,0x62,0x42,0x42,0x42,0x66,0x3a,0x00,0x00,0x00, 77 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x62,0x42,0x7e,0x40,0x60,0x3e,0x00,0x00,0x00, 78 0x00,0x00,0x00,0x0f,0x18,0x10,0x10,0x7e,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x00, 79 0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x66,0x42,0x66,0x58,0x40,0x3e,0x43,0x42,0x3c, 80 0x00,0x00,0x00,0x40,0x40,0x40,0x5c,0x62,0x42,0x42,0x42,0x42,0x42,0x00,0x00,0x00, 81 0x00,0x00,0x00,0x18,0x18,0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x7e,0x00,0x00,0x00, 82 0x00,0x00,0x00,0x04,0x0c,0x00,0x7c,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x0c,0x78, 83 0x00,0x00,0x00,0x60,0x60,0x60,0x62,0x6c,0x78,0x70,0x68,0x64,0x62,0x00,0x00,0x00, 84 0x00,0x00,0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x7e,0x00,0x00,0x00, 85 0x00,0x00,0x00,0x00,0x00,0x00,0x76,0x4b,0x4b,0x4b,0x4b,0x4b,0x4b,0x00,0x00,0x00, 86 0x00,0x00,0x00,0x00,0x00,0x00,0x5c,0x62,0x42,0x42,0x42,0x42,0x42,0x00,0x00,0x00, 87 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x62,0x42,0x43,0x42,0x62,0x3c,0x00,0x00,0x00, 88 0x00,0x00,0x00,0x00,0x00,0x00,0x5c,0x62,0x42,0x42,0x42,0x42,0x7c,0x40,0x40,0x40, 89 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x62,0x42,0x42,0x42,0x66,0x3a,0x02,0x02,0x02, 90 0x00,0x00,0x00,0x00,0x00,0x00,0x6e,0x72,0x63,0x60,0x60,0x60,0x60,0x00,0x00,0x00, 91 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x20,0x20,0x3c,0x06,0x02,0x7c,0x00,0x00,0x00, 92 0x00,0x00,0x00,0x00,0x10,0x10,0xfe,0x10,0x10,0x10,0x10,0x10,0x1e,0x00,0x00,0x00, 93 0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x42,0x42,0x66,0x3a,0x00,0x00,0x00, 94 0x00,0x00,0x00,0x00,0x00,0x00,0x43,0x42,0x66,0x24,0x34,0x18,0x18,0x00,0x00,0x00, 95 0x00,0x00,0x00,0x00,0x00,0x00,0xc1,0xc1,0x5b,0x5a,0x5e,0x66,0x66,0x00,0x00,0x00, 96 0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x26,0x1c,0x18,0x1c,0x26,0x62,0x00,0x00,0x00, 97 0x00,0x00,0x00,0x00,0x00,0x00,0x43,0x42,0x66,0x24,0x34,0x1c,0x18,0x18,0x30,0xe0, 98 0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x06,0x0c,0x18,0x10,0x20,0x7e,0x00,0x00,0x00, 99 0x00,0x00,0x00,0x0e,0x18,0x10,0x10,0x10,0x30,0x70,0x10,0x10,0x10,0x10,0x18,0x0e, 100 0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08, 101 0x00,0x00,0x00,0x30,0x18,0x08,0x08,0x08,0x0c,0x0e,0x08,0x08,0x08,0x08,0x18,0x30, 102 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x71,0x4b,0x06,0x00,0x00,0x00,0x00,0x00, 103 }; 104 105 106 107 //LCD相关GPIO PIN初始化函数 108 void user_LCD_GPIO_Config(void) 109 { 110 GPIO_InitTypeDef LCD_GPIO_Config; 111 112 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); 113 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); 114 115 LCD_GPIO_Config.GPIO_Mode = GPIO_Mode_AF_PP; 116 LCD_GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz; 117 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_14; 118 GPIO_Init(GPIOD, &LCD_GPIO_Config); 119 120 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_15; 121 GPIO_Init(GPIOD, &LCD_GPIO_Config); 122 123 124 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_0; 125 GPIO_Init(GPIOD, &LCD_GPIO_Config); 126 127 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_1; 128 GPIO_Init(GPIOD, &LCD_GPIO_Config); 129 130 131 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_7; 132 GPIO_Init(GPIOE, &LCD_GPIO_Config); 133 134 135 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_8; 136 GPIO_Init(GPIOE, &LCD_GPIO_Config); 137 138 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_9; 139 GPIO_Init(GPIOE, &LCD_GPIO_Config); 140 141 142 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_10; 143 GPIO_Init(GPIOE, &LCD_GPIO_Config); 144 145 146 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_11; 147 GPIO_Init(GPIOE, &LCD_GPIO_Config); 148 149 150 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_12; 151 GPIO_Init(GPIOE, &LCD_GPIO_Config); 152 153 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_13; 154 GPIO_Init(GPIOE, &LCD_GPIO_Config); 155 156 157 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_14; 158 GPIO_Init(GPIOE, &LCD_GPIO_Config); 159 160 161 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_15; 162 GPIO_Init(GPIOE, &LCD_GPIO_Config); 163 164 165 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_8; 166 GPIO_Init(GPIOD, &LCD_GPIO_Config); 167 168 169 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_9; 170 GPIO_Init(GPIOD, &LCD_GPIO_Config); 171 172 173 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_10; 174 GPIO_Init(GPIOD, &LCD_GPIO_Config); 175 176 177 178 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_7; 179 GPIO_Init(GPIOD, &LCD_GPIO_Config); 180 181 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_4; 182 GPIO_Init(GPIOD, &LCD_GPIO_Config); 183 184 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_5; 185 GPIO_Init(GPIOD, &LCD_GPIO_Config); 186 187 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_11; 188 GPIO_Init(GPIOD, &LCD_GPIO_Config); 189 190 191 LCD_GPIO_Config.GPIO_Mode = GPIO_Mode_Out_PP; 192 LCD_GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz; 193 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_1; 194 GPIO_Init(GPIOE, &LCD_GPIO_Config); 195 196 LCD_GPIO_Config.GPIO_Pin = GPIO_Pin_12; 197 GPIO_Init(GPIOD, &LCD_GPIO_Config); 198 199 user_LCD_RST(ENABLE); 200 user_LCD_RST(DISABLE); 201 202 user_LCD_BL(ENABLE); 203 204 205 } 206 207 //LCD复位信号控制函数 208 void user_LCD_RST(FunctionalState Status) 209 { 210 211 if(Status == DISABLE) 212 { 213 GPIOE->BSRR = GPIO_Pin_1; 214 } 215 216 else 217 { 218 GPIOE->BRR = GPIO_Pin_1; 219 } 220 221 222 } 223 224 //LCD背光信号控制函数 225 void user_LCD_BL(FunctionalState Status) 226 { 227 if(Status == DISABLE) 228 { 229 GPIOD->BSRR = GPIO_Pin_12; 230 } 231 232 else 233 { 234 GPIOD->BRR = GPIO_Pin_12; 235 } 236 237 } 238 239 //FSMC配置函数 240 void user_FSMC_Config(void) 241 { 242 FSMC_NORSRAMTimingInitTypeDef LCD_FSMC_Timing; 243 FSMC_NORSRAMInitTypeDef LCD_FSMC_Config; 244 245 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 246 247 LCD_FSMC_Timing.FSMC_AccessMode = FSMC_AccessMode_B; 248 LCD_FSMC_Timing.FSMC_AddressHoldTime = 0x00; 249 LCD_FSMC_Timing.FSMC_AddressSetupTime = 0x01; 250 LCD_FSMC_Timing.FSMC_BusTurnAroundDuration = 0x00; 251 LCD_FSMC_Timing.FSMC_CLKDivision = 0x00; 252 LCD_FSMC_Timing.FSMC_DataLatency = 0x00; 253 LCD_FSMC_Timing.FSMC_DataLatency = 0x00; 254 LCD_FSMC_Timing.FSMC_DataSetupTime = 0x04; 255 256 257 LCD_FSMC_Config.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; 258 LCD_FSMC_Config.FSMC_Bank = FSMC_Bank1_NORSRAM1; 259 LCD_FSMC_Config.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; 260 LCD_FSMC_Config.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; 261 LCD_FSMC_Config.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; 262 LCD_FSMC_Config.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; 263 LCD_FSMC_Config.FSMC_MemoryType = FSMC_MemoryType_NOR; 264 LCD_FSMC_Config.FSMC_ReadWriteTimingStruct = &LCD_FSMC_Timing; 265 LCD_FSMC_Config.FSMC_WaitSignal = FSMC_WaitSignal_Disable; 266 LCD_FSMC_Config.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; 267 LCD_FSMC_Config.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; 268 LCD_FSMC_Config.FSMC_WrapMode = FSMC_WrapMode_Disable; 269 LCD_FSMC_Config.FSMC_WriteBurst = FSMC_WriteBurst_Disable; 270 LCD_FSMC_Config.FSMC_WriteOperation = FSMC_WriteOperation_Enable; 271 LCD_FSMC_Config.FSMC_WriteTimingStruct = &LCD_FSMC_Timing; 272 273 FSMC_NORSRAMInit(&LCD_FSMC_Config); 274 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); 275 276 277 278 } 279 280 //LCD对应的IC控制寄存器配置函数,这个配置与LCD上控制芯片相关,如何配置应该是向供应商获取的 281 void user_LCD_REG_Config(void) 282 { 283 /* Power control B (CFh) */ 284 285 user_LCD_CMD( 0xCF ); 286 user_LCD_DATA ( 0x00 ); 287 user_LCD_DATA ( 0x81 ); 288 user_LCD_DATA ( 0x30 ); 289 290 /* Power on sequence control (EDh) */ 291 292 user_LCD_CMD ( 0xED ); 293 user_LCD_DATA ( 0x64 ); 294 user_LCD_DATA ( 0x03 ); 295 user_LCD_DATA ( 0x12 ); 296 user_LCD_DATA ( 0x81 ); 297 298 /* Driver timing control A (E8h) */ 299 300 user_LCD_CMD ( 0xE8 ); 301 user_LCD_DATA ( 0x85 ); 302 user_LCD_DATA ( 0x10 ); 303 user_LCD_DATA ( 0x78 ); 304 305 /* Power control A (CBh) */ 306 307 user_LCD_CMD ( 0xCB ); 308 user_LCD_DATA ( 0x39 ); 309 user_LCD_DATA ( 0x2C ); 310 user_LCD_DATA ( 0x00 ); 311 user_LCD_DATA ( 0x34 ); 312 user_LCD_DATA ( 0x02 ); 313 314 /* Pump ratio control (F7h) */ 315 316 user_LCD_CMD ( 0xF7 ); 317 user_LCD_DATA ( 0x20 ); 318 319 /* Driver timing control B */ 320 321 user_LCD_CMD ( 0xEA ); 322 user_LCD_DATA ( 0x00 ); 323 user_LCD_DATA ( 0x00 ); 324 325 /* Frame Rate Control (In Normal Mode/Full Colors) (B1h) */ 326 327 user_LCD_CMD ( 0xB1 ); 328 user_LCD_DATA ( 0x00 ); 329 user_LCD_DATA ( 0x1B ); 330 331 /* Display Function Control (B6h) */ 332 333 user_LCD_CMD ( 0xB6 ); 334 user_LCD_DATA ( 0x0A ); 335 user_LCD_DATA ( 0xA2 ); 336 337 /* Power Control 1 (C0h) */ 338 339 user_LCD_CMD ( 0xC0 ); 340 user_LCD_DATA ( 0x35 ); 341 342 /* Power Control 2 (C1h) */ 343 344 user_LCD_CMD( 0xC1 ); 345 user_LCD_DATA( 0x11 ); 346 347 /* VCOM Control 1 (C5h) */ 348 user_LCD_CMD( 0xC5 ); 349 user_LCD_DATA( 0x45 ); 350 user_LCD_DATA( 0x45 ); 351 352 /* VCOM Control 2 (C7h) */ 353 user_LCD_CMD ( 0xC7 ); 354 user_LCD_DATA ( 0xA2 ); 355 356 /* Enable 3G (F2h) */ 357 user_LCD_CMD ( 0xF2 ); 358 user_LCD_DATA ( 0x00 ); 359 360 /* Gamma Set (26h) */ 361 user_LCD_CMD ( 0x26 ); 362 user_LCD_DATA ( 0x01 ); 363 364 365 /* Positive Gamma Correction */ 366 user_LCD_CMD ( 0xE0 ); //Set Gamma 367 user_LCD_DATA ( 0x0F ); 368 user_LCD_DATA ( 0x26 ); 369 user_LCD_DATA ( 0x24 ); 370 user_LCD_DATA ( 0x0B ); 371 user_LCD_DATA ( 0x0E ); 372 user_LCD_DATA ( 0x09 ); 373 user_LCD_DATA ( 0x54 ); 374 user_LCD_DATA ( 0xA8 ); 375 user_LCD_DATA ( 0x46 ); 376 user_LCD_DATA ( 0x0C ); 377 user_LCD_DATA ( 0x17 ); 378 user_LCD_DATA ( 0x09 ); 379 user_LCD_DATA ( 0x0F ); 380 user_LCD_DATA ( 0x07 ); 381 user_LCD_DATA ( 0x00 ); 382 383 /* Negative Gamma Correction (E1h) */ 384 user_LCD_CMD ( 0XE1 ); //Set Gamma 385 user_LCD_DATA ( 0x00 ); 386 user_LCD_DATA ( 0x19 ); 387 user_LCD_DATA ( 0x1B ); 388 user_LCD_DATA ( 0x04 ); 389 user_LCD_DATA ( 0x10 ); 390 user_LCD_DATA ( 0x07 ); 391 user_LCD_DATA ( 0x2A ); 392 user_LCD_DATA ( 0x47 ); 393 user_LCD_DATA ( 0x39 ); 394 user_LCD_DATA ( 0x03 ); 395 user_LCD_DATA ( 0x06 ); 396 user_LCD_DATA ( 0x06 ); 397 user_LCD_DATA ( 0x30 ); 398 user_LCD_DATA ( 0x38 ); 399 user_LCD_DATA ( 0x0F ); 400 401 /* memory access control set */ 402 403 user_LCD_CMD ( 0x36 ); 404 user_LCD_DATA ( 0xC8 ); /*���� ���Ͻǵ� (���)�����½� (�յ�)ɨ�跽ʽ*/ 405 406 407 /* column address control set */ 408 user_LCD_CMD ( 0x2A ); 409 user_LCD_DATA ( 0x00 ); 410 user_LCD_DATA ( 0x00 ); 411 user_LCD_DATA ( 0x00 ); 412 user_LCD_DATA ( 0xEF ); 413 414 /* page address control set */ 415 416 user_LCD_CMD ( 0x2B ); 417 user_LCD_DATA ( 0x00 ); 418 user_LCD_DATA ( 0x00 ); 419 user_LCD_DATA ( 0x01 ); 420 user_LCD_DATA ( 0x3F ); 421 422 /* Pixel Format Set (3Ah) */ 423 424 user_LCD_CMD ( 0x3a ); 425 user_LCD_DATA ( 0x55 ); 426 427 /* Sleep Out (11h) */ 428 user_LCD_CMD ( 0x11 ); 429 //ILI9341_Delay ( 0xAFFf<<2 ); 430 431 432 /* Display ON (29h) */ 433 user_LCD_CMD ( 0x29 ); 434 435 436 437 } 438 439 //LCD命令操作函数 440 void user_LCD_CMD(uint16_t cmd) 441 { 442 *(volatile uint16_t *) (LCD_CMD) = cmd; 443 444 } 445 446 //LCD数据写入函数 447 void user_LCD_DATA(uint16_t data) 448 { 449 *(volatile uint16_t *) (LCD_DATA) = data; 450 451 } 452 453 454 455 //LCD扫描函数,这里配置为模式六,即X--240, Y--320,X从左到右,Y从上到下扫描 456 void user_LCD_Scan(void) 457 { 458 459 user_LCD_CMD(0x36); 460 //user_LCD_DATA(0x08 | (6 << 5)); 461 user_LCD_CMD(0x68); 462 463 user_LCD_CMD(0x2A); 464 user_LCD_DATA(0x00); 465 user_LCD_DATA(0x00); 466 user_LCD_DATA(((240 - 1) >> 8)&0xFF); //因为从0开始,所以需要-1 467 user_LCD_DATA((240 - 1) &0xFF); 468 469 470 471 user_LCD_CMD(0x2B); 472 user_LCD_DATA(0x00); 473 user_LCD_DATA(0x00); 474 user_LCD_DATA(((320 - 1) >> 8)&0xFF); 475 user_LCD_DATA((320 - 1) &0xFF); 476 477 // user_LCD_CMD(0x2C); //这条语句后面没有跟填充的数据,去掉也不影响,这里应该是多余的 478 479 480 481 } 482 483 //LCD初始化函数 484 void user_LCD_Init(void) 485 { 486 user_LCD_GPIO_Config(); 487 user_FSMC_Config(); 488 489 user_LCD_REG_Config(); 490 491 user_LCD_Scan(); 492 } 493 494 495 //初始化整个屏幕 496 void user_View_Window(uint16_t lcd_x, uint16_t lcd_y, uint16_t lcd_width, uint16_t lcd_height) 497 { 498 499 user_LCD_CMD(0x2A); //行初始化命令 500 user_LCD_DATA(lcd_x >> 8); //行起始点坐标,先给高8bit 501 user_LCD_DATA(lcd_x & 0xFF); 502 user_LCD_DATA((lcd_x + lcd_width - 1) >> 8 ); //行结束点坐标,先给高8bit 503 user_LCD_DATA((lcd_x + lcd_width -1) & 0xFF); 504 505 506 user_LCD_CMD(0x2B); //列初始化命令 507 user_LCD_DATA(lcd_y >> 8); //列起始点坐标,先给高8bit 508 user_LCD_DATA(lcd_y & 0xFF); 509 user_LCD_DATA((lcd_y + lcd_height -1 ) >> 8); //列结束点坐标,先给高8bit 510 user_LCD_DATA((lcd_y + lcd_height -1 ) & 0xFF); 511 512 } 513 514 //单个字符显示函数 515 void user_lcd_display_char(uint16_t x, uint16_t y, char ch) 516 { 517 uint16_t a,b,c,d; 518 519 uint8_t * pfont; 520 521 a = ch - ' '; 522 b = 16; 523 524 pfont = (uint8_t *)&ASCII8sx16_Table[a * b]; //找到字模开始的位置,一个字的字模占16个字节 525 526 user_View_Window(x, y, 8, 16); //显示单个字模所需屏幕尺寸 527 528 user_LCD_CMD(0x2C); //填充像素命令 529 530 for(c = 0; c < 16; c++) 531 { 532 for(d = 0; d < 8; d++) 533 { 534 if(*pfont & (0x80 >> d)) 535 { 536 user_LCD_DATA(TEXTCOLOR); 537 } 538 else 539 { 540 user_LCD_DATA(BACKGROUND); 541 } 542 543 } 544 pfont++; 545 } 546 547 } 548 549 550 551 //清屏函数,将屏幕配置为黑色 552 void user_lcd_clear(void) 553 { 554 user_View_Window(0, 0, 240, 320); 555 user_lcd_fill_color(240*320, BACKGROUND); 556 557 558 } 559 560 //整个屏幕填充的颜色函数 561 void user_lcd_fill_color(uint32_t fill_lcd, uint16_t color) 562 { 563 uint32_t i; 564 565 user_LCD_CMD(0x2C); 566 for(i = 0; i < fill_lcd; i++) 567 { 568 user_LCD_DATA(color); 569 570 } 571 572 } 573 574 //显示英文字符串函数 575 void user_lcd_display_string(char * pchar) 576 { 577 uint16_t x = 0,y = 0; 578 579 while(*pchar != '\0') 580 { 581 582 if((x + 8) > 240) 583 { 584 x = 0; 585 y += 16; 586 } 587 if((y + 16) > 320) 588 { 589 x = 0; 590 y = 0; 591 } 592 593 user_lcd_display_char(x, y, *pchar); 594 595 pchar++; 596 x +=8; 597 598 599 } 600 601 602 } 603 604 //时间延时函数 605 void time_delay(volatile uint32_t t) 606 { 607 uint32_t time = t; 608 uint32_t i; 609 610 for(i = 0; i < time; i++) 611 { 612 613 } 614 615 } 616 617 618 619 //单个中文字符显示函数 620 void user_lcd_display_char_CN(uint16_t x, uint16_t y, uint16_t ch) 621 { 622 uint16_t a,c,d; 623 624 uint8_t buffer[16*2]; 625 626 user_View_Window(x, y, 16, 16); //显示单个字模所需屏幕尺寸 627 628 user_LCD_CMD(0x2C); //填充像素命令 629 630 user_Get_CHAR_CN(buffer, ch); //找到字模开始的位置,一个字的字模占16个字节 631 632 633 634 for(c = 0; c < 16; c++) 635 { 636 637 a = buffer[c * 2]; 638 a = (a << 8); 639 a |= buffer[c * 2 + 1]; 640 641 for(d = 0; d < 16; d++) 642 { 643 if(a & (0x8000 >> d)) 644 { 645 user_LCD_DATA(TEXTCOLOR); 646 } 647 else 648 { 649 user_LCD_DATA(BACKGROUND); 650 } 651 652 } 653 654 } 655 656 657 } 658 659 660 //显示中英文字符串函数 661 void user_lcd_display_string_EN_CN(char * pchar) 662 { 663 uint16_t x = 0,y = 0; 664 uint16_t cn_char; 665 666 while(*pchar != '\0') 667 { 668 669 if(*pchar <= 126) //获取英文字模 670 { 671 if((x + 8) > 240) 672 { 673 x = 0; 674 y += 16; 675 } 676 if((y + 16) > 320) 677 { 678 x = 0; 679 y = 0; 680 } 681 682 user_lcd_display_char(x, y, *pchar); 683 684 pchar++; 685 x +=8; 686 687 } 688 else //获取中文字模 689 { 690 691 if((x + 16) > 240) 692 { 693 x = 0; 694 y += 16; 695 } 696 if((y + 16) > 320) 697 { 698 x = 0; 699 y = 0; 700 } 701 702 cn_char = *(uint16_t *)pchar; 703 cn_char = (cn_char << 8) + (cn_char >> 8); 704 705 user_lcd_display_char_CN(x, y, cn_char); 706 707 pchar += 2; //因为GB2312编码使用两个字节,所以此这里要加2,用来指向下一个汉字 708 x +=16; 709 710 711 } 712 713 714 715 } 716 717 718 } 719 720 721 722 //获取汉字索引函数,字模已经固化在Flash中了,所以这里直接找到地址获取即可,具体如何自己烧录,目前没有研究 723 void user_Get_CHAR_CN(uint8_t * pchar, uint16_t ch) 724 { 725 uint16_t CN_Ch_h8,CN_Ch_l8; 726 uint32_t CN_Ch_Location; //此处类型要么是uint32_t要么是unsigned int类型,不然会超出数值范围而溢出,导致后面的计算出错 727 unsigned char High8bit,Low8bit; 728 729 static uint8_t Flash_Init_Flag = 0; //有个很操蛋的问题,刚接触KEIL5的时候就发行了,就是定义并初始化变量一定要放到正式语句前面,不然 730 //KEIL5的编译器会报错,即声明变量一定要放在一个函数开始的最前面,这个是编译器做的很不好的地方 731 732 733 if(Flash_Init_Flag == 0) 734 { 735 user_SPI_GPIO_Config(); 736 user_SPI_Config(); 737 738 Flash_Init_Flag = 1; 739 } 740 741 CN_Ch_h8 = ch >> 8; 742 CN_Ch_l8 = ch & 0x00FF; 743 744 CN_Ch_Location = ((CN_Ch_h8-0xa1)*94+CN_Ch_l8-0xa1) * 16*2; 745 746 747 user_SPI_Flash_Read_NData(pchar, FONT_ADDRESS + CN_Ch_Location, 16*2); 748 749 750 }
6. 在main.c中添加如下代码
1 #include "stm32f10x.h" 2 #include "user_fsmc_lcd.h" 3 4 5 6 int main(void) 7 { 8 user_LCD_Init(); 9 user_LCD_Scan(); 10 11 while(1) 12 { 13 user_lcd_clear(); //清屏,将屏幕置为黑色背景 14 15 /* 16 这里需要注意的是,编写要显示中文时,最好不要使用SourceInsight来写,不然会因为保存的时候 17 因为编码不一样,导致KEIL5编译程序后,LCD显示的字符为乱码,如果实在需要用SourceInsigt来编写 18 ,通过先建立KEIL5工程,将各文件加载好后,再使用SourceInsight来编写,可能会避免这个问题 19 */ 20 21 user_lcd_display_string_EN_CN("It's Test:中英文显示功能"); 22 23 24 time_delay(0xFFFFFF); //延时,等待一定时间屏幕会出现闪烁的现象 25 26 } 27 28 }
总结:
1. 编写要显示中文时,最好不要使用SourceInsight来写,不然会因为保存的时候因为编码不一样,导致KEIL5编译程序后,LCD显示的字符为乱码,如果实在需要用SourceInsigt来编写,通过先建立KEIL5工程,将各文件加载好后,再使用SourceInsight来编写,可能会避免这个问题
2. 当程序出现问题不能正常工作时,通过OK代码(野火的代码)来一步一步替换查找自己的代码异常的地方,可以快速定位问题。
本实验代码:
链接:https://pan.baidu.com/s/1KHHZsUdJMMXccxCljUXYyA
提取码:vc5u