首页->【玩CortexM0】

97 0

[Nuvoton M0516] 实验二 黑白128X64LCD驱显

由前一个实验,我们已经基本掌握了GPIO的控制,因此驱显也非常方便。本例驱显基本的串口LCD,4根数据+2根电源线的。

先写一个lcd128x64.h文档,内容如下:

#include "common.h"


#define sdin P00_DOUT//Seri Data

#define sclk P01_DOUT//Seri Clock

#define sreg P02_DOUT//1=Data & 0=Command

#define rest P03_DOUT//Reset LCD


const unsigned char cha[][6]={//ASCII Table

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

{0x00,0x00,0x21,0x7F,0x01,0x00},//1

{0x00,0x31,0x43,0x45,0x49,0x31},//2

{0x00,0x22,0x49,0x49,0x49,0x36},//3

{0x00,0x04,0x0C,0x14,0x7F,0x04},//4

{0x00,0x72,0x51,0x51,0x51,0x4E},//5-5

{0x00,0x3E,0x49,0x49,0x49,0x06},//6

{0x00,0x40,0x43,0x4C,0x50,0x60},//7

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

{0x00,0x30,0x48,0x48,0x48,0x3F},//9

{0x00,0x1F,0x24,0x64,0x24,0x1F},//A-10

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

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

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

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

{0x00,0x41,0x7F,0x49,0x5C,0x60},//F-15

{0x00,0x3E,0x41,0x45,0x47,0x24},//G

{0x41,0x7F,0x08,0x08,0x08,0x7F},//H

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

{0x00,0x42,0x41,0x7F,0x40,0x40},//J

{0x00,0x41,0x7F,0x59,0x24,0x43},//K-20

{0x00,0x40,0x7F,0x41,0x01,0x03},//L

{0x00,0x7F,0x20,0x1E,0x20,0x7F},//M

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

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

{0x00,0x41,0x7F,0x49,0x48,0x30},//P-25

{0x00,0x3E,0x41,0x47,0x42,0x3D},//Q

{0x00,0x41,0x7F,0x49,0x4C,0x33},//R

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

{0x00,0x60,0x41,0x7F,0x41,0x60},//T

{0x00,0x7E,0x01,0x01,0x01,0x7E},//U-30

{0x00,0x7C,0x02,0x01,0x02,0x7C},//V

{0x00,0x7C,0x03,0x3C,0x03,0x7C},//W

{0x00,0x43,0x24,0x18,0x24,0x43},//X

{0x00,0x60,0x18,0x07,0x18,0x60},//Y

{0x00,0x63,0x45,0x49,0x51,0x63},//Z-35

{0x00,0x00,0x00,0x01,0x00,0x00},//.

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

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

{0x00,0x80,0x80,0x80,0x80,0x80},//-

{0x00,0x00,0x00,0x24,0x00,0x00},//:-40

};

const unsigned char hanzi[]={

//16X16

0X03,0X08,0X20,0X00,0X40,0X00,0X80,0X81,0X81,0X80,0X00,0X40,0X00,0X20,0X08,0X03,

0XC0,0X10,0X04,0X00,0X02,0X00,0X01,0X81,0X81,0X01,0X00,0X02,0X00,0X04,0X10,0XC0,

//32X32

0X00,0X00,0X01,0X03,0X04,0X08,0X18,0X38,0X20,0X40,0X40,0X40,0X80,0X80,0X80,0XE0,

0XE0,0X80,0X80,0X80,0X40,0X40,0X40,0X30,0X30,0X10,0X08,0X04,0X03,0X01,0X00,0X00,

0X0F,0X71,0X81,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,

0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0X81,0X71,0X0F,

0XF0,0X8E,0X81,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,

0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X81,0X8E,0XF0,

0X00,0X00,0X80,0XC0,0X20,0X10,0X18,0X1C,0X04,0X02,0X02,0X02,0X01,0X01,0X01,0X07,

0X07,0X01,0X01,0X01,0X02,0X02,0X02,0X04,0X1C,0X18,0X10,0X20,0XC0,0X80,0X00,0X00,

//32X32-DOT

0X00,0X00,0X01,0X03,0X04,0X08,0X18,0X38,0X20,0X40,0X43,0X43,0X80,0X80,0X80,0XE6,

0XE6,0X80,0X80,0X80,0X40,0X43,0X43,0X30,0X30,0X10,0X08,0X04,0X03,0X01,0X00,0X00,

0X0F,0X71,0X81,0X80,0X00,0X01,0X31,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,

0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X30,0X31,0X01,0X00,0X80,0X81,0X71,0X0F,

0XF0,0X8E,0X81,0X01,0X00,0X80,0X8C,0X0C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,

0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X8C,0X80,0X00,0X01,0X81,0X8E,0XF0,

0X00,0X00,0X80,0XC0,0X20,0X10,0X18,0X1C,0X04,0X02,0XC2,0XC2,0X01,0X01,0X01,0X67,

0X67,0X01,0X01,0X01,0XC2,0XC2,0X02,0X04,0X1C,0X18,0X10,0X20,0XC0,0X80,0X00,0X00,

//16X16-Nothing

0X03,0X08,0X20,0X00,0X40,0X00,0X80,0X81,0X81,0X80,0X00,0X40,0X00,0X20,0X08,0X03,

0XC0,0X10,0X04,0X00,0X02,0X00,0X01,0X81,0X81,0X01,0X00,0X02,0X00,0X04,0X10,0XC0,

//64X64

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X03,0X07,0X0F,

0X0F,0X1C,0X18,0X38,0X30,0X70,0X60,0X60,0X60,0X60,0X60,0XE0,0XC0,0XC0,0XF8,0XF8,

0XF8,0XF8,0XC0,0XC0,0XE0,0X60,0X60,0X60,0X60,0X60,0X70,0X30,0X38,0X18,0X1C,0X0F,

0X07,0X07,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X03,0X0F,0X1E,0X38,0X70,0XE0,0XC0,0X80,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X80,0XC0,0XE0,0X70,0X38,0X1E,0X0F,0X03,0X01,0X00,0X00,0X00,0X00,

0X00,0X07,0X1F,0X7C,0XF0,0XE0,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XC0,0XF0,0X7C,0X1F,0X07,0X00,

0X3F,0XFF,0XE3,0X03,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X03,

0X03,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X03,0XE3,0XFF,0X3F,

0XFC,0XFF,0XC7,0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XC0,

0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XC0,0XC7,0XFF,0XFC,

0X00,0XE0,0XF8,0X3E,0X0F,0X03,0X01,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X07,0X07,0X0F,0X3E,0XF8,0XE0,0X00,

0X00,0X00,0X00,0X00,0X80,0XC0,0XF0,0XF8,0X1C,0X0E,0X07,0X03,0X01,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X01,0X03,0X07,0X0E,0X1C,0X78,0XF0,0XC0,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XC0,0XE0,0X60,

0X70,0X78,0X78,0X1C,0X0C,0X0E,0X06,0X06,0X06,0X06,0X06,0X07,0X03,0X03,0X1F,0X1F,

0X1F,0X1F,0X03,0X03,0X07,0X06,0X06,0X06,0X06,0X06,0X0E,0X0C,0X1C,0X18,0X38,0XF0,

0XF0,0XE0,0XC0,0XC0,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

//wyd

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,

0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X03,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X03,0X03,0X03,0X07,0X07,0X0F,0X0F,

0X0F,0X0F,0X1F,0X1F,0X1F,0X1F,0X0F,0X0F,0X07,0X07,0XFD,0XFF,0XFF,0XFF,0XFF,0XFF,

0XC0,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X7F,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X0F,0X0F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X1F,0X1F,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,

0XF8,0XC0,0X80,0X80,0X80,0X80,0XC0,0XE0,0XF8,0XF8,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,

0X00,0X00,0X7C,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X3F,

0X3F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0XD8,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X3F,

0X1F,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XC0,0XC0,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,

0X00,0X00,0X00,0X00,0XF8,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X3E,0X7F,0XFF,

0XFF,0XFF,0XFF,0XFF,0XF8,0XF8,0X00,0X00,0X00,0X00,0XF8,0XF8,0XFF,0XFF,0XFF,0XFF,

0XFF,0X7F,0X00,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X07,0X07,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X02,0X03,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,

0X00,0X00,0X00,0X00,0X00,0X00,0XF0,0XF0,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,

0XFF,0XFF,0XE0,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XF0,0XFF,0XFF,0XFF,

0XFF,0XFF,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XF8,0XE0,0XE0,0XF8,0XF8,0XFC,0XFC,

0XFC,0X7E,0X7E,0X3E,0X3E,0X7E,0X7C,0XFC,0XF8,0XF8,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,

0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC1,0XC1,0XFF,

0XFF,0XFF,0XFF,0XFF,0XFC,0XFC,0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X3F,0X3F,0X3F,0X1F,0X1F,0X1F,0X3F,0XBF,0XFF,0XFE,

0XFE,0XFC,0XF8,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,

};

//Delayer

void delay0(unsigned int delayer){

  unsigned int i,j;

  for(i=0;i<delayer;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;

  sreg=com;

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

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

    dt=dt<<1;

    wri();

  }

}

//Set X,Y,X=Colum:0-102 Y=page:0-8

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

  b2l(0xB0+pag,0);//page

  //col+=0x04;//+4-->4bit

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

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

}

//Clear LCD

void lcdClear(void){

  unsigned char pag,col;

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

  setXY(pag,0);

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

  }

}

//Display a ASCII,c:Char in cha[]

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

  unsigned char line;

  setXY(pag,col*6);//6    

  for (line=0;line<6;line++)//6

  b2l(cha[c][line],1);//b2l(hanzi[c*8+line],1);//

}

//16X16 Chinases to LCD,row,page:addr,c:hanzi[]

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

  unsigned char i;

  setXY(pag,col*8);//Page,Colum 

  for(i=0;i<16;i++){b2l(hanzi[c*32+i],1);}

  setXY(pag-1,col*8);

  for(i=16;i<32;i++){b2l(hanzi[c*32+i],1);}

}

//32X32 Chinases to LCD,row,page:Addr,c:hanzi[]

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

  unsigned char i;

  setXY(pag,col*8);//page,Colum

  for(i=0;i<32;i++){b2l(hanzi[c*32+i],1);}

}

//64X64 Chinases to LCD,row,page:Addr,c:hanzi[]

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

  unsigned char i;

  setXY(pag,col*8);

  for(i=0;i<64;i++){b2l(hanzi[c*64+i],1);}

}

//LCD Initial

void lcdInit(void){

  rest=0;

  delay0(1000);

  rest=1;

  delay0(1000);

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

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

  b2l(0xA0,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(100);

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

  delay0(100);

  b2l(0x22,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(0x18,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(0xE3,0);//NOP mode

  //b2l(0xE0,0);//LCD Read/Write Edit Mode On

  //b2l(0xEE,0);//LCD Read/Write Edit Mode Off

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

  b2l(0xA6,0);//A6=Normal Disp/A7=Invert Disp

  delay0(200);

}

//ASCII mide=0~40 

void disAsc(void){

 int i,j,k=0;

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

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

   c2l(i,j,k);

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

  }

 }

}

上述程序仅仅完成初始化128x64LCD,字符点阵和一些基本的控制显示函数,看程序注释大致能明白,此处不一一解释了。

主程序也直接给出,显示结果与主程序不一致是有些32,64点阵的数据显示与以后的程序无关,有兴致的朋友们可以自己尝试将他们显示出来。主程序修改如下:

//128X64 LCD Test

#include "common.h"

#include "lcd128x64.h"


#define P0_MODE 0x55

#define P4_MODE 0x54

#define led0 P41_DOUT//Led0

#define led1 P42_DOUT//Led1


//Delayer

void delay(unsigned int delayer){

  unsigned int i,j;

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

}

//IO Initial

void ioInit(void){

 P0_PMD = P0_MODE;//P0 DOUT

 P4_PMD = P4_MODE;//P4 DOUT

}

//Test LED for Programe

void led(void){

 if(led0==0){led0=1;led1=0;}else{led0=0;led1=1;}

}

//Main program

int main(void){

 int ti=3000;

 ioInit();

 led();

 lcdInit();

 lcdClear();

 while(1){

  disAsc();

  led();

  delay(ti);

  lcdClear();

 }

}

程序结果见图21。21-驱显128x64LCD.JPG

视频见http://v.youku.com/v_show/id_XMjQ5MDgwMjQ3Mg==.html


楼主可见