首页->【玩CortexM0】

124 0

实验五 LPC824驱动黑白LCD显示

实验五及以后采用ISP或JLINK取决于手头有什么了,实验中不再介绍怎么下载了。

实验三、四完成了GPIO口的实验,为了以后做实验具有结果可视性,我们首先完成LCD的驱显实验。为通用性我们先做128X64dots的黑白LCD实验。LCD见照片51.51-几种128x64 LCD屏.JPG

这款板子的电源引线太少,需要扩充。此板厂商刻意将引脚按照51芯片的方式引出,可以直接插在51开发板上。因此,先找51开发板直接用。如果没有51开发板,也可以按照此板的引脚逻辑找一个40脚的插座和洞洞板,自己焊一个。见照片5353-洞洞板焊接的扩展板.JPG

为了照顾没有玩过51的网友,我们直接用洞洞板见照片54。54-用洞洞板做的扩展板.JPG

各种扩展板见照片52。52-洞洞板和51开发板.JPG

这款LCD通常支持8位并口数据+4[5]根控制线的和5[4]根数据线串口的屏,为了简单开始,从5[4]根串口LCD开始。

串口的LCD无论什么型号和分辨率,大致上最多可以化简为7[6]根线:

+V——电源正,通常3.3V[5V]

GND——电源地

sdin——串口数据

sclk——串口时钟

sreg——LCD命令/数据

sres——LCD复位

schi——LCD片选

由于只有1个LCD,因此将片选接地或通过电阻接地即可。这样只要前述的4根信号线+2根电源线即可。为了看的清除,此次用大的LCD。

在制作我们的项目过程可能需要自己编一些include文档,因此我们在原来的目录下创建一个inc目录,并且在项目中将此目录以gorup加入进来,以后自编的include文档就拷贝到这个文件夹中;而在项目中将自编的include文件加入的这个项目目录中。

设置和显示LCD是比较麻烦的事情:需要了解LCD驱动芯片和信号逻辑,而后需要自己变成来完成接口逻辑,为了提高通用性和简单,我们采用模拟串口方式来模拟所谓的8080或6800读写方式。

在这里先介绍C语言的一个特点:编译语言模块化编程占用空间小,执行速度快。这我已经在其他MCU上做过测试,大约在500行代码上编译后能节省大约1/8~1/10的存储空间。

LCD取现需要如下一些准备工作:

准备显示的字库[有些LCD自带字库,此步可省],本例按照廉价的五无字库的LCD;选择控制信号的GPIO口,我们选连续的23,24,25,26这4个IO口。硬件连接就是如此了。

软件考虑几个模块:

 a.产生sclk,LCD要求写数据或命令采用sclk的上升沿——sclk模块;

 b.完成将并行数据改成串行数据写模块——wri();

 c.设置字符显示在LCD屏幕位置的坐标模块——setXY();

 d.将1个ASCII码[如果是8X8点阵的1个ASCII码有8个字节]转成字节输出模块——b2l();

 e.清除屏幕模块——lcdClear();

 f.LCD初始化模块——lcdInit();实际上无论何种显示屏,只是芯片不一样驱动几乎差不多,会一种可以举一反三的。当然黑白与TFT的差别较大一些。只要改这个就可以了——除非显示方式转90度例外;

 g.显示所有ASCII码满屏测试LCD程序——dispAsc()

这几个模块与主程序无关,只与LCD有关,因此将定义GPIO口和a~f模块都写入创建的12864.inc文件中,代码如下:

#include "mbed.h"


DigitalOut sres(P0_23);

DigitalOut sreg(P0_24);

DigitalOut sclk(P0_25);

DigitalOut sdat(P0_26);


//128x64 LCD ASCII Codes

const unsigned char asc2[][8]={//8x6 Dots 32 to 127 + caratteri alfa_omega a seguire

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//space

{0x00,0x1c,0x22,0x24,0x18,0x16,0x00,0x00},//!-1=32

{0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00},//"

{0x00,0x02,0x04,0x78,0x10,0x0c,0x02,0x00},//#

{0x00,0x7e,0x15,0x15,0x15,0x0a,0x00,0x00},//$

{0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00},//%

{0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00},//&

{0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00},//'

{0x00,0x00,0x1C,0x22,0x41,0x00,0x00,0x00},//(

{0x00,0x00,0x41,0x22,0x1C,0x00,0x00,0x00},//)

{0x00,0x14,0x08,0x3E,0x08,0x14,0x00,0x00},//  /-10=42

{0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00},//+

{0x00,0x00,0x50,0x30,0x00,0x00,0x00,0x00},//;

{0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00},//-

{0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00},//.

{0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00},///

{0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},//0

{0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},//1

{0x00,0x42,0x61,0x51,0x49,0x46,0x00,0x00},//2

{0x00,0x21,0x41,0x45,0x4B,0x31,0x00,0x00},//3

{0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},//4-20

{0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},//5

{0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00},//6

{0x00,0x01,0x01,0x79,0x05,0x03,0x00,0x00},//7

{0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},//8

{0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00},//9

{0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},//:

{0x00,0x00,0x56,0x36,0x00,0x00,0x00,0x00},//;

{0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00},//<

{0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00},//=

{0x00,0x00,0x41,0x22,0x14,0x08,0x00,0x00},//>-30

{0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00},//?

{0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00},//@

{0x00,0x7E,0x11,0x11,0x11,0x7E,0x00,0x00},//A

{0x00,0x41,0x7F,0x49,0x49,0x36,0x00,0x00},//B

{0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00},//C

{0x00,0x41,0x7F,0x41,0x41,0x3E,0x00,0x00},//D

{0x00,0x7F,0x49,0x49,0x49,0x49,0x00,0x00},//E

{0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00},//F

{0x00,0x3E,0x41,0x41,0x49,0x7A,0x00,0x00},//G

{0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00},//H-40

{0x00,0x00,0x41,0x7F,0x41,0x00,0x00,0x00},//I

{0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00},//J

{0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00},//K

{0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00},//L

{0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00},//M

{0x00,0x7F,0x06,0x08,0x30,0x7F,0x00,0x00},//N

{0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00},//O

{0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00},//P

{0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00},//Q

{0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00},//R-50

{0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00},//S

{0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00},//T

{0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00},//U

{0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00},//V

{0x00,0x7F,0x20,0x18,0x20,0x7F,0x00,0x00},//W

{0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00},//X

{0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x00},//Y

{0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00},//Z

{0x00,0x00,0x7F,0x41,0x41,0x00,0x00,0x00},//[

{0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00},//\//-60

{0x00,0x00,0x41,0x41,0x7F,0x00,0x00,0x00},//]

{0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00},//^

{0x00,0x40,0x40,0x40,0x40,0x40,0x00,0x00},//_

{0x00,0x00,0x00,0x07,0x05,0x07,0x00,0x00},//' Insty 

{0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00},//a

{0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00},//b

{0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x00},//c

{0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00},//d

{0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00},//e

{0x00,0x00,0x08,0x7E,0x09,0x02,0x00,0x00},//f-70 

{0x00,0x0C,0x52,0x52,0x4C,0x3E,0x00,0x00},//g

{0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},//h

{0x00,0x00,0x44,0x7D,0x40,0x00,0x00,0x00},//i

{0x00,0x20,0x40,0x44,0x3D,0x00,0x00,0x00},//j

{0x00,0x00,0x7F,0x10,0x28,0x44,0x00,0x00},//k

{0x00,0x00,0x41,0x7F,0x40,0x00,0x00,0x00},//l

{0x00,0x7C,0x04,0x78,0x04,0x78,0x00,0x00},//m

{0x00,0x7C,0x08,0x04,0x04,0x78,0x00,0x00},//n

{0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00},//o

{0x00,0x7E,0x0C,0x12,0x12,0x0C,0x00,0x00},//p-80

{0x00,0x0C,0x12,0x12,0x0C,0x7E,0x00,0x00},//q

{0x00,0x7C,0x08,0x04,0x04,0x08,0x00,0x00},//r

{0x00,0x58,0x54,0x54,0x54,0x64,0x00,0x00},//s

{0x00,0x04,0x3F,0x44,0x40,0x20,0x00,0x00},//t

{0x00,0x3C,0x40,0x40,0x3C,0x40,0x00,0x00},//u

{0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},//v

{0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00},//w

{0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},//x

{0x00,0x1C,0xA0,0xA0,0x90,0x7C,0x00,0x00},//y

{0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},//z-90

{0x00,0x00,0x08,0x36,0x41,0x00,0x00,0x00},//{

{0x00,0x00,0x00,0x77,0x00,0x00,0x00,0x00},//|

{0x00,0x00,0x41,0x36,0x08,0x00,0x00,0x00},//}

{0x00,0x08,0x04,0x08,0x10,0x08,0x00,0x00},//~

{0x00,0x5c,0x62,0x02,0x62,0x5c,0x00,0x00},//OM

{0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14},//horiz lines-96

};

//Delayer

void delay0(int counter){

 int i,j;

 for(i=0;i<counter;i++){for(j=0;j<i;j++){;}}

}

//Write Plus

void wri(void){

 sclk=0;

 sclk=1;

}

//a B Data to LCD,dt:data,com:1-data/0-command

void b2l(unsigned char dt,unsigned char com){

 unsigned char i;

 if(com) sreg=1;else sreg=0;

 for(i=0;i<8;i++){

  if(dt&0x80){sdat=1;}else{sdat=0;};

  dt=dt<<1;

  wri();

 }

}

//Set X Y

void setXY(unsigned char pag,unsigned char col){

 b2l(0xB0+pag,0);//page3~0第1~第3行

 col+=4;//Remark/\

 b2l(0x10+((col&0xF0)>>4),0);//row-H4

 b2l(col&0x0F,0);//row-L4

}

//Display ASCII

void c2l(unsigned char pag,unsigned char col,unsigned char c){

 unsigned char line;

 setXY(pag,col*6);

 for(line=0;line<6;line++) b2l(asc2[c][line],1);

}

//Clear LCD

void lcdClear(void){

 unsigned char pag,col;

 for(pag=0xB8;pag>=0xB0;pag--){

  b2l(pag,0);

  b2l(0x10,0);

  b2l(0x00,0);

  for(col=0;col<132;col++){b2l(0x00,1);}

 }

}

//12864LCD初始化,参数:X

void lcdInit(void){

 sres=0;

 delay0(2000);

 sres=1;

 delay0(2000);

 b2l(0xAE,0);//AE=Disp Off/AF=Disp On

 b2l(0xE2,0);//LCD Initial

 b2l(0xA1,0);//Refresh Colum:A0=1-->132/A1=132-->1

 b2l(0xC0,0);//Refresh COM:C0=1-->64/C8=64-->1||A0+C8:col--X-->4

 b2l(0xA3,0);//A2=1/65=1/7__A3=1/65=1/9

 delay0(190);

 b2l(0x2F,0);//Power Mode:28+VC+VR+VF

 delay0(100);

 b2l(0x23,0);//20->27=1.9,2.19,2.55,3.02,3.61,4.35,5.29,6.48V/Bian Vol

 delay0(100);

 b2l(0x81,0);//V0 Vol,After:Num

 b2l(0x14,0);//000000~111111=0~63=8.83~11.17V/30=10V

 delay0(100);

 b2l(0xA4,0);//A4-All Off/A5-All On

 b2l(0x10,0);//Column High 4 start:0x10+0000->1111

 b2l(0x00,0);//Column Low 4 start:0x10+0000->1111

 b2l(0x40,0);//40+xxxxxx=Disp From 0->63

 b2l(0x10,0);//10+xxxx=Disp Column Migh 4 bts

 b2l(0x00,0);//00+xxxx=Disp Column low bits xxxxxxxx=0->131

 b2l(0xAD,0);//AC=Corser Off/AD=Corser On/After Num

 b2l(0x03,0);//00=OFF/01=1S/02=0.5S/03=All On

 b2l(0xAF,0);//Start Display

}

//Display ASCII Test

void dispAsc(void){

 int i,j,k=0;

 for(i=0;i<8;i++){

  for(j=0;j<21;j++){

   c2l(i,j,k);

   //delay(1000);

   if(k>94){k=0;}else{k++;}

  }

 }

}


//END Files//


main.cpp改为:

#include "mbed.h"

#include "12864.h"


//Defin GPIO

DigitalOut myled0(P0_15);


//Main Program

int main(){

 lcdInit();

 lcdClear();

 while(true){

  dispAsc();

  myled0 =!myled0;

  wait(0.2);

  lcdClear();

 }

}

下载后运行结果见照片55。55-128X64LCD驱显.JPG

以后的编成结果直接显示在LCD上,因此只是增加代码和模块函数,届时会说明增加的位置的。此外,在驱动这款LCD刷屏是能明显地看到后记行的刷屏状况,说明LPC824的IO口速度不是很快。在以后的并口试验中我们还可以看看战况如何。

为了规范设计,以后我们将main.cpp分成这样几个区域:

头文件区

#include

...


公共类对象定义区

//Define IO

DigitalOut plus27(P0_27);

...


变量定义区

int32_t jsq;

...


下边是各个模块区域,顺序按照执行顺序先后排列——为了省略函数声明!可以预知的是中断函数与顺序无关,可以都放在main函数之后。

我用的都是不多于10元的LCD,价廉物美。最便宜的带背光的小LCD只有6元。试试吧!


作者于2017-01-01 21:39:34修改!
楼主可见