STM32之FreeRTOS学习笔记
- 简介
- 1. CubeMX创建FreeRTOS工程模板
- 1.1 跑马灯验证移植模板
- 2. 移植正点原子精英板TFTLCD程序代码
- 2.1 FSMC的配置
- 2.2 LCD的驱动及功能函数的移植
- 3. 移植正点原子SPI驱动W25Q128
- 3.1 CubeMX配置SPI
- 3.2 W25Q128程序移植
简介
学习的硬件平台是基于正点原子的精英板。
1. CubeMX创建FreeRTOS工程模板
选择芯片配置时钟及调试接口这种基本操作就不说了,直接从开始配置FreeRTOS工程模板说起。
① 首先点击最下方的FreeRTOS选项
② 点击v2版本,v1和v2有啥区别我不知道,可以自己试试
经过如上操作,FreeRTOS的工程模板就已经创建好了。
注意:如果使用FreeRTOS的话,它也是和HAL库一样用的sysTick作为系统时基,因此我们需要将HAL库的时基换一下,这里我使用的是定时器1。
1.1 跑马灯验证移植模板
现在我们可以验证一下我们的工程模板是否有问题,我们先玩个跑马灯,还是在CubeMX上配置任务,步骤如下:
第三步的操作是进行任务添加,设置任务名称任务堆栈任务优先级之类的,添加好了之后直接点击生成代码即可。
生成完毕之后打开MDK工程在文件freertos.c文件中的任务函数里添加如下代码(注意,是找到自己在CubeMX配置的任务函数名字,将函数的内容添加进去,不是直接将整个任务函数复制粘贴进去):
void Fun_LED1(void *argument)
{
/* USER CODE BEGIN Fun_LED1 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
osDelay(50);
HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
osDelay(50);
}
/* USER CODE END Fun_LED1 */
}
void Fun_LED2(void *argument)
{
/* USER CODE BEGIN Fun_LED2 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
osDelay(50);
HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
osDelay(50);
}
/* USER CODE END Fun_LED2 */
}
经过上面的例程,可以发现两个灯都是同时亮同时灭,看起来像是同一时刻执行两个任务(其实不是),示波器检测两个引脚的波形也是一致的,移植成功。
2. 移植正点原子精英板TFTLCD程序代码
正点原子的例程都是根据标准库写的,在我们使用CubeMX工具配置的话就得需要经过一个移植来完成对LCD的控制。通讯时序我采用的是FSMC模拟产生8080时序来完成,主要分为两部分:
①:FSMC的配置
②:LCD的驱动及功能代码的移植
2.1 FSMC的配置
具体的FSMC配置如下:
同一器件,FSMC可以挂载4个,用片选引脚分开,这里我们选用块1的第四个区,也就是第2步所选的内容,采用与NOR FLASH通信的这块地址区域来模拟产生8080时序。这里我们主要关注的是第2步和第4步,因为这两个是根据硬件图来决定的,FSMC的数据线和地址线及其他的信号线都是固定的,因此这个我们不用管,我们需要关注的就是就是我们硬件中采用的是哪个区的那个片选,对应着不同的通信地址。我的板子上片选引脚连接的是PG12,对应FSMC的片选4,因此这里采用片选4。因为LCD屏没有地址线,但是有一个数据命令区分线,因此我们可以将LCD的数据命令区分线连接到其中的一根地址线,这里我的硬件连接是FSMC的A10地址线,对应GPIO的PG0。根据访问不同的地址A10地址线产生的高低电平来决定是给LCD写数据还是写命令(具体是哪个电平写数据哪个电平写命令是根据LCD的控制芯片来决定的)。
主要的地方配置好后就是第6项的数据命令等待时间及模式的配置,数据命令等待时间可以根据LCD时序图来配置,也可以使用经验值,模式我选得A模式。配置好之后就可以去控制我们的LCD了
2.2 LCD的驱动及功能函数的移植
首先是lcd.h文件,代码如下:
#ifndef _LCD_H
#define _LCD_H
#include "stm32f1xx_hal.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"
//定义一些常用的数据类型短关键字
//-----------------------------------------------------------------------------------------------------------------
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s8;
typedef const int32_t sc32;
typedef const int16_t sc16;
typedef const int8_t sc8;
typedef __IO int32_t vs32;
typedef __IO int16_t vs16;
typedef __IO int8_t vs8;
typedef __I int32_t vsc32;
typedef __I int16_t vsc16;
typedef __I int8_t vsc8;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef const uint32_t uc32;
typedef const uint16_t uc16;
typedef const uint8_t uc8;
typedef __IO uint32_t vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t vu8;
typedef __I uint32_t vuc32;
typedef __I uint16_t vuc16;
typedef __I uint8_t vuc8;
//-----------------------------------------------------------------------------------------------------------------
// 代码移植部分
//-----------------------------------------------------------------------------------------------------------------
// 注意:
// FSMC 部分为硬件固定引脚,不可更改
//
// 引脚连接状态:
//
// 可自定义引脚
// LCD_RST------------------------------------nRST LCD 复位引脚
// LCD_BL-------------------------------------PB0 LCD 背光控制引脚,通过三极管控制
//
// FSMC 固定引脚
// FSMC_NE4 -> LCD_CS-------------------------PG12 片选引脚,选择的是块 1 第 4 个区(总共四块,每块4个区)
// FSMC_NOE -> LCD_RD-------------------------PD4 读数据
// FSMC_NWE -> LCD_WR-------------------------PD5 写数据
// FSMC_A10 -> LCD_RS-------------------------PG0 数据/命令选择/地址线
// FSMC_D0 -> LCD_D0-------------------------PD14 数据/命令线 0
// FSMC_D1 -> LCD_D1-------------------------PD15 数据/命令线 1
// FSMC_D2 -> LCD_D2-------------------------PD0 数据/命令线 2
// FSMC_D3 -> LCD_D3-------------------------PD1 数据/命令线 3
// FSMC_D4 -> LCD_D4-------------------------PE7 数据/命令线 4
// FSMC_D5 -> LCD_D5-------------------------PE8 数据/命令线 5
// FSMC_D6 -> LCD_D6-------------------------PE9 数据/命令线 6
// FSMC_D7 -> LCD_D7-------------------------PE10 数据/命令线 7
// FSMC_D8 -> LCD_D8-------------------------PE11 数据/命令线 8
// FSMC_D9 -> LCD_D9-------------------------PE12 数据/命令线 9
// FSMC_D10 -> LCD_D10------------------------PE13 数据/命令线 10
// FSMC_D11 -> LCD_D11------------------------PE14 数据/命令线 11
// FSMC_D12 -> LCD_D12------------------------PE15 数据/命令线 12
// FSMC_D13 -> LCD_D13------------------------PD8 数据/命令线 13
// FSMC_D14 -> LCD_D14------------------------PD9 数据/命令线 14
// FSMC_D15 -> LCD_D15------------------------PD10 数据/命令线 15
//----------------------------------------------------------------------------------------------------------------
// TFTLCD 数据命令选择
//
// 由于 TFTLCD 没有地址线,但是有一条 LCD_RS 线,将
// 其连接在其中一条地址线上,通过访问不同的地址改变
// LCD_RS 引脚的高低电平来决定是数据与命令
//
// 注意:
// STM32 FSMC 访问外部 SRAM 地址时,会自动将地址右移一位对齐
//-----------------------------------
typedef struct
{
vu16 LCD_Cmd;
vu16 LCD_Data;
}LCD_TypeDef;
#define LCD_BASE ( (u32)(0x6C000000 | 0x000007FE) )
#define LCD ( (LCD_TypeDef *) LCD_BASE )
//-----------------------------------------------------------------------------------------------------------------
// TFTLCD 背光
// 1:开背光
// 0:关背光
//-----------------------------------
#define ON 1
#define OFF 0
#define LCD_BL(ONorOFF) if(ONorOFF) \
GPIOB->ODR |= 0x01; \
else \
GPIOB->ODR &= 0xFE;
//-----------------------------------------------------------------------------------------------------------------
// TFTLCD 读写方向
//-----------------------------------
#define L2R_U2D 0
#define L2R_D2U 1
#define R2L_U2D 2
#define R2L_D2U 3
#define U2D_L2R 4
#define U2D_R2L 5
#define D2U_L2R 6
#define D2U_R2L 7
//-----------------------------------------------------------------------------------------------------------------
// 笔画颜色
//-----------------------------------
#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define BRED 0XF81F
#define GRED 0XFFE0
#define GBLUE 0X07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define BROWN 0XBC40 //棕色
#define BRRED 0XFC07 //棕红色
#define GRAY 0X8430 //灰色
//-----------------------------------------------------------------------------------------------------------------
// 液晶驱动芯片常用指令
// 0x36:读写方向
// 0x2A:设置 x 起始坐标
// 0x2B:设置 y 起始坐标
// 0x2C:写 GRAM 指令(设置此指令之后,有效位宽为 16 位)
//-----------------------------------
#define RW_Direction 0x36
#define X_Pos 0x2A
#define Y_Pos 0x2B
#define WR_GRAM 0x2C
//-----------------------------------------------------------------------------------------------------------------
// API函数声明
//-----------------------------------
// 写操作
void LCD_WR_Cmd(u16 cmd);
void LCD_WR_Data(u16 data);
void LCD_WR_GRAM_Cmd(void);
u16 RD_ID(void);
// 读操作
u16 LCD_RD_Cmd(void);
u16 LCD_RD_Data(void);
// 初始化部分(用cubeMX配置之后,初始化此处即可)
void LCD_DriveID_5310_Init(void); // 此函数包含了开背光,设置读写方向和清屏函数
void Set_RW_Direction(u8 direction);
void LCD_Clear(u16 color);
// 功能函数
void Open_Window(u16 Xpos, u16 Ypos, u16 Width, u16 Hight);
void LCD_SetCursor(u16 Xpos, u16 Ypos); // 设置光标
void LCD_DrawPoint(u16 x,u16 y,u16 color); // 画点
void LCD_Fast_DrawPoint(u16 x,u16 y,u16 color); // 快速画点
u16 LCD_ReadPoint(u16 x,u16 y); // 读点
void LCD_Draw_Circle(u16 x0,u16 y0,u8 r,u16 color); // 画圆
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2, u16 color); // 画线
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2, u16 color); // 画矩形
void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color); // 填充单色
void LCD_Color_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 *color); // 填充指定颜色
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode); // 显示一个字符
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size); // 显示一个数字
void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode); // 显示 数字
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p); // 显示一个字符串,12/16字体
//-----------------------------------------------------------------------------------------------------------------
#endif
再就是lcd.c文件,代码如下:
#include "lcd.h"
// TFTLCD 写指令
//-----------------------------------
void LCD_WR_Cmd(u16 cmd)
{
LCD->LCD_Cmd = cmd;
}
//-----------------------------------------------------------------------------------------------------------------
// TFTLCD 写数据
//-----------------------------------
void LCD_WR_Data(u16 data)
{
LCD->LCD_Data = data;
}
//-----------------------------------------------------------------------------------------------------------------
// 开始写 GRAM
//-----------------------------------
void LCD_WR_GRAM_Cmd(void)
{
LCD_WR_Cmd(0x2C);
}
//-----------------------------------------------------------------------------------------------------------------
// TFTLCD 读指令
//-----------------------------------
u16 LCD_RD_Cmd(void)
{
vu16 tmp;
tmp = LCD->LCD_Cmd ;
return tmp;
}
//-----------------------------------------------------------------------------------------------------------------
// TFTLCD 读数据
//-----------------------------------
u16 LCD_RD_Data(void)
{
vu16 tmp;
tmp = LCD->LCD_Data;
return tmp;
}
//-----------------------------------------------------------------------------------------------------------------
// 设置 TFTLCD 读写方向
//-----------------------------------
void Set_RW_Direction(u8 direction)
{
u8 tmp = 0;
switch(direction)
{
case L2R_U2D://从左到右,从上到下
tmp|=(0<<7)|(0<<6)|(0<<5);
break;
case L2R_D2U://从左到右,从下到上
tmp|=(1<<7)|(0<<6)|(0<<5);
break;
case R2L_U2D://从右到左,从上到下
tmp|=(0<<7)|(1<<6)|(0<<5);
break;
case R2L_D2U://从右到左,从下到上
tmp|=(1<<7)|(1<<6)|(0<<5);
break;
case U2D_L2R://从上到下,从左到右
tmp|=(0<<7)|(0<<6)|(1<<5);
break;
case U2D_R2L://从上到下,从右到左
tmp|=(0<<7)|(1<<6)|(1<<5);
break;
case D2U_L2R://从下到上,从左到右
tmp|=(1<<7)|(0<<6)|(1<<5);
break;
case D2U_R2L://从下到上,从右到左
tmp|=(1<<7)|(1<<6)|(1<<5);
break;
}
LCD_WR_Cmd(0x36);
LCD_WR_Data(tmp);
}
//-----------------------------------------------------------------------------------------------------------------
// 清屏函数(适用于 3.5寸屏)
// color:要清屏的填充色
//-----------------------------------------------------------------------------------------------------------------
void LCD_Clear(u16 color)
{
u32 index=0;
Open_Window(0,0,320,480);
LCD_WR_GRAM_Cmd();
for(index = 0; index<320*480; index++)
{
LCD_WR_Data(color);
}
}
//-----------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------
void LCD_DriveID_5310_Init()
{
u32 i;
// ID 为 5310 驱动的初始化序列
LCD_WR_Cmd(0xED);
LCD_WR_Data(0x01);
LCD_WR_Data(0xFE);
LCD_WR_Cmd(0xEE);
LCD_WR_Data(0xDE);
LCD_WR_Data(0x21);
LCD_WR_Cmd(0xF1);
LCD_WR_Data(0x01);
LCD_WR_Cmd(0xDF);
LCD_WR_Data(0x10);
//VCOMvoltage//
LCD_WR_Cmd(0xC4);
LCD_WR_Data(0x8F); //5f
LCD_WR_Cmd(0xC6);
LCD_WR_Data(0x00);
LCD_WR_Data(0xE2);
LCD_WR_Data(0xE2);
LCD_WR_Data(0xE2);
LCD_WR_Cmd(0xBF);
LCD_WR_Data(0xAA);
LCD_WR_Cmd(0xB0);
LCD_WR_Data(0x0D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x0D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x11);
LCD_WR_Data(0x00);
LCD_WR_Data(0x19);
LCD_WR_Data(0x00);
LCD_WR_Data(0x21);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x5D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x5D);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB1);
LCD_WR_Data(0x80);
LCD_WR_Data(0x00);
LCD_WR_Data(0x8B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x96);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB2);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x02);
LCD_WR_Data(0x00);
LCD_WR_Data(0x03);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB3);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB4);
LCD_WR_Data(0x8B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x96);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA1);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB5);
LCD_WR_Data(0x02);
LCD_WR_Data(0x00);
LCD_WR_Data(0x03);
LCD_WR_Data(0x00);
LCD_WR_Data(0x04);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB6);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB7);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3F);
LCD_WR_Data(0x00);
LCD_WR_Data(0x5E);
LCD_WR_Data(0x00);
LCD_WR_Data(0x64);
LCD_WR_Data(0x00);
LCD_WR_Data(0x8C);
LCD_WR_Data(0x00);
LCD_WR_Data(0xAC);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDC);
LCD_WR_Data(0x00);
LCD_WR_Data(0x70);
LCD_WR_Data(0x00);
LCD_WR_Data(0x90);
LCD_WR_Data(0x00);
LCD_WR_Data(0xEB);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDC);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xB8);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xBA);
LCD_WR_Data(0x24);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC1);
LCD_WR_Data(0x20);
LCD_WR_Data(0x00);
LCD_WR_Data(0x54);
LCD_WR_Data(0x00);
LCD_WR_Data(0xFF);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC2);
LCD_WR_Data(0x0A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x04);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC3);
LCD_WR_Data(0x3C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x39);
LCD_WR_Data(0x00);
LCD_WR_Data(0x37);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x36);
LCD_WR_Data(0x00);
LCD_WR_Data(0x32);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2F);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x29);
LCD_WR_Data(0x00);
LCD_WR_Data(0x26);
LCD_WR_Data(0x00);
LCD_WR_Data(0x24);
LCD_WR_Data(0x00);
LCD_WR_Data(0x24);
LCD_WR_Data(0x00);
LCD_WR_Data(0x23);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x36);
LCD_WR_Data(0x00);
LCD_WR_Data(0x32);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2F);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x29);
LCD_WR_Data(0x00);
LCD_WR_Data(0x26);
LCD_WR_Data(0x00);
LCD_WR_Data(0x24);
LCD_WR_Data(0x00);
LCD_WR_Data(0x24);
LCD_WR_Data(0x00);
LCD_WR_Data(0x23);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC4);
LCD_WR_Data(0x62);
LCD_WR_Data(0x00);
LCD_WR_Data(0x05);
LCD_WR_Data(0x00);
LCD_WR_Data(0x84);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF0);
LCD_WR_Data(0x00);
LCD_WR_Data(0x18);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA4);
LCD_WR_Data(0x00);
LCD_WR_Data(0x18);
LCD_WR_Data(0x00);
LCD_WR_Data(0x50);
LCD_WR_Data(0x00);
LCD_WR_Data(0x0C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x17);
LCD_WR_Data(0x00);
LCD_WR_Data(0x95);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Data(0xE6);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC5);
LCD_WR_Data(0x32);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x65);
LCD_WR_Data(0x00);
LCD_WR_Data(0x76);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC6);
LCD_WR_Data(0x20);
LCD_WR_Data(0x00);
LCD_WR_Data(0x17);
LCD_WR_Data(0x00);
LCD_WR_Data(0x01);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC7);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC8);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xC9);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE0);
LCD_WR_Data(0x16);
LCD_WR_Data(0x00);
LCD_WR_Data(0x1C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x21);
LCD_WR_Data(0x00);
LCD_WR_Data(0x36);
LCD_WR_Data(0x00);
LCD_WR_Data(0x46);
LCD_WR_Data(0x00);
LCD_WR_Data(0x52);
LCD_WR_Data(0x00);
LCD_WR_Data(0x64);
LCD_WR_Data(0x00);
LCD_WR_Data(0x7A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x8B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA8);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB9);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC4);
LCD_WR_Data(0x00);
LCD_WR_Data(0xCA);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD9);
LCD_WR_Data(0x00);
LCD_WR_Data(0xE0);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE1);
LCD_WR_Data(0x16);
LCD_WR_Data(0x00);
LCD_WR_Data(0x1C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x22);
LCD_WR_Data(0x00);
LCD_WR_Data(0x36);
LCD_WR_Data(0x00);
LCD_WR_Data(0x45);
LCD_WR_Data(0x00);
LCD_WR_Data(0x52);
LCD_WR_Data(0x00);
LCD_WR_Data(0x64);
LCD_WR_Data(0x00);
LCD_WR_Data(0x7A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x8B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA8);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB9);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC4);
LCD_WR_Data(0x00);
LCD_WR_Data(0xCA);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD8);
LCD_WR_Data(0x00);
LCD_WR_Data(0xE0);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE2);
LCD_WR_Data(0x05);
LCD_WR_Data(0x00);
LCD_WR_Data(0x0B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x1B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x34);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x4F);
LCD_WR_Data(0x00);
LCD_WR_Data(0x61);
LCD_WR_Data(0x00);
LCD_WR_Data(0x79);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Data(0x97);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA6);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB7);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC7);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD1);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD6);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDD);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE3);
LCD_WR_Data(0x05);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA);
LCD_WR_Data(0x00);
LCD_WR_Data(0x1C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x33);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x50);
LCD_WR_Data(0x00);
LCD_WR_Data(0x62);
LCD_WR_Data(0x00);
LCD_WR_Data(0x78);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Data(0x97);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA6);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB7);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC7);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD1);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD5);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDD);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE4);
LCD_WR_Data(0x01);
LCD_WR_Data(0x00);
LCD_WR_Data(0x01);
LCD_WR_Data(0x00);
LCD_WR_Data(0x02);
LCD_WR_Data(0x00);
LCD_WR_Data(0x2A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x4B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x5D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x74);
LCD_WR_Data(0x00);
LCD_WR_Data(0x84);
LCD_WR_Data(0x00);
LCD_WR_Data(0x93);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB3);
LCD_WR_Data(0x00);
LCD_WR_Data(0xBE);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC4);
LCD_WR_Data(0x00);
LCD_WR_Data(0xCD);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD3);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDD);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE5);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x02);
LCD_WR_Data(0x00);
LCD_WR_Data(0x29);
LCD_WR_Data(0x00);
LCD_WR_Data(0x3C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x4B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x5D);
LCD_WR_Data(0x00);
LCD_WR_Data(0x74);
LCD_WR_Data(0x00);
LCD_WR_Data(0x84);
LCD_WR_Data(0x00);
LCD_WR_Data(0x93);
LCD_WR_Data(0x00);
LCD_WR_Data(0xA2);
LCD_WR_Data(0x00);
LCD_WR_Data(0xB3);
LCD_WR_Data(0x00);
LCD_WR_Data(0xBE);
LCD_WR_Data(0x00);
LCD_WR_Data(0xC4);
LCD_WR_Data(0x00);
LCD_WR_Data(0xCD);
LCD_WR_Data(0x00);
LCD_WR_Data(0xD3);
LCD_WR_Data(0x00);
LCD_WR_Data(0xDC);
LCD_WR_Data(0x00);
LCD_WR_Data(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE6);
LCD_WR_Data(0x11);
LCD_WR_Data(0x00);
LCD_WR_Data(0x34);
LCD_WR_Data(0x00);
LCD_WR_Data(0x56);
LCD_WR_Data(0x00);
LCD_WR_Data(0x76);
LCD_WR_Data(0x00);
LCD_WR_Data(0x77);
LCD_WR_Data(0x00);
LCD_WR_Data(0x66);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0xBB);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0x66);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Data(0x45);
LCD_WR_Data(0x00);
LCD_WR_Data(0x43);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE7);
LCD_WR_Data(0x32);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Data(0x76);
LCD_WR_Data(0x00);
LCD_WR_Data(0x66);
LCD_WR_Data(0x00);
LCD_WR_Data(0x67);
LCD_WR_Data(0x00);
LCD_WR_Data(0x67);
LCD_WR_Data(0x00);
LCD_WR_Data(0x87);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0xBB);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0x77);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x56);
LCD_WR_Data(0x00);
LCD_WR_Data(0x23);
LCD_WR_Data(0x00);
LCD_WR_Data(0x33);
LCD_WR_Data(0x00);
LCD_WR_Data(0x45);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE8);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0x87);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Data(0x77);
LCD_WR_Data(0x00);
LCD_WR_Data(0x66);
LCD_WR_Data(0x00);
LCD_WR_Data(0x88);
LCD_WR_Data(0x00);
LCD_WR_Data(0xAA);
LCD_WR_Data(0x00);
LCD_WR_Data(0xBB);
LCD_WR_Data(0x00);
LCD_WR_Data(0x99);
LCD_WR_Data(0x00);
LCD_WR_Data(0x66);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x44);
LCD_WR_Data(0x00);
LCD_WR_Data(0x55);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xE9);
LCD_WR_Data(0xAA);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0x00);
LCD_WR_Data(0xAA);
LCD_WR_Cmd(0xCF);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xF0);
LCD_WR_Data(0x00);
LCD_WR_Data(0x50);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xF3);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0xF9);
LCD_WR_Data(0x06);
LCD_WR_Data(0x10);
LCD_WR_Data(0x29);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0x3A);
LCD_WR_Data(0x55); //66
LCD_WR_Cmd(0x11);
for(i=0; i<0xFFFFFFFF; i--);
LCD_WR_Cmd(0x29);
LCD_WR_Cmd(0x35);
LCD_WR_Data(0x00);
LCD_WR_Cmd(0x51);
LCD_WR_Data(0xFF);
LCD_WR_Cmd(0x53);
LCD_WR_Data(0x2C);
LCD_WR_Cmd(0x55);
LCD_WR_Data(0x82);
LCD_WR_Cmd(0x2c);
LCD_BL(ON);
LCD_Clear(WHITE);
Set_RW_Direction(L2R_U2D);
}
//-----------------------------------------------------------------------------------------------------------------
// 读液晶驱动 ID (基于 5310 驱动芯片)
//-----------------------------------
u16 RD_ID(void)
{
u16 tmp=0;
LCD_WR_Cmd(0XD4);
tmp=LCD_RD_Data(); //dummy read
tmp=LCD_RD_Data(); //读回0X01
tmp=LCD_RD_Data(); //读回0X53
tmp<<=8;
tmp|=LCD_RD_Data(); //这里读回0X10
// 加入此句可通过串口打印出驱动 ID
//printf(" LCD ID:%x\r\n",tmp); //打印LCD ID
return tmp;
}
//-----------------------------------------------------------------------------------------------------------------
// 开窗函数
//-----------------------------------------------------------------------------------------------------------------
void Open_Window(u16 Xpos, u16 Ypos, u16 Width, u16 Hight)
{
// 设置坐标
LCD_WR_Cmd(0x2A);
LCD_WR_Data(Xpos>>8);
LCD_WR_Data(Xpos&0xFF);
LCD_WR_Data( (Xpos+Width)>>8 );
LCD_WR_Data( (Xpos+Width)&0xFF );
LCD_WR_Cmd(0x2B);
LCD_WR_Data(Ypos>>8);
LCD_WR_Data(Ypos&0xFF);
LCD_WR_Data( (Ypos+Hight)>>8 );
LCD_WR_Data( (Ypos+Hight)&0xFF );
}
//-----------------------------------------------------------------------------------------------------------------
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
//-----------------------------------------------------------------------------------------------------------------
void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
LCD_WR_Cmd(0X2A); // 设置x光标
LCD_WR_Data(Xpos>>8);
LCD_WR_Data(Xpos&0XFF);
LCD_WR_Cmd(0X2B); // 设置y光标
LCD_WR_Data(Ypos>>8);
LCD_WR_Data(Ypos&0XFF);
}
//-----------------------------------------------------------------------------------------------------------------
//画点
//x,y:坐标
//POINT_COLOR:此点的颜色
//-----------------------------------------------------------------------------------------------------------------
void LCD_DrawPoint(u16 x,u16 y,u16 color)
{
LCD_SetCursor(x,y); //设置光标位置
LCD_WR_GRAM_Cmd(); //开始写入GRAM
LCD_WR_Data(color);
}
//-----------------------------------------------------------------------------------------------------------------
//快速画点
//x,y:坐标
//color:颜色
void LCD_Fast_DrawPoint(u16 x,u16 y,u16 color)
{
LCD_WR_Cmd(Set_X_Pos);
LCD_WR_Data(x>>8);LCD_WR_Data(x&0XFF);
LCD_WR_Cmd(Set_Y_Pos);
LCD_WR_Data(y>>8);LCD_WR_Data(y&0XFF);
LCD_WR_GRAM_Cmd();
LCD_WR_Data(color);
}
//当mdk -O1时间优化时需要设置
//延时i
void opt_delay(u8 i)
{
while(i--);
}
// 读点
// x,y:坐标点
//-----------------------------------------------------------------------------------------------------------------
u16 LCD_ReadPoint(u16 x,u16 y)
{
u16 r=0,g=0,b=0;
if(x>=LCD_WIDTH||y>=LCD_HIGHT)
return 0; //超过了范围,直接返回
LCD_SetCursor(x,y);
LCD_WR_Cmd(RD_GRAM);
opt_delay(2);
b=LCD_RD_Data();
g=r&0XFF; //对于9341/5310/5510,第一次读取的是RG的值,R在前,G在后,各占8位
g<<=8;
return (((r>>11)<<11)|((g>>10)<<5)|(b>>11));
}
//-----------------------------------------------------------------------------------------------------------------
// 在指定位置画一个指定大小的圆
// (x,y):中心点
// r :半径
//-----------------------------------------------------------------------------------------------------------------
void LCD_Draw_Circle(u16 x0,u16 y0,u8 r,u16 color)
{
int a,b;
int di;
a=0;b=r;
di=3-(r<<1); //判断下个点位置的标志
while(a<=b)
{
LCD_DrawPoint(x0+a,y0-b,color); //5
LCD_DrawPoint(x0+b,y0-a,color); //0
LCD_DrawPoint(x0+b,y0+a,color); //4
LCD_DrawPoint(x0+a,y0+b,color); //6
LCD_DrawPoint(x0-a,y0+b,color); //1
LCD_DrawPoint(x0-b,y0+a,color);
LCD_DrawPoint(x0-a,y0-b,color); //2
LCD_DrawPoint(x0-b,y0-a,color); //7
a++;
//使用Bresenham算法画圆
if(di<0)di +=4*a+6;
else
{
di+=10+4*(a-b);
b--;
}
}
}
//-----------------------------------------------------------------------------------------------------------------
// 画线
// x1,y1:起点坐标
// x2,y2:终点坐标
//-----------------------------------------------------------------------------------------------------------------
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2, u16 color)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; // 计算坐标增量
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1; // 设置单步方向
else if(delta_x==0)incx=0; // 垂直线
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0; // 水平线
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x; // 选取基本增量坐标轴
else distance=delta_y;
for(t=0;t<=distance+1;t++ ) // 画线输出
{
LCD_DrawPoint(uRow,uCol,color); // 画点
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//-----------------------------------------------------------------------------------------------------------------
// 画矩形
// (x1,y1),(x2,y2):矩形的对角坐标
//-----------------------------------------------------------------------------------------------------------------
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2, u16 color)
{
LCD_DrawLine(x1,y1,x2,y1,color);
LCD_DrawLine(x1,y1,x1,y2,color);
LCD_DrawLine(x1,y2,x2,y2,color);
LCD_DrawLine(x2,y1,x2,y2,color);
}
//-----------------------------------------------------------------------------------------------------------------
// 在指定区域内填充单个颜色
// (sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
// color:要填充的颜色
//-----------------------------------------------------------------------------------------------------------------
void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color)
{
u16 i,j;
u16 xlen=0;
xlen=ex-sx+1;
for(i=sy;i<=ey;i++)
{
LCD_SetCursor(sx,i); // 设置光标位置
LCD_WR_GRAM_Cmd(); // 开始写入GRAM
for(j=0;j<xlen;j++)LCD_WR_Data(color); // 显示颜色
}
}
//-----------------------------------------------------------------------------------------------------------------
// 在指定区域内填充指定颜色块
// (sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
// color:要填充的颜色
//-----------------------------------------------------------------------------------------------------------------
void LCD_Color_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 *color)
{
u16 height,width;
u16 i,j;
width=ex-sx+1; // 得到填充的宽度
height=ey-sy+1; // 高度
for(i=0;i<height;i++)
{
LCD_SetCursor(sx,sy+i); // 设置光标位置
LCD_WR_GRAM_Cmd(); // 开始写入GRAM
for(j=0;j<width;j++)LCD_WR_Data(color[i*width+j]);// 写入数据
}
}
//-----------------------------------------------------------------------------------------------------------------
// 在指定位置显示一个字符
// x,y:起始坐标
// num:要显示的字符:" "--->"~"
// size:字体大小 12/16/24
// mode:叠加方式(1)还是非叠加方式(0)
//-----------------------------------------------------------------------------------------------------------------
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{
u8 temp,t1,t;
u16 y0=y;
u8 csize=(size/8+((size%8)?1:0))*(size/2); // 得到字体一个字符对应点阵集所占的字节数
num=num-' '; // 得到偏移后的值(ASCII字库是从空格开始取模,所以-' '就是对应字符的字库)
for(t=0;t<csize;t++)
{
if(size==12)temp=asc2_1206[num][t]; // 调用1206字体
else if(size==16)temp=asc2_1608[num][t]; // 调用1608字体
else if(size==24)temp=asc2_2412[num][t]; // 调用2412字体
else return; // 没有的字库
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LCD_Fast_DrawPoint(x,y,POINT_COLOR);
else if(mode==0)LCD_Fast_DrawPoint(x,y,BACK_COLOR);
temp<<=1;
y++;
if(y>=LCD_HIGHT)return; // 超区域了
if((y-y0)==size)
{
y=y0;
x++;
if(x>=LCD_WIDTH)return; // 超区域了
break;
}
}
}
}
//-----------------------------------------------------------------------------------------------------------------
//m^n函数
//返回值:m^n次方.
//-----------------------------------------------------------------------------------------------------------------
u32 LCD_Pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//-----------------------------------------------------------------------------------------------------------------
//显示数字,高位为0,则不显示
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//color:颜色
//num:数值(0~4294967295);
//-----------------------------------------------------------------------------------------------------------------
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/LCD_Pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
continue;
}else enshow=1;
}
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0);
}
}
//-----------------------------------------------------------------------------------------------------------------
//显示数字,高位为0,还是显示
//x,y:起点坐标
//num:数值(0~999999999);
//len:长度(即要显示的位数)
//size:字体大小
//mode:
//[7]:0,不填充;1,填充0.
//[6:1]:保留
//[0]:0,非叠加显示;1,叠加显示.
//-----------------------------------------------------------------------------------------------------------------
void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/LCD_Pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);
else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);
continue;
}else enshow=1;
}
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01);
}
}
//-----------------------------------------------------------------------------------------------------------------
//显示字符串
//x,y:起点坐标
//width,height:区域大小
//size:字体大小
//*p:字符串起始地址
//-----------------------------------------------------------------------------------------------------------------
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
{
u8 x0=x;
width+=x;
height+=y;
while((*p<='~')&&(*p>=' '))//判断是不是非法字符!
{
if(x>=width){x=x0;y+=size;}
if(y>=height)break;//退出
LCD_ShowChar(x,y,*p,size,0);
x+=size/2;
p++;
}
}
//-----------------------------------------------------------------------------------------------------------------
以上便完成了对LCD驱动及功能函数的一个移植。
3. 移植正点原子SPI驱动W25Q128
3.1 CubeMX配置SPI
第二步:配置全双工模式
第三步:可配置片选引脚是硬件还是软件,因为板子上连接的就是SPI硬件引脚,因此我配置为硬件
第四步:一般的SPI常规配置,主机模式,8位数据位为一个传输单位,高位在前
第五步:这个是根据与其通信的器件来决定的,根据所通信器件的SPI通信属性来配置。
3.2 W25Q128程序移植
w25qxx.h:
#ifndef __W25QXX_H
#define __W25QXX_H
#include "sys.h"
extern u16 W25QXX_TYPE; // 定义W25QXX芯片型号
extern u32 W25QXX_Flash; // Flash大小(以字节为单位)
//W25X系列/Q系列芯片列表
//W25Q80 ID 0XEF13
//W25Q16 ID 0XEF14
//W25Q32 ID 0XEF15
//W25Q64 ID 0XEF16
//W25Q128 ID 0XEF17
//W25Q256 ID 0XEF18
#define W25Q80 0XEF13
#define W25Q16 0XEF14
#define W25Q32 0XEF15
#define W25Q64 0XEF16
#define W25Q128 0XEF17
#define W25Q256 0XEF18
#define W25QXX_CS(OnOrOff) \
{ \
if(OnOrOff) \
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET); \
else \
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET); \
}
//指令表
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg1 0x05
#define W25X_ReadStatusReg2 0x35
#define W25X_ReadStatusReg3 0x15
#define W25X_WriteStatusReg1 0x01
#define W25X_WriteStatusReg2 0x31
#define W25X_WriteStatusReg3 0x11
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#define W25X_Enable4ByteAddr 0xB7
#define W25X_Exit4ByteAddr 0xE9
void W25QXX_Init(void);
u16 W25QXX_ReadID(void); // 读取FLASH ID
u8 W25QXX_ReadSR(u8 regno); // 读取状态寄存器
void W25QXX_4ByteAddr_Enable(void); // 使能4字节地址模式
void W25QXX_Write_SR(u8 regno,u8 sr); // 写状态寄存器
void W25QXX_Write_Enable(void); // 写使能
void W25QXX_Write_Disable(void); // 写保护
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead); // 读取flash
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite); // 写入flash
void W25QXX_Erase_Chip(void); // 整片擦除
void W25QXX_Erase_Sector(u32 Dst_Addr); // 扇区擦除
void W25QXX_Wait_Busy(void); // 等待空闲
void W25QXX_PowerDown(void); // 进入掉电模式
void W25QXX_WAKEUP(void); // 唤醒
#endif
w25qxx.c:
#include "w25qxx.h"
#include "spi2.h"
#include "spi.h"
extern SPI_HandleTypeDef hspi2;
u16 W25QXX_TYPE=W25Q128; //默认是W25Q128
// 根据初始化的芯片ID来决定Flash大小
u32 W25QXX_Flash;
// 4Kbytes为一个Sector
// 16个扇区为1个Block
// W25Q256
// 容量为32M字节,共有512个Block,8192个Sector
// 初始化SPI FLASH的IO口
void W25QXX_Init(void)
{
u8 temp;
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOB_CLK_ENABLE(); //使能GPIOB时钟
//PB14
GPIO_Initure.Pin=GPIO_PIN_12; //PB12
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//高速
HAL_GPIO_Init(GPIOB,&GPIO_Initure); //初始化
W25QXX_CS(1); //SPI FLASH不选中
MX_SPI2_Init();
SPI2_SetSpeed(SPI_BAUDRATEPRESCALER_2); //设置为42M时钟,高速模式
W25QXX_TYPE=W25QXX_ReadID(); //读取FLASH ID.
switch(W25QXX_TYPE)
{
case W25Q80: W25QXX_Flash = 1*1024*1024; break; // 1MB,20位地址
case W25Q16: W25QXX_Flash = 2*1024*1024; break; // 2MB,21位地址
case W25Q32: W25QXX_Flash = 4*1024*1024; break; // 4MB,22位地址
case W25Q64: W25QXX_Flash = 8*1024*1024; break; // 8MB,23位地址
case W25Q128: W25QXX_Flash = 16*1024*1024; break; // 16MB,24位地址
case W25Q256: W25QXX_Flash = 32*1024*1024; break; // 32MB,25位地址
}
if(W25QXX_TYPE==W25Q256) //SPI FLASH为W25Q256
{
temp=W25QXX_ReadSR(3); //读取状态寄存器3,判断地址模式
if((temp&0X01)==0) //如果不是4字节地址模式,则进入4字节地址模式
{
W25QXX_CS(0); //选中
SPI2_ReadWriteByte(W25X_Enable4ByteAddr);//发送进入4字节地址模式指令
W25QXX_CS(1); //取消片选
}
}
}
// 读取W25QXX的状态寄存器,W25QXX一共有3个状态寄存器
// 状态寄存器1:
// BIT7 6 5 4 3 2 1 0
// SPR RV TB BP2 BP1 BP0 WEL BUSY
// SPR:默认0,状态寄存器保护位,配合WP使用
// TB,BP2,BP1,BP0:FLASH区域写保护设置
// WEL:写使能锁定
// BUSY:忙标记位(1,忙;0,空闲)
// 默认:0x00
// 状态寄存器2:
// BIT7 6 5 4 3 2 1 0
// SUS CMP LB3 LB2 LB1 (R) QE SRP1
// 状态寄存器3:
// BIT7 6 5 4 3 2 1 0
// HOLD/RST DRV1 DRV0 (R) (R) WPS ADP ADS
// regno:状态寄存器号,范:1~3
// 返回值:状态寄存器值
u8 W25QXX_ReadSR(u8 regno)
{
u8 byte=0,command=0;
switch(regno)
{
case 1:
command=W25X_ReadStatusReg1; break; //读状态寄存器1指令
case 2:
command=W25X_ReadStatusReg2; break; //读状态寄存器2指令
case 3:
command=W25X_ReadStatusReg3; break; //读状态寄存器3指令
default:
command=W25X_ReadStatusReg1; break;
}
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(command); //发送读取状态寄存器命令
byte=SPI2_ReadWriteByte(0Xff); //读取一个字节
W25QXX_CS(1); //取消片选
return byte;
}
// 写W25QXX状态寄存器
void W25QXX_Write_SR(u8 regno,u8 sr)
{
u8 command=0;
switch(regno)
{
case 1:
command=W25X_WriteStatusReg1; break; //写状态寄存器1指令
case 2:
command=W25X_WriteStatusReg2; break; //写状态寄存器2指令
case 3:
command=W25X_WriteStatusReg3; break; //写状态寄存器3指令
default:
command=W25X_WriteStatusReg1; break;
}
W25QXX_CS(0); // 使能器件
SPI2_ReadWriteByte(command); // 发送写取状态寄存器命令
SPI2_ReadWriteByte(sr); // 写入一个字节
W25QXX_CS(1); // 取消片选
}
// W25QXX写使能
// 将WEL置位
void W25QXX_Write_Enable(void)
{
W25QXX_CS(0); // 使能器件
SPI2_ReadWriteByte(W25X_WriteEnable); // 发送写使能
W25QXX_CS(1); // 取消片选
}
// W25QXX写禁止
// 将WEL清零
void W25QXX_Write_Disable(void)
{
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_WriteDisable); //发送写禁止指令
W25QXX_CS(1); //取消片选
}
// 读取芯片ID
// 返回值如下:
// 0XEF13,表示芯片型号为W25Q80
// 0XEF14,表示芯片型号为W25Q16
// 0XEF15,表示芯片型号为W25Q32
// 0XEF16,表示芯片型号为W25Q64
// 0XEF17,表示芯片型号为W25Q128
// 0XEF18,表示芯片型号为W25Q256
u16 W25QXX_ReadID(void)
{
u16 Temp = 0;
W25QXX_CS(0);
SPI2_ReadWriteByte(0x90);//发送读取ID命令
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
Temp|=SPI2_ReadWriteByte(0xFF)<<8;
Temp|=SPI2_ReadWriteByte(0xFF);
W25QXX_CS(1);
return Temp;
}
// 读取SPI FLASH
// 在指定地址开始读取指定长度的数据
// pBuffer:数据存储区
// ReadAddr:开始读取的地址(24bit)
// NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
u16 i;
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_ReadData); //发送读取命令
if(W25QXX_TYPE==W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPI2_ReadWriteByte((u8)((ReadAddr)>>24));
}
SPI2_ReadWriteByte((u8)((ReadAddr)>>16)); //发送24bit地址
SPI2_ReadWriteByte((u8)((ReadAddr)>>8));
SPI2_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=SPI2_ReadWriteByte(0XFF); //循环读数
}
W25QXX_CS(1);
}
// SPI在一页(0~65535)内写入少于256个字节的数据
// 在指定地址开始写入最大256字节的数据
// pBuffer:数据存储区
// WriteAddr:开始写入的地址(24bit)
// NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
W25QXX_Write_Enable(); //SET WEL
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_PageProgram); //发送写页命令
if(W25QXX_TYPE==W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPI2_ReadWriteByte((u8)((WriteAddr)>>24));
}
SPI2_ReadWriteByte((u8)((WriteAddr)>>16)); //发送24bit地址
SPI2_ReadWriteByte((u8)((WriteAddr)>>8));
SPI2_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)SPI2_ReadWriteByte(pBuffer[i]);//循环写数
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待写入结束
}
// 无检验写SPI FLASH
// 必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
// 具有自动换页功能
// 在指定地址开始写入指定长度的数据,但是要确保地址不越界!
// pBuffer:数据存储区
// WriteAddr:开始写入的地址(24bit)
// NumByteToWrite:要写入的字节数(最大65535)
// CHECK OK
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr%256; //单页剩余的字节数
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
while(1)
{
W25QXX_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)break;//写入结束了
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //减去已经写入了的字节数
if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
else pageremain=NumByteToWrite; //不够256个字节了
}
};
}
// 写SPI FLASH
// 在指定地址开始写入指定长度的数据
// 该函数带擦除操作!
// pBuffer:数据存储区
// WriteAddr:开始写入的地址(24bit)
// NumByteToWrite:要写入的字节数(最大65535)
u8 W25QXX_BUFFER[4096];
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u8 * W25QXX_BUF;
W25QXX_BUF=W25QXX_BUFFER;
secpos=WriteAddr/4096;//扇区地址
secoff=WriteAddr%4096;//在扇区内的偏移
secremain=4096-secoff;//扇区剩余空间大小
//printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试用
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
while(1)
{
W25QXX_Read(W25QXX_BUF,secpos*4096,4096);//读出整个扇区的内容
for(i=0;i<secremain;i++)//校验数据
{
if(W25QXX_BUF[secoff+i]!=0XFF)break;//需要擦除
}
if(i<secremain)//需要擦除
{
W25QXX_Erase_Sector(secpos);//擦除这个扇区
for(i=0;i<secremain;i++) //复制
{
W25QXX_BUF[i+secoff]=pBuffer[i];
}
W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);//写入整个扇区
}
else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.
if(NumByteToWrite==secremain)break;//写入结束了
else//写入未结束
{
secpos++;//扇区地址增1
secoff=0;//偏移位置为0
pBuffer+=secremain; //指针偏移
WriteAddr+=secremain;//写地址偏移
NumByteToWrite-=secremain; //字节数递减
if(NumByteToWrite>4096)secremain=4096; //下一个扇区还是写不完
else secremain=NumByteToWrite; //下一个扇区可以写完了
}
};
}
// 擦除整个芯片
// 等待时间超长...
void W25QXX_Erase_Chip(void)
{
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_ChipErase); //发送片擦除命令
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待芯片擦除结束
}
// 擦除一个扇区
// Dst_Addr:扇区地址 根据实际容量设置
// 擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)
{
//监视falsh擦除情况,测试用
//printf("fe:%x\r\n",Dst_Addr);
Dst_Addr*=4096;
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_SectorErase); //发送扇区擦除指令
if(W25QXX_TYPE==W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPI2_ReadWriteByte((u8)((Dst_Addr)>>24));
}
SPI2_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
SPI2_ReadWriteByte((u8)((Dst_Addr)>>8));
SPI2_ReadWriteByte((u8)Dst_Addr);
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待擦除完成
}
// 等待空闲
void W25QXX_Wait_Busy(void)
{
while((W25QXX_ReadSR(1)&0x01)==0x01); // 等待BUSY位清空
}
// 进入掉电模式
void W25QXX_PowerDown(void)
{
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_PowerDown); //发送掉电命令
W25QXX_CS(1); //取消片选
HAL_Delay(1); //等待TPD
}
// 唤醒
void W25QXX_WAKEUP(void)
{
W25QXX_CS(0); //使能器件
SPI2_ReadWriteByte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
W25QXX_CS(1); //取消片选
HAL_Delay(1); //等待TRES1
}