电子秤stm32程序

更新时间:2024-02-03 14:16:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include \#include \#include \#include \#include \#include \#include \#include \#include \

int main(void) { u8 num,flag; float vol1; delay_init(); uart_init(9600); LED_Init(); LCD_Init(); Adc_Init(); KEY_Init(); HX711_Init(); POINT_COLOR=RED; HZ_show(); LCD_ShowString(54+25,55,200,24,16,\提示信息显示 LCD_ShowString(54+25,85,200,24,16,\提示信息显示 LCD_ShowString(54+25,115,200,24,16,\提示信息显示 LCD_ShowString(54+25,145,200,24,16,\提示信息显示 while(1) { vol1=HX711_count(); if(KEY_Scan(1)==WKUP_PRES) //单价设置 num的值可直接用 { num++; if(num==100) num=0; LCD_ShowxNum(54+25+8,85,num,2,16,0); } if(KEY_Scan(1)==KEY0_PRES) //控制金额是否累加flag=1是累加 flag=0不累加 { HZ_show_moy();

flag++; LED0=0; if(flag==2) { LED0=1; LCD_ShowString(54+25+32+30,115,200,16,16,\ \ flag=0; } printf(\ } delay_ms(300); LED1=!LED1; } }

#include \#include \

u8 table1[]= {

0x00,0x00,0x00,0x00,0x00,0x04,0x02,0x00,0x04,0x02,0x00,0x04,0x02,0x00,0x24,0x0A, 0xFF,0xA4,0x0A,0x7F,0x24,0x0A,0x49,0x24,0x12,0x49,0x24,0x12,0x49,0x24,0x12,0x49, 0x24,0x1F,0xFF,0xFC,0x12,0x49,0x24,0x12,0x49,0x24,0x12,0x49,0x24,0x22,0x49,0x24, 0x22,0x49,0x24,0x22,0xFF,0xA4,0x02,0x40,0x64,0x06,0x00,0x24,0x06,0x00,0x0C,0x02, 0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00, };/*重*/

u8 table2[]= {

0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x02,0x00,0x40,0x02,0x00,0x40,0x12,0x3F, 0xDF,0xD2,0x3F,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52, 0x92,0x25,0x5F,0xFC,0x25,0x5F,0xFC,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92, 0x25,0x52,0x92,0x3F,0x5F,0xD2,0x20,0x5F,0xB2,0x00,0x40,0x12,0x00,0x40,0x04,0x00, 0xC0,0x04,0x00,0x40,0x00,0x00,0x00,0x00, };/*量*/

u8 table3[]= {

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x00,0x00,0x40,0x03, 0xFE,0x40,0x02,0x22,0x40,0x22,0x22,0x40,0x32,0x22,0x40,0x1E,0x22,0x40,0x02,0x22, 0x40,0x03,0xFF,0xFE,0x02,0x22,0x40,0x06,0x22,0x40,0x0A,0x22,0x40,0x72,0x22,0x40, 0x22,0x22,0x40,0x02,0x22,0x40,0x03,0xFE,0x40,0x02,0x00,0x40,0x00,0x00,0xC0,0x00, 0x00,0xC0,0x00,0x00,0x40,0x00,0x00,0x00, };/*单*/

u8 table4[]= {

0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x10,0x00,0x00,0x60,0x00,0x01,0xC0,0x00,0x07, 0xFF,0xFE,0x3C,0x80,0x00,0x30,0x10,0x02,0x00,0x20,0x04,0x00,0x40,0x08,0x00,0x80, 0x18,0x03,0x3F,0xE0,0x06,0x3E,0x00,0x3C,0x00,0x00,0x30,0x00,0x00,0x0C,0x00,0x00, 0x02,0x00,0x01,0x01,0x3F,0xFE,0x00,0x80,0x00,0x00,0xC0,0x00,0x00,0x60,0x00,0x00, 0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };/*价*/ u8 table5[]= {

0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x01,0x08,0x08,0x01, 0x08,0x0C,0x01,0x08,0x1C,0x01,0x08,0x2C,0x01,0x08,0x4C,0x01,0x08,0x88,0x01,0x0B, 0x08,0x3F,0xFE,0x08,0x3F,0xF8,0x08,0x01,0x08,0x08,0x01,0x08,0x08,0x01,0x08,0x48, 0x01,0x08,0x68,0x01,0x08,0x38,0x03,0x08,0x18,0x01,0x08,0x1E,0x00,0x18,0x00,0x00, 0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };/*去*/

u8 table6[]= {

0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x04,0x00,0x00,0x18,0x00,0x00,0xE2,0x07, 0xFF,0x82,0x02,0x00,0x02,0x02,0x00,0x02,0x02,0x08,0x04,0x02,0x0E,0x04,0x02,0x09, 0x08,0x02,0x08,0xC8,0x7F,0xF0,0x70,0x3F,0xF0,0x30,0x02,0x08,0x70,0x02,0x09,0x88, 0x02,0x0B,0x0C,0x02,0x0E,0x0C,0x02,0x98,0x04,0x07,0x08,0x06,0x06,0x00,0x06,0x00, 0x00,0x04,0x00,0x00,0x04,0x00,0x00,0x00, };/*皮*/

u8 table7[]= {

0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x08,0x02,0x00,0x10,0x02,0x00,0x21,0x02,0x00, 0x41,0x82,0x00,0xC1,0x42,0x01,0x91,0x32,0x03,0x11,0x1A,0x0E,0x11,0x02,0x38,0x11, 0x02,0x30,0x1F,0xFC,0x28,0x11,0x02,0x04,0x11,0x02,0x02,0x11,0x04,0x01,0x11,0x1A, 0x00,0xA1,0xE2,0x00,0xD1,0x42,0x00,0x62,0x02,0x00,0x62,0x02,0x00,0x30,0x04,0x00, 0x20,0x04,0x00,0x20,0x00,0x00,0x00,0x00, };/*金*/

u8 table8[]=

{

0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x08,0x80,0x0C,0x31,0x00,0x04,0xD3,0xFC,0x07, 0x96,0x84,0x24,0x5C,0x84,0x1C,0x58,0x84,0x04,0x68,0x84,0x05,0xCD,0xFC,0x06,0x06, 0x00,0x0C,0x02,0x02,0x16,0x00,0x02,0x11,0xFF,0x84,0x11,0x00,0x08,0x13,0x00,0x10, 0x1D,0x47,0xE0,0x11,0x3F,0x00,0x11,0x20,0x20,0x11,0x00,0x10,0x11,0xFF,0xD8,0x23, 0xFF,0x8C,0x00,0x00,0x06,0x00,0x00,0x00, };/*额*/

u8 table9[]= {

0x00,0x00,0x00,0x01,0x7C,0x12,0x54,0x94,0x54,0x90,0x55,0xB2,0x56,0xB1,0x7C,0xDE, 0x54,0xD0,0x54,0x90,0x55,0x14,0x54,0x32,0x7C,0x19,0x00,0x00,0x00,0x00,0x00,0x00, };/*累*/

u8 table10[]= {

0x08,0x01,0x08,0x02,0x08,0x0C,0xFF,0xF0,0x08,0x02,0x08,0x01,0x0F,0xFE,0x00,0x00, 0x00,0x00,0x1F,0xFE,0x10,0x04,0x10,0x04,0x10,0x04,0x1F,0xFE,0x00,0x00,0x00,0x00, };/*加*/

// 显示一个汉字 //

u8 lcdoneHZ(u8 x,u8 y,unsigned char *code) { u8 temp,t,y0,t1; u8 table[72]; y0=y; POINT_COLOR=RED; for(t=0;t<72;t++) { temp= *code++; for(t1=0;t1<8;t1++) { if(temp&0x80)LCD_DrawPoint(x,y); temp<<=1; y++; if((y-y0)==24) { y=y0; x++; break;

} } } }

u8 lcdone16HZ(u8 x,u8 y,unsigned char *code) { u8 temp,t,y0,t1; u8 table[32]; y0=y; POINT_COLOR=RED; for(t=0;t<32;t++) { temp= *code++; for(t1=0;t1<8;t1++) { if(temp&0x80)LCD_DrawPoint(x,y); temp<<=1; y++; if((y-y0)==16) { y=y0; x++; break; } } } }

void HZ_show(void) { lcdoneHZ(30,50,table1);//重量 lcdoneHZ(54,50,table2); lcdoneHZ(30,80,table3); //单价 lcdoneHZ(54,80,table4); lcdoneHZ(30,110,table7); //金额 lcdoneHZ(54,110,table8); //金额 lcdoneHZ(30,140,table5);//去皮 lcdoneHZ(54,140,table6); }

void HZ_show_moy(void) { lcdone16HZ(54+25+32+30,115,table9); lcdone16HZ(54+25+32+30+17,115,table10); }

#ifndef __LCDFOT_H #define __LCDFOT_H #include \

u8 lcdoneHZ(u8 x,u8 y,unsigned char *code); //显示24位汉字 u8 lcdone16HZ(u8 x,u8 y,unsigned char *code); //显示16位汉字

void HZ_show(void);

void HZ_show_moy(void);

#endif

#include \#include \#include \

void HX711_Init(void) {

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PE端口时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //LED0-->PB.5 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5

GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6); }

u32 HX711_read(void) { u32 count; u8 i; delay_us(1); HX711_SCL=0; count=0; while(HX711_DOUT); for(i=0;i<24;i++) { HX711_SCL=1; count=count<<1; HX711_SCL=0; if(HX711_DOUT) count++; } HX711_SCL=1; count=count^0x800000; delay_us(1); HX711_SCL=0; return count; }

//得到的质量单位是 g 克 float HX711_count(void) { u8 a; u32 adc,wei; float vol; for(a=10;a>0;a--) { adc=HX711_read(); wei=wei+adc; } wei=wei/10; printf(\值:%d\\r\\n\ vol=((float)(wei-7431260)/1118)-1.8-1.55; wei=0;

//注意改参数 printf(\重量=%f\\r\\n\ return vol; }

#ifndef __HX711_H

#define __HX711_H #include \

#define HX711_SCL PAout(5) #define HX711_DOUT PAin(6)

void HX711_Init(void); u32 HX711_read(void); float HX711_count(void);

#endif

#include \#include \#include \ #include \ #include \

//////////////////////////////////////////////////////////////////////////////////

//本程序只供学习使用,未经作者许可,不得用于其它任何用途 //2.4/2.8寸/3.5寸 TFT液晶驱动 //支持驱动IC型号包括:ILI9341/ILI9325/RM68042/RM68021/ILI9320/ILI9328/LGDP4531/LGDP4535/SPFD5408/SSD1289/1505/B505/C505等 //正点原子@ALIENTEK

//********************************************************************************

//V1.2修改说明

//支持了SPFD5408的驱动,另外把液晶ID直接打印成HEX格式.方便查看LCD驱动IC. //V1.3

//加入了快速IO的支持

//修改了背光控制的极性(适用于V1.8及以后的开发板版本)

//对于1.8版本之前(不包括1.8)的液晶模块,请修改LCD_Init函数的LCD_LED=1;为

LCD_LED=1; //V1.4

//修改了LCD_ShowChar函数,使用画点功能画字符。 //加入了横竖屏显示的支持 //V1.5 20110730

//1,修改了B505液晶读颜色有误的bug. //2,修改了快速IO及横竖屏的设置方式. //V1.6 20111116

//1,加入对LGDP4535液晶的驱动支持 //V1.7 20120713

//1,增加LCD_RD_DATA函数 //2,增加对ILI9341的支持

//3,增加ILI9325的独立驱动代码

//4,增加LCD_Scan_Dir函数(慎重使用)

//6,另外修改了部分原来的函数,以适应9341的操作 //V1.8 20120905

//1,加入LCD重要参数设置结构体lcddev

//2,加入LCD_Display_Dir函数,支持在线横竖屏切换 //V1.9 20120911

//1,新增RM68042驱动(ID:6804),但是6804不支持横屏显示!!原因:改变扫描方式, //导致6804坐标设置失效,试过很多方法都不行,暂时无解。 //V2.0 20120924

//在不硬件复位的情况下,ILI9341的ID读取会被误读成9300,修改LCD_Init,将无法识别 //的情况(读到ID为9300/非法ID),强制指定驱动IC为ILI9341,执行9341的初始化。 //V2.1 20120930

//修正ILI9325读颜色的bug。 //V2.2 20121007

//修正LCD_Scan_Dir的bug。

//////////////////////////////////////////////////////////////////////////////////

//LCD的画笔颜色和背景色

u16 POINT_COLOR=0x0000; //画笔颜色 u16 BACK_COLOR=0xFFFF; //背景色

//管理LCD重要参数 //默认为竖屏 _lcd_dev lcddev; //写寄存器函数 //regval:寄存器值

void LCD_WR_REG(u16 regval) { LCD->LCD_REG=regval;//写入要写的寄存器序号

}

//写LCD数据 //data:要写入的值

void LCD_WR_DATA(u16 data) { LCD->LCD_RAM=data; }

//读LCD数据 //返回值:读到的值

u16 LCD_RD_DATA(void) { return LCD->LCD_RAM; } //写寄存器

//LCD_Reg:寄存器地址

//LCD_RegValue:要写入的数据

void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue) { LCD->LCD_REG = LCD_Reg; //写入要写的寄存器序号 LCD->LCD_RAM = LCD_RegValue;//写入数据 } //读寄存器

//LCD_Reg:寄存器地址 //返回值:读到的数据

u16 LCD_ReadReg(u8 LCD_Reg) { LCD_WR_REG(LCD_Reg); //写入要读的寄存器序号 delay_us(5); return LCD_RD_DATA(); //返回读到的值 }

//开始写GRAM

void LCD_WriteRAM_Prepare(void) {

LCD->LCD_REG=lcddev.wramcmd; }

//LCD写GRAM //RGB_Code:颜色值

void LCD_WriteRAM(u16 RGB_Code) { LCD->LCD_RAM = RGB_Code;//写十六位GRAM }

//从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。 //通过该函数转换

//c:GBR格式的颜色值

//返回值:RGB格式的颜色值 u16 LCD_BGR2RGB(u16 c) { u16 r,g,b,rgb; b=(c>>0)&0x1f; g=(c>>5)&0x3f; r=(c>>11)&0x1f; rgb=(b<<11)+(g<<5)+(r<<0); return(rgb); }

//当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>=lcddev.width||y>=lcddev.height)return 0; //超过了范围,直接返回 LCD_SetCursor(x,y); if(lcddev.id==0X9341||lcddev.id==0X6804)LCD_WR_REG(0X2E);//9341/6804 发送读GRAM指令 else LCD_WR_REG(R34); //其他IC发送读GRAM指令

if(lcddev.id==0X9320)opt_delay(2); //FOR 9320,延时2us if(LCD->LCD_RAM)r=0; //dummy Read opt_delay(2);

r=LCD->LCD_RAM; //实际坐标颜色 if(lcddev.id==0X9341)//9341要分2次读出 { opt_delay(2); b=LCD->LCD_RAM; g=r&0XFF;//对于9341,第一次读取的是RG的值,R在前,G在后,各占8位 g<<=8; }else if(lcddev.id==0X6804)r=LCD->LCD_RAM;//6804第二次读取的才是真实值 if(lcddev.id==0X9325||lcddev.id==0X4535||lcddev.id==0X4531||lcddev.id==0X8989||lcddev.id==0XB505)return r;//这几种IC直接返回颜色值 else if(lcddev.id==0X9341)return (((r>>11)<<11)|((g>>10)<<5)|(b>>11)); //ILI9341需要公式转换一下 else return LCD_BGR2RGB(r); //

其他IC } //LCD开启显示

void LCD_DisplayOn(void) { if(lcddev.id==0X9341||lcddev.id==0X6804)LCD_WR_REG(0X29); //开启显示 else LCD_WriteReg(R7,0x0173); //开启显示 }

//LCD关闭显示

void LCD_DisplayOff(void) { if(lcddev.id==0X9341||lcddev.id==0X6804)LCD_WR_REG(0X28); //关闭显示 else LCD_WriteReg(R7,0x0);//关闭显示 }

//设置光标位置 //Xpos:横坐标 //Ypos:纵坐标

void LCD_SetCursor(u16 Xpos, u16 Ypos) {

if(lcddev.id==0X9341||lcddev.id==0X6804) { LCD_WR_REG(lcddev.setxcmd); LCD_WR_DATA(Xpos>>8); LCD_WR_DATA(Xpos&0XFF); LCD_WR_REG(lcddev.setycmd); LCD_WR_DATA(Ypos>>8); LCD_WR_DATA(Ypos&0XFF); }else { if(lcddev.dir==1)Xpos=lcddev.width-1-Xpos;//横屏其实就是调转x,y坐标 LCD_WriteReg(lcddev.setxcmd, Xpos); LCD_WriteReg(lcddev.setycmd, Ypos); } }

//设置LCD的自动扫描方向

//注意:其他函数可能会受到此函数设置的影响(尤其是9341/6804这两个奇葩), //所以,一般设置为L2R_U2D即可,如果设置为其他扫描方式,可能导致显示不正常. //dir:0~7,代表8个方向(具体定义见lcd.h)

//9320/9325/9328/4531/4535/1505/b505/8989/5408/9341等IC已经实际测试 void LCD_Scan_Dir(u8 dir) { u16 regval=0; u8 dirreg=0; u16 temp;

if(lcddev.dir==1&&lcddev.id!=0X6804)//横屏时,对6804不改变扫描方向! { switch(dir)//方向转换 { case 0:dir=6;break; case 1:dir=7;break; case 2:dir=4;break; case 3:dir=5;break; case 4:dir=1;break; case 5:dir=0;break; case 6:dir=3;break; case 7:dir=2;break; } }

if(lcddev.id==0x9341||lcddev.id==0X6804)//9341/6804,很特殊 { switch(dir) { case L2R_U2D://从左到右,从上到下 regval|=(0<<7)|(0<<6)|(0<<5); break; case L2R_D2U://从左到右,从下到上 regval|=(1<<7)|(0<<6)|(0<<5); break; case R2L_U2D://从右到左,从上到下 regval|=(0<<7)|(1<<6)|(0<<5); break; case R2L_D2U://从右到左,从下到上 regval|=(1<<7)|(1<<6)|(0<<5); break; case U2D_L2R://从上到下,从左到右 regval|=(0<<7)|(0<<6)|(1<<5); break; case U2D_R2L://从上到下,从右到左 regval|=(0<<7)|(1<<6)|(1<<5); break; case D2U_L2R://从下到上,从左到右 regval|=(1<<7)|(0<<6)|(1<<5); break; case D2U_R2L://从下到上,从右到左 regval|=(1<<7)|(1<<6)|(1<<5); break; } dirreg=0X36;

regval|=0X08;//BGR if(lcddev.id==0X6804)regval|=0x02;//6804的BIT6和9341的反了 LCD_WriteReg(dirreg,regval); if(regval&0X20) { if(lcddev.widthlcddev.height)//交换X,Y { temp=lcddev.width; lcddev.width=lcddev.height; lcddev.height=temp; } } LCD_WR_REG(lcddev.setxcmd); LCD_WR_DATA(0);LCD_WR_DATA(0); LCD_WR_DATA((lcddev.width-1)>>8);LCD_WR_DATA((lcddev.width-1)&0XFF); LCD_WR_REG(lcddev.setycmd); LCD_WR_DATA(0);LCD_WR_DATA(0); LCD_WR_DATA((lcddev.height-1)>>8);LCD_WR_DATA((lcddev.height-1)&0XFF); }else { switch(dir) { case L2R_U2D://从左到右,从上到下 regval|=(1<<5)|(1<<4)|(0<<3); break; case L2R_D2U://从左到右,从下到上 regval|=(0<<5)|(1<<4)|(0<<3); break; case R2L_U2D://从右到左,从上到下 regval|=(1<<5)|(0<<4)|(0<<3); break; case R2L_D2U://从右到左,从下到上 regval|=(0<<5)|(0<<4)|(0<<3); break; case U2D_L2R://从上到下,从左到右 regval|=(1<<5)|(1<<4)|(1<<3);

break; case U2D_R2L://从上到下,从右到左 regval|=(1<<5)|(0<<4)|(1<<3); break; case D2U_L2R://从下到上,从左到右 regval|=(0<<5)|(1<<4)|(1<<3); break; case D2U_R2L://从下到上,从右到左 regval|=(0<<5)|(0<<4)|(1<<3); break; } if(lcddev.id==0x8989)//8989 IC { dirreg=0X11; regval|=0X6040; //65K }else//其他驱动IC { dirreg=0X03; regval|=1<<12; } LCD_WriteReg(dirreg,regval); } } //画点 //x,y:坐标

//POINT_COLOR:此点的颜色 void LCD_DrawPoint(u16 x,u16 y) { LCD_SetCursor(x,y); //设置光标位置 LCD_WriteRAM_Prepare(); //开始写入GRAM LCD->LCD_RAM=POINT_COLOR; }

//快速画点 //x,y:坐标 //color:颜色

void LCD_Fast_DrawPoint(u16 x,u16 y,u16 color) { if(lcddev.id==0X9341||lcddev.id==0X6804) { LCD_WR_REG(lcddev.setxcmd); LCD_WR_DATA(x>>8); LCD_WR_DATA(x&0XFF); LCD_WR_REG(lcddev.setycmd); LCD_WR_DATA(y>>8);

LCD_WR_DATA(y&0XFF); }else { if(lcddev.dir==1)x=lcddev.width-1-x;//横屏其实就是调转x,y坐标 LCD_WriteReg(lcddev.setxcmd,x); LCD_WriteReg(lcddev.setycmd,y); } LCD->LCD_REG=lcddev.wramcmd; LCD->LCD_RAM=color; }

//设置LCD显示方向(6804不支持横屏显示) //dir:0,竖屏;1,横屏

void LCD_Display_Dir(u8 dir) { if(dir==0)//竖屏 { lcddev.dir=0;//竖屏 lcddev.width=240; lcddev.height=320; if(lcddev.id==0X9341||lcddev.id==0X6804) { lcddev.wramcmd=0X2C; lcddev.setxcmd=0X2A; lcddev.setycmd=0X2B; if(lcddev.id==0X6804) { lcddev.width=320; lcddev.height=480; } }else if(lcddev.id==0X8989) { lcddev.wramcmd=R34; lcddev.setxcmd=0X4E; lcddev.setycmd=0X4F; }else { lcddev.wramcmd=R34; lcddev.setxcmd=R32; lcddev.setycmd=R33; } }else if(lcddev.id!=0X6804)//6804不支持横屏显示 {

lcddev.dir=1;//横屏 lcddev.width=320; lcddev.height=240; if(lcddev.id==0X9341) { lcddev.wramcmd=0X2C; lcddev.setxcmd=0X2A; lcddev.setycmd=0X2B; }else if(lcddev.id==0X8989) { lcddev.wramcmd=R34; lcddev.setxcmd=0X4F; lcddev.setycmd=0X4E; }else { lcddev.wramcmd=R34; lcddev.setxcmd=R33; lcddev.setycmd=R32; } } LCD_Scan_Dir(DFT_SCAN_DIR); //默认扫描方向 }

//初始化lcd

//该初始化函数可以初始化各种ILI93XX液晶,但是其他函数是基于ILI9320的!!! //在其他型号的驱动芯片上没有测试! void LCD_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef readWriteTiming; FSMC_NORSRAMTimingInitTypeDef writeTiming;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE); //使能FSMC时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOG|RCC_APB2Periph_AFIO,ENABLE);//使能PORTB,D,E,G以及AFIO复用功能时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PB0 推挽输出 背光 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);

//PORTD复用推挽输出 GPIO_InitStructure.GPIO_Pin =

GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_14|GPIO_Pin_15; // //PORTD复用推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); //PORTE复用推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; // //PORTD复用推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure);

// //PORTG12复用推挽输出 A0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_12; // //PORTD复用推挽输出

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOG, &GPIO_InitStructure); readWriteTiming.FSMC_AddressSetupTime = 0x01; //地址建立时间(ADDSET)为2个HCLK 1/36M=27ns

readWriteTiming.FSMC_AddressHoldTime = 0x00; //地址保持时间(ADDHLD)模式A未用到

readWriteTiming.FSMC_DataSetupTime = 0x0f; // 数据保存时间为16个HCLK,因为液晶驱动IC的读数据的时候,速度不能太快,尤其对1289这个IC。 readWriteTiming.FSMC_BusTurnAroundDuration = 0x00; readWriteTiming.FSMC_CLKDivision = 0x00; readWriteTiming.FSMC_DataLatency = 0x00;

readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A writeTiming.FSMC_AddressSetupTime = 0x00; //地址建立时间(ADDSET)为1个HCLK

writeTiming.FSMC_AddressHoldTime = 0x00; //地址保持时间(A writeTiming.FSMC_DataSetupTime = 0x03; ////数据保存时间为4个HCLK writeTiming.FSMC_BusTurnAroundDuration = 0x00; writeTiming.FSMC_CLKDivision = 0x00; writeTiming.FSMC_DataLatency = 0x00;

writeTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A

FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;// 这里我们

使用NE4 ,也就对应BTCR[6],[7]。

FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // 不复用数据地址 FSMC_NORSRAMInitStructure.FSMC_MemoryType =FSMC_MemoryType_SRAM;// FSMC_MemoryType_SRAM; //SRAM

FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//存储器数据宽度为16bit FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode

=FSMC_BurstAccessMode_Disable;// FSMC_BurstAccessMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait=FSMC_AsynchronousWait_Disable;

FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;

FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; // 存储器写使能

FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;

FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // 读写使用不同的时序

FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;

FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; //读写时序

FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; //写时序

FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //初始化FSMC配置

FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE); // 使能BANK1

delay_ms(50); // delay 50 ms LCD_WriteReg(0x0000,0x0001); delay_ms(50); // delay 50 ms

lcddev.id = LCD_ReadReg(0x0000);

if(lcddev.id<0XFF||lcddev.id==0XFFFF||lcddev.id==0X9300)//读到ID不正确,新增lcddev.id==0X9300判断,因为9341在未被复位的情况下会被读成9300 { //尝试9341 ID的读取 LCD_WR_REG(0XD3); LCD_RD_DATA(); //dummy read LCD_RD_DATA(); //读到0X00 lcddev.id=LCD_RD_DATA(); //读取93 lcddev.id<<=8; lcddev.id|=LCD_RD_DATA(); //读取41

if(lcddev.id!=0X9341) //非9341,尝试是不是6804 { LCD_WR_REG(0XBF); LCD_RD_DATA(); //dummy read LCD_RD_DATA(); //读回0X01 LCD_RD_DATA(); //读回0XD0 lcddev.id=LCD_RD_DATA();//这里读回0X68 lcddev.id<<=8; lcddev.id|=LCD_RD_DATA();//这里读回0X04 } if(lcddev.id!=0X9341&&lcddev.id!=0X6804)lcddev.id=0x9341;//新增,用于识别9341 }

printf(\打印LCD ID if(lcddev.id==0X9341) //9341初始化 { LCD_WR_REG(0xCF); LCD_WR_DATA(0x00); LCD_WR_DATA(0xC1); LCD_WR_DATA(0X30); LCD_WR_REG(0xED); LCD_WR_DATA(0x64); LCD_WR_DATA(0x03); LCD_WR_DATA(0X12); LCD_WR_DATA(0X81); LCD_WR_REG(0xE8); LCD_WR_DATA(0x85); LCD_WR_DATA(0x10); LCD_WR_DATA(0x7A); LCD_WR_REG(0xCB); LCD_WR_DATA(0x39); LCD_WR_DATA(0x2C); LCD_WR_DATA(0x00); LCD_WR_DATA(0x34); LCD_WR_DATA(0x02); LCD_WR_REG(0xF7); LCD_WR_DATA(0x20); LCD_WR_REG(0xEA); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_REG(0xC0); //Power control LCD_WR_DATA(0x1B); //VRH[5:0] LCD_WR_REG(0xC1); //Power control LCD_WR_DATA(0x01); //SAP[2:0];BT[3:0]

LCD_WR_REG(0xC5); //VCM control LCD_WR_DATA(0x30); //3F LCD_WR_DATA(0x30); //3C

LCD_WR_REG(0xC7); //VCM control2 LCD_WR_DATA(0XB7);

LCD_WR_REG(0x36); // Memory Access Control LCD_WR_DATA(0x48); LCD_WR_REG(0x3A); LCD_WR_DATA(0x55); LCD_WR_REG(0xB1); LCD_WR_DATA(0x00); LCD_WR_DATA(0x1A); LCD_WR_REG(0xB6); LCD_WR_DATA(0x0A); LCD_WR_DATA(0xA2); LCD_WR_REG(0xF2); LCD_WR_DATA(0x00); LCD_WR_REG(0x26); LCD_WR_DATA(0x01); LCD_WR_REG(0xE0); LCD_WR_DATA(0x0F); LCD_WR_DATA(0x2A); LCD_WR_DATA(0x28); LCD_WR_DATA(0x08); LCD_WR_DATA(0x0E); LCD_WR_DATA(0x08); LCD_WR_DATA(0x54); LCD_WR_DATA(0XA9); LCD_WR_DATA(0x43); LCD_WR_DATA(0x0A); LCD_WR_DATA(0x0F); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_REG(0XE1); LCD_WR_DATA(0x00); LCD_WR_DATA(0x15); LCD_WR_DATA(0x17); LCD_WR_DATA(0x07); LCD_WR_DATA(0x11); LCD_WR_DATA(0x06); LCD_WR_DATA(0x2B); LCD_WR_DATA(0x56); // Display Function Control // 3Gamma Function Disable //Gamma curve selected //Set Gamma

//Set Gamma

LCD_WR_DATA(0x3C); LCD_WR_DATA(0x05); LCD_WR_DATA(0x10); LCD_WR_DATA(0x0F); LCD_WR_DATA(0x3F); LCD_WR_DATA(0x3F); LCD_WR_DATA(0x0F); LCD_WR_REG(0x2B); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x01); LCD_WR_DATA(0x3f); LCD_WR_REG(0x2A); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0xef); LCD_WR_REG(0x11); //Exit Sleep delay_ms(120); LCD_WR_REG(0x29); //display on }else if(lcddev.id==0x6804) //6804初始化 { LCD_WR_REG(0X11); delay_ms(20); LCD_WR_REG(0XD0);//VCI1 VCL VGH VGL DDVDH VREG1OUT power amplitude setting LCD_WR_DATA(0X07); LCD_WR_DATA(0X42); LCD_WR_DATA(0X1D); LCD_WR_REG(0XD1);//VCOMH VCOM_AC amplitude setting LCD_WR_DATA(0X00); LCD_WR_DATA(0X1a); LCD_WR_DATA(0X09); LCD_WR_REG(0XD2);//Operational Amplifier Circuit Constant Current Adjust , charge pump frequency setting LCD_WR_DATA(0X01); LCD_WR_DATA(0X22); LCD_WR_REG(0XC0);//REV SM GS LCD_WR_DATA(0X10); LCD_WR_DATA(0X3B); LCD_WR_DATA(0X00); LCD_WR_DATA(0X02); LCD_WR_DATA(0X11);

LCD_WR_REG(0XC5);// Frame rate setting = 72HZ when setting 0x03

LCD_WR_DATA(0X03);

LCD_WR_REG(0XC8);//Gamma setting LCD_WR_DATA(0X00); LCD_WR_DATA(0X25); LCD_WR_DATA(0X21); LCD_WR_DATA(0X05); LCD_WR_DATA(0X00); LCD_WR_DATA(0X0a); LCD_WR_DATA(0X65); LCD_WR_DATA(0X25); LCD_WR_DATA(0X77); LCD_WR_DATA(0X50); LCD_WR_DATA(0X0f);

LCD_WR_DATA(0X00); LCD_WR_REG(0XF8);

LCD_WR_DATA(0X01);

LCD_WR_REG(0XFE); LCD_WR_DATA(0X00); LCD_WR_DATA(0X02);

LCD_WR_REG(0X20);//Exit invert mode LCD_WR_REG(0X36);

LCD_WR_DATA(0X08);//原来是a

LCD_WR_REG(0X3A);

LCD_WR_DATA(0X55);//16位模式 LCD_WR_REG(0X2B); LCD_WR_DATA(0X00); LCD_WR_DATA(0X00); LCD_WR_DATA(0X01); LCD_WR_DATA(0X3F);

LCD_WR_REG(0X2A); LCD_WR_DATA(0X00); LCD_WR_DATA(0X00); LCD_WR_DATA(0X01); LCD_WR_DATA(0XDF); delay_ms(120);

LCD_WR_REG(0X29);

}else if(lcddev.id==0x9325)//9325 { LCD_WriteReg(0x00E5,0x78F0); LCD_WriteReg(0x0001,0x0100); LCD_WriteReg(0x0002,0x0700); LCD_WriteReg(0x0003,0x1030); LCD_WriteReg(0x0004,0x0000); LCD_WriteReg(0x0008,0x0202); LCD_WriteReg(0x0009,0x0000); LCD_WriteReg(0x000A,0x0000); LCD_WriteReg(0x000C,0x0000); LCD_WriteReg(0x000D,0x0000); LCD_WriteReg(0x000F,0x0000); //power on sequence VGHVGL LCD_WriteReg(0x0010,0x0000); LCD_WriteReg(0x0011,0x0007); LCD_WriteReg(0x0012,0x0000); LCD_WriteReg(0x0013,0x0000); LCD_WriteReg(0x0007,0x0000); //vgh LCD_WriteReg(0x0010,0x1690); LCD_WriteReg(0x0011,0x0227); //delayms(100); //vregiout LCD_WriteReg(0x0012,0x009D); //0x001b //delayms(100); //vom amplitude LCD_WriteReg(0x0013,0x1900); //delayms(100); //vom H LCD_WriteReg(0x0029,0x0025); LCD_WriteReg(0x002B,0x000D); //gamma LCD_WriteReg(0x0030,0x0007); LCD_WriteReg(0x0031,0x0303); LCD_WriteReg(0x0032,0x0003);// 0006 LCD_WriteReg(0x0035,0x0206); LCD_WriteReg(0x0036,0x0008); LCD_WriteReg(0x0037,0x0406); LCD_WriteReg(0x0038,0x0304);//0200 LCD_WriteReg(0x0039,0x0007); LCD_WriteReg(0x003C,0x0602);// 0504 LCD_WriteReg(0x003D,0x0008); //ram

LCD_WriteReg(0x0050,0x0000); LCD_WriteReg(0x0051,0x00EF); LCD_WriteReg(0x0052,0x0000); LCD_WriteReg(0x0053,0x013F); LCD_WriteReg(0x0060,0xA700); LCD_WriteReg(0x0061,0x0001); LCD_WriteReg(0x006A,0x0000); // LCD_WriteReg(0x0080,0x0000); LCD_WriteReg(0x0081,0x0000); LCD_WriteReg(0x0082,0x0000); LCD_WriteReg(0x0083,0x0000); LCD_WriteReg(0x0084,0x0000); LCD_WriteReg(0x0085,0x0000); // LCD_WriteReg(0x0090,0x0010); LCD_WriteReg(0x0092,0x0600); LCD_WriteReg(0x0007,0x0133); LCD_WriteReg(0x00,0x0022);// }else if(lcddev.id==0x9328)//ILI9328 OK { LCD_WriteReg(0x00EC,0x108F);// internal timeing LCD_WriteReg(0x00EF,0x1234);// ADD //LCD_WriteReg(0x00e7,0x0010);

//LCD_WriteReg(0x0000,0x0001);//开启内部时钟 LCD_WriteReg(0x0001,0x0100);

LCD_WriteReg(0x0002,0x0700);//电源开启 //LCD_WriteReg(0x0003,(1<<3)|(1<<4) ); //65K RGB //DRIVE TABLE(寄存器 03H) //BIT3=AM BIT4:5=ID0:1 //AM ID0 ID1 FUNCATION // 0 0 0 R->L D->U // 1 0 0 D->U R->L // 0 1 0 L->R D->U // 1 1 0 D->U L->R // 0 0 1 R->L U->D // 1 0 1 U->D R->L // 0 1 1 L->R U->D 正常就用这个. // 1 1 1 U->D L->R

LCD_WriteReg(0x0003,(1<<12)|(3<<4)|(0<<3) );//65K

LCD_WriteReg(0x0004,0x0000); LCD_WriteReg(0x0008,0x0202); LCD_WriteReg(0x0009,0x0000);

LCD_WriteReg(0x00A4,0x0001);//NVM LCD_WriteReg(0x0008,0x000F); LCD_WriteReg(0x000A,0x0008);

LCD_WriteReg(0x000D,0x0008); //伽马校正

LCD_WriteReg(0x0030,0x0707);

LCD_WriteReg(0x0031,0x0007); //0x0707 LCD_WriteReg(0x0032,0x0603); LCD_WriteReg(0x0033,0x0700); LCD_WriteReg(0x0034,0x0202);

LCD_WriteReg(0x0035,0x0002); //?0x0606 LCD_WriteReg(0x0036,0x1F0F);

LCD_WriteReg(0x0037,0x0707); //0x0f0f 0x0105 LCD_WriteReg(0x0038,0x0000); LCD_WriteReg(0x0039,0x0000); LCD_WriteReg(0x003A,0x0707);

LCD_WriteReg(0x003B,0x0000); //0x0303 LCD_WriteReg(0x003C,0x0007); //?0x0707

LCD_WriteReg(0x003D,0x0000); //0x1313//0x1f08 delay_ms(50);

LCD_WriteReg(0x0007,0x0001);

LCD_WriteReg(0x0017,0x0001);//开启电源 delay_ms(50); //电源配置

LCD_WriteReg(0x0010,0x17A0);

LCD_WriteReg(0x0011,0x0217);//reference voltage VC[2:0] Vciout = 1.00*Vcivl LCD_WriteReg(0x0012,0x011E);//0x011c //Vreg1out = Vcilvl*1.80 is it the same as Vgama1out ?

LCD_WriteReg(0x0013,0x0F00);//VDV[4:0]-->VCOM Amplitude VcomL = VcomH - Vcom Ampl

LCD_WriteReg(0x002A,0x0000);

LCD_WriteReg(0x0029,0x000A);//0x0001F Vcomh = VCM1[4:0]*Vreg1out gate source voltage??

LCD_WriteReg(0x0012,0x013E);// 0x013C power supply on //Coordinates Control//

LCD_WriteReg(0x0050,0x0000);//0x0e00 LCD_WriteReg(0x0051,0x00EF); LCD_WriteReg(0x0052,0x0000); LCD_WriteReg(0x0053,0x013F); //Pannel Image Control//

LCD_WriteReg(0x0060,0x2700); LCD_WriteReg(0x0061,0x0001); LCD_WriteReg(0x006A,0x0000); LCD_WriteReg(0x0080,0x0000);

//Partial Image Control//

LCD_WriteReg(0x0081,0x0000); LCD_WriteReg(0x0082,0x0000); LCD_WriteReg(0x0083,0x0000); LCD_WriteReg(0x0084,0x0000); LCD_WriteReg(0x0085,0x0000); //Panel Interface Control//

LCD_WriteReg(0x0090,0x0013);//0x0010 frenqucy LCD_WriteReg(0x0092,0x0300); LCD_WriteReg(0x0093,0x0005); LCD_WriteReg(0x0095,0x0000); LCD_WriteReg(0x0097,0x0000); LCD_WriteReg(0x0098,0x0000);

LCD_WriteReg(0x0001,0x0100); LCD_WriteReg(0x0002,0x0700);

LCD_WriteReg(0x0003,0x1038);//扫描方向 上->下 LCD_WriteReg(0x0004,0x0000); LCD_WriteReg(0x000C,0x0000); LCD_WriteReg(0x000F,0x0000); LCD_WriteReg(0x0020,0x0000); LCD_WriteReg(0x0021,0x0000); LCD_WriteReg(0x0007,0x0021); delay_ms(20);

LCD_WriteReg(0x0007,0x0061); delay_ms(20);

LCD_WriteReg(0x0007,0x0173); delay_ms(20); }else if(lcddev.id==0xB505) { LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x00a4,0x0001); delay_ms(20); LCD_WriteReg(0x0060,0x2700); LCD_WriteReg(0x0008,0x0202); LCD_WriteReg(0x0030,0x0214); LCD_WriteReg(0x0031,0x3715); LCD_WriteReg(0x0032,0x0604); LCD_WriteReg(0x0033,0x0e16);

左->右 LCD_WriteReg(0x0034,0x2211); LCD_WriteReg(0x0035,0x1500); LCD_WriteReg(0x0036,0x8507); LCD_WriteReg(0x0037,0x1407); LCD_WriteReg(0x0038,0x1403); LCD_WriteReg(0x0039,0x0020); LCD_WriteReg(0x0090,0x001a); LCD_WriteReg(0x0010,0x0000); LCD_WriteReg(0x0011,0x0007); LCD_WriteReg(0x0012,0x0000); LCD_WriteReg(0x0013,0x0000); delay_ms(20); LCD_WriteReg(0x0010,0x0730); LCD_WriteReg(0x0011,0x0137); delay_ms(20); LCD_WriteReg(0x0012,0x01b8); delay_ms(20); LCD_WriteReg(0x0013,0x0f00); LCD_WriteReg(0x002a,0x0080); LCD_WriteReg(0x0029,0x0048); delay_ms(20); LCD_WriteReg(0x0001,0x0100); LCD_WriteReg(0x0002,0x0700);

LCD_WriteReg(0x0003,0x1038);//扫描方向 上->下LCD_WriteReg(0x0008,0x0202); LCD_WriteReg(0x000a,0x0000); LCD_WriteReg(0x000c,0x0000); LCD_WriteReg(0x000d,0x0000); LCD_WriteReg(0x000e,0x0030); LCD_WriteReg(0x0050,0x0000); LCD_WriteReg(0x0051,0x00ef); LCD_WriteReg(0x0052,0x0000); LCD_WriteReg(0x0053,0x013f); LCD_WriteReg(0x0060,0x2700); LCD_WriteReg(0x0061,0x0001); LCD_WriteReg(0x006a,0x0000); //LCD_WriteReg(0x0080,0x0000); //LCD_WriteReg(0x0081,0x0000); LCD_WriteReg(0x0090,0X0011);

左->右 LCD_WriteReg(0x0092,0x0600); LCD_WriteReg(0x0093,0x0402); LCD_WriteReg(0x0094,0x0002); delay_ms(20); LCD_WriteReg(0x0007,0x0001); delay_ms(20); LCD_WriteReg(0x0007,0x0061); LCD_WriteReg(0x0007,0x0173); LCD_WriteReg(0x0020,0x0000); LCD_WriteReg(0x0021,0x0000); LCD_WriteReg(0x00,0x22); }else if(lcddev.id==0xC505) { LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); delay_ms(20); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x0000,0x0000); LCD_WriteReg(0x00a4,0x0001); delay_ms(20); LCD_WriteReg(0x0060,0x2700); LCD_WriteReg(0x0008,0x0806); LCD_WriteReg(0x0030,0x0703);//gamma setting LCD_WriteReg(0x0031,0x0001); LCD_WriteReg(0x0032,0x0004); LCD_WriteReg(0x0033,0x0102); LCD_WriteReg(0x0034,0x0300); LCD_WriteReg(0x0035,0x0103); LCD_WriteReg(0x0036,0x001F); LCD_WriteReg(0x0037,0x0703); LCD_WriteReg(0x0038,0x0001); LCD_WriteReg(0x0039,0x0004); LCD_WriteReg(0x0090, 0x0015); //80Hz LCD_WriteReg(0x0010, 0X0410); //BT,AP LCD_WriteReg(0x0011,0x0247); //DC1,DC0,VC LCD_WriteReg(0x0012, 0x01BC);

LCD_WriteReg(0x0013, 0x0e00); delay_ms(120); LCD_WriteReg(0x0001, 0x0100); LCD_WriteReg(0x0002, 0x0200); LCD_WriteReg(0x0003, 0x1030); LCD_WriteReg(0x000A, 0x0008); LCD_WriteReg(0x000C, 0x0000); LCD_WriteReg(0x000E, 0x0020); LCD_WriteReg(0x000F, 0x0000); LCD_WriteReg(0x0020, 0x0000); //H Start LCD_WriteReg(0x0021, 0x0000); //V Start LCD_WriteReg(0x002A,0x003D); //vcom2 delay_ms(20); LCD_WriteReg(0x0029, 0x002d); LCD_WriteReg(0x0050, 0x0000); LCD_WriteReg(0x0051, 0xD0EF); LCD_WriteReg(0x0052, 0x0000); LCD_WriteReg(0x0053, 0x013F); LCD_WriteReg(0x0061, 0x0000); LCD_WriteReg(0x006A, 0x0000); LCD_WriteReg(0x0092,0x0300); LCD_WriteReg(0x0093, 0x0005); LCD_WriteReg(0x0007, 0x0100); }else if(lcddev.id==0x8989)//OK |/|/| { LCD_WriteReg(0x0000,0x0001);//打开晶振 LCD_WriteReg(0x0003,0xA8A4);//0xA8A4 LCD_WriteReg(0x000C,0x0000); LCD_WriteReg(0x000D,0x080C); LCD_WriteReg(0x000E,0x2B00); LCD_WriteReg(0x001E,0x00B0); LCD_WriteReg(0x0001,0x2B3F);//驱动输出控制320*240 0x6B3F LCD_WriteReg(0x0002,0x0600); LCD_WriteReg(0x0010,0x0000); LCD_WriteReg(0x0011,0x6078); //定义数据格式 16位色 横屏 0x6058 LCD_WriteReg(0x0005,0x0000); LCD_WriteReg(0x0006,0x0000); LCD_WriteReg(0x0016,0xEF1C); LCD_WriteReg(0x0017,0x0003); LCD_WriteReg(0x0007,0x0233); //0x0233 LCD_WriteReg(0x000B,0x0000);

LCD_WriteReg(0x000F,0x0000); //扫描开始地址 LCD_WriteReg(0x0041,0x0000); LCD_WriteReg(0x0042,0x0000); LCD_WriteReg(0x0048,0x0000); LCD_WriteReg(0x0049,0x013F); LCD_WriteReg(0x004A,0x0000); LCD_WriteReg(0x004B,0x0000); LCD_WriteReg(0x0044,0xEF00); LCD_WriteReg(0x0045,0x0000); LCD_WriteReg(0x0046,0x013F); LCD_WriteReg(0x0030,0x0707); LCD_WriteReg(0x0031,0x0204); LCD_WriteReg(0x0032,0x0204); LCD_WriteReg(0x0033,0x0502); LCD_WriteReg(0x0034,0x0507); LCD_WriteReg(0x0035,0x0204); LCD_WriteReg(0x0036,0x0204); LCD_WriteReg(0x0037,0x0502); LCD_WriteReg(0x003A,0x0302); LCD_WriteReg(0x003B,0x0302); LCD_WriteReg(0x0023,0x0000); LCD_WriteReg(0x0024,0x0000); LCD_WriteReg(0x0025,0x8000); LCD_WriteReg(0x004f,0); //行首址0 LCD_WriteReg(0x004e,0); //列首址0 }else if(lcddev.id==0x4531)//OK |/|/| { LCD_WriteReg(0X00,0X0001); delay_ms(10); LCD_WriteReg(0X10,0X1628); LCD_WriteReg(0X12,0X000e);//0x0006 LCD_WriteReg(0X13,0X0A39); delay_ms(10); LCD_WriteReg(0X11,0X0040); LCD_WriteReg(0X15,0X0050); delay_ms(10); LCD_WriteReg(0X12,0X001e);//16 delay_ms(10); LCD_WriteReg(0X10,0X1620); LCD_WriteReg(0X13,0X2A39); delay_ms(10); LCD_WriteReg(0X01,0X0100); LCD_WriteReg(0X02,0X0300); LCD_WriteReg(0X03,0X1038);//改变方向的

LCD_WriteReg(0X08,0X0202); LCD_WriteReg(0X0A,0X0008); LCD_WriteReg(0X30,0X0000); LCD_WriteReg(0X31,0X0402); LCD_WriteReg(0X32,0X0106); LCD_WriteReg(0X33,0X0503); LCD_WriteReg(0X34,0X0104); LCD_WriteReg(0X35,0X0301); LCD_WriteReg(0X36,0X0707); LCD_WriteReg(0X37,0X0305); LCD_WriteReg(0X38,0X0208); LCD_WriteReg(0X39,0X0F0B); LCD_WriteReg(0X41,0X0002); LCD_WriteReg(0X60,0X2700); LCD_WriteReg(0X61,0X0001); LCD_WriteReg(0X90,0X0210); LCD_WriteReg(0X92,0X010A); LCD_WriteReg(0X93,0X0004); LCD_WriteReg(0XA0,0X0100); LCD_WriteReg(0X07,0X0001); LCD_WriteReg(0X07,0X0021); LCD_WriteReg(0X07,0X0023); LCD_WriteReg(0X07,0X0033); LCD_WriteReg(0X07,0X0133); LCD_WriteReg(0XA0,0X0000); }else if(lcddev.id==0x4535) { LCD_WriteReg(0X15,0X0030); LCD_WriteReg(0X9A,0X0010); LCD_WriteReg(0X11,0X0020); LCD_WriteReg(0X10,0X3428); LCD_WriteReg(0X12,0X0002);//16 LCD_WriteReg(0X13,0X1038); delay_ms(40); LCD_WriteReg(0X12,0X0012);//16 delay_ms(40); LCD_WriteReg(0X10,0X3420); LCD_WriteReg(0X13,0X3038); delay_ms(70); LCD_WriteReg(0X30,0X0000); LCD_WriteReg(0X31,0X0402); LCD_WriteReg(0X32,0X0307); LCD_WriteReg(0X33,0X0304); LCD_WriteReg(0X34,0X0004);

LCD_WriteReg(0X35,0X0401); LCD_WriteReg(0X36,0X0707); LCD_WriteReg(0X37,0X0305); LCD_WriteReg(0X38,0X0610); LCD_WriteReg(0X39,0X0610); LCD_WriteReg(0X01,0X0100); LCD_WriteReg(0X02,0X0300); LCD_WriteReg(0X03,0X1030);//改变方向的 LCD_WriteReg(0X08,0X0808); LCD_WriteReg(0X0A,0X0008); LCD_WriteReg(0X60,0X2700); LCD_WriteReg(0X61,0X0001); LCD_WriteReg(0X90,0X013E); LCD_WriteReg(0X92,0X0100); LCD_WriteReg(0X93,0X0100); LCD_WriteReg(0XA0,0X3000); LCD_WriteReg(0XA3,0X0010); LCD_WriteReg(0X07,0X0001); LCD_WriteReg(0X07,0X0021); LCD_WriteReg(0X07,0X0023); LCD_WriteReg(0X07,0X0033); LCD_WriteReg(0X07,0X0133); } LCD_Display_Dir(0); //默认为竖屏 LCD_LED=1; //点亮背光 LCD_Clear(WHITE); }

//清屏函数

//color:要清屏的填充色 void LCD_Clear(u16 color) { u32 index=0; u32 totalpoint=lcddev.width; totalpoint*=lcddev.height; //得到总点数 LCD_SetCursor(0x00,0x0000); //设置光标位置 LCD_WriteRAM_Prepare(); //开始写入GRAM for(index=0;indexLCD_RAM=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_WriteRAM_Prepare(); //开始写入GRAM for(j=0;j

//在指定区域内填充指定颜色块

//(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;iLCD_RAM=color[i*height+j];//写入数据 } } //画线

//x1,y1:起点坐标 //x2,y2:终点坐标

void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2) { 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);//画点 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) { LCD_DrawLine(x1,y1,x2,y1); LCD_DrawLine(x1,y1,x1,y2); LCD_DrawLine(x1,y2,x2,y2); LCD_DrawLine(x2,y1,x2,y2); }

//在指定位置画一个指定大小的圆 //(x,y):中心点 //r :半径

void Draw_Circle(u16 x0,u16 y0,u8 r) { int a,b; int di; a=0;b=r; di=3-(r<<1); //判断下个点位置的标志 while(a<=b) { LCD_DrawPoint(x0+a,y0-b); //5 LCD_DrawPoint(x0+b,y0-a); //0

LCD_DrawPoint(x0+b,y0+a); //4 LCD_DrawPoint(x0+a,y0+b); //6 LCD_DrawPoint(x0-a,y0+b); //1 LCD_DrawPoint(x0-b,y0+a); LCD_DrawPoint(x0-a,y0-b); //2 LCD_DrawPoint(x0-b,y0-a); //7 a++; //使用Bresenham算法画圆 if(di<0)di +=4*a+6; else { di+=10+4*(a-b); b--; } } } //在指定位置显示一个字符 //x,y:起始坐标

//num:要显示的字符:\//size:字体大小 12/16

//mode:叠加方式(1)还是非叠加方式(0)

void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode) { u8 temp,t1,t; u16 y0=y; u16 colortemp=POINT_COLOR; //设置窗口 num=num-' ';//得到偏移后的值 if(!mode) //非叠加方式 { for(t=0;t=lcddev.width){POINT_COLOR=colortemp;return;}//超区域了 if((y-y0)==size) {

y=y0; x++; if(x>=lcddev.width){POINT_COLOR=colortemp;return;}//超区域了 break; } } } }else//叠加方式 { for(t=0;t=lcddev.height){POINT_COLOR=colortemp;return;}//超区域了 if((y-y0)==size) { y=y0; x++; if(x>=lcddev.width){POINT_COLOR=colortemp;return;}//超区域了 break; } } } } POINT_COLOR=colortemp; }

//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

//显示数字,高位为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

} 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++; } }

#ifndef __LCD_H #define __LCD_H #include \ #include \

//////////////////////////////////////////////////////////////////////////////////

//本程序只供学习使用,未经作者许可,不得用于其它任何用途 //ALIENTEK战舰STM32开发板 //2.4/2.8寸TFT液晶驱动 //支持驱动IC型号包括:ILI9341/ILI9325/RM68021/ILI9320/ILI9328/LGDP4531/LGDP4535/SPFD5408/SSD1289/1505/B505/C505等 //正点原子@ALIENTEK

//技术论坛:www.openedv.com //修改日期:2012/9/11

//版本:V1.9

//版权所有,盗版必究。

//Copyright(C) 广州市星翼电子科技有限公司 2009-2019 //All rights reserved

//********************************************************************************

//V1.2修改说明

//支持了SPFD5408的驱动,另外把液晶ID直接打印成HEX格式.方便查看LCD驱动IC. //V1.3

//加入了快速IO的支持

//修改了背光控制的极性(适用于V1.8及以后的开发板版本)

//对于1.8版本之前(不包括1.8)的液晶模块,请修改LCD_Init函数的LCD_LED=1;为LCD_LED=1; //V1.4

//修改了LCD_ShowChar函数,使用画点功能画字符。 //加入了横竖屏显示的支持 //V1.5 20110730

//1,修改了B505液晶读颜色有误的bug. //2,修改了快速IO及横竖屏的设置方式. //V1.6 20111116

//1,加入对LGDP4535液晶的驱动支持 //V1.7 20120713

//1,增加LCD_RD_DATA函数 //2,增加对ILI9341的支持

//3,增加ILI9325的独立驱动代码

//4,增加LCD_Scan_Dir函数(慎重使用)

//6,另外修改了部分原来的函数,以适应9341的操作 //V1.8 20120905

//1,加入LCD重要参数设置结构体lcddev

//2,加入LCD_Display_Dir函数,支持在线横竖屏切换 //V1.9 20120911

//1,新增RM68042驱动(ID:6804),但是6804不支持横屏显示!!原因:改变扫描方式, //导致6804坐标设置失效,试过很多方法都不行,暂时无解。 //////////////////////////////////////////////////////////////////////////////////

//LCD重要参数集 typedef struct { u16 width; //LCD 宽度 u16 height; //LCD 高度 u16 id; //LCD ID u8 dir; //横屏还是竖屏控制:0,竖屏;1,横屏。 u8 wramcmd; //开始写gram指令

u8 setxcmd; //设置x坐标指令 u8 setycmd; //设置y坐标指令 }_lcd_dev;

//LCD参数

extern _lcd_dev lcddev; //管理LCD重要参数 //LCD的画笔颜色和背景色

extern u16 POINT_COLOR;//默认红色

extern u16 BACK_COLOR; //背景颜色.默认为白色

////////////////////////////////////////////////////////////////////////////////// //-----------------LCD端口定义---------------- #define LCD_LED PBout(0) //LCD背光 PB0 //LCD地址结构体 typedef struct { u16 LCD_REG; u16 LCD_RAM; } LCD_TypeDef;

//使用NOR/SRAM的 Bank1.sector4,地址位HADDR[27,26]=11 A10作为数据命令区分线 //注意设置时STM32内部会右移一位对其! 111110=0X3E #define LCD_BASE ((u32)(0x6C000000 | 0x000007FE)) #define LCD ((LCD_TypeDef *) LCD_BASE) //////////////////////////////////////////////////////////////////////////////////

//扫描方向定义

#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 DFT_SCAN_DIR L2R_U2D //默认的扫描方向

//画笔颜色

#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 //灰色 //GUI颜色

#define DARKBLUE 0X01CF //深蓝色 #define LIGHTBLUE 0X7D7C //浅蓝色 #define GRAYBLUE 0X5458 //灰蓝色 //以上三色为PANEL的颜色

#define LIGHTGREEN 0X841F //浅绿色

//#define LIGHTGRAY 0XEF5B //浅灰色(PANNEL) #define LGRAY 0XC618 //浅灰色(PANNEL),窗体背景色

#define LGRAYBLUE 0XA651 //浅灰蓝色(中间层颜色) #define LBBLUE 0X2B12 //浅棕蓝色(选择条目的反色) void LCD_Init(void); 化

void LCD_DisplayOn(void); 示

void LCD_DisplayOff(void); 示

void LCD_Clear(u16 Color); void LCD_SetCursor(u16 Xpos, u16 Ypos); 光标

void LCD_DrawPoint(u16 x,u16 y); void LCD_Fast_DrawPoint(u16 x,u16 y,u16 color); 画点

u16 LCD_ReadPoint(u16 x,u16 y); void Draw_Circle(u16 x0,u16 y0,u8 r); void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2); void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2); 形

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字体

void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue); u16 LCD_ReadReg(u8 LCD_Reg); void LCD_WriteRAM_Prepare(void);

void LCD_WriteRAM(u16 RGB_Code); void LCD_Scan_Dir(u8 dir); void LCD_Display_Dir(u8 dir); //9320/9325 LCD寄存器 #define R0 0x00 #define R1 0x01 #define R2 0x02 #define R3 0x03 #define R4 0x04 #define R5 0x05 #define R6 0x06 #define R7 0x07 #define R8 0x08 #define R9 0x09 #define R10 0x0A #define R12 0x0C #define R13 0x0D #define R14 0x0E #define R15 0x0F #define R16 0x10 #define R17 0x11 #define R18 0x12 #define R19 0x13 #define R20 0x14 #define R21 0x15 #define R22 0x16 #define R23 0x17 #define R24 0x18 #define R25 0x19 #define R26 0x1A #define R27 0x1B

//设置屏扫描方向 //设置屏幕显示方向 #define R28 0x1C #define R29 0x1D #define R30 0x1E #define R31 0x1F #define R32 0x20 #define R33 0x21 #define R34 0x22 #define R36 0x24 #define R37 0x25 #define R40 #define R41 #define R43 #define R45 #define R48 #define R49 #define R50 #define R51 #define R52 #define R53 #define R54 #define R55 #define R56 #define R57 #define R59 #define R60 #define R61 #define R62 #define R63 #define R64 #define R65 #define R66 #define R67 #define R68 #define R69 #define R70 #define R71 #define R72 #define R73 #define R74 #define R75 #define R76 #define R77 #define R78 #define R79 0x28 0x29 0x2B 0x2D 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3B 0x3C 0x3D 0x3E 0x3F 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F

#define R80 0x50 #define R81 0x51 #define R82 0x52 #define R83 0x53 #define R96 0x60 #define R97 0x61 #define R106 0x6A #define R118 0x76 #define R128 0x80 #define R129 #define R130 #define R131 #define R132 #define R133 #define R134 #define R135 #define R136 #define R137 #define R139 #define R140 #define R141 #define R143 #define R144 #define R145 #define R146 #define R147 #define R148 #define R149 #define R150 #define R151 #define R152 #define R153 #define R154 #define R157 #define R192 #define R193 #define R229 #endif

0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8B 0x8C 0x8D 0x8F 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9A 0x9D 0xC0 0xC1 0xE5

本文来源:https://www.bwwdw.com/article/7k5w.html

Top