单片机+LCD12864液晶简易菜单程序-c语言12864cd 单片机 LCD12864液晶简易菜单程序-c语言12864cd.rar
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- Index_Num 数组元素最大序号
- Index 数组元素当前序号
- Index_P 选中项序号
- Index_P_Pre 上一选中项序号,用于快速清理上次所画下的图案
- */
- #include
- #include
- #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
- #define LCD_data P0 //数据口
- #define Index_Num 8
- typedef unsigned char uchar;
- typedef unsigned int uint;
- ***it Key1 = P1^0;
- ***it Key2 = P1^1;
- ***it LCD_RS = P2^6; //寄存器选择输入 cs
- ***it LCD_RW = P2^5; //液晶读/写控制 sid
- ***it LCD_EN = P2^7; //液晶使能控制 clk
- ***it LCD_PSB = P3^2; //串/并方式控制
- ***it LCD_RST = P3^7; //液晶复位端口
- bdata uchar DataBuf;
- uint Index,Index_P_Pre;
- uint Index_P=0;
- uchar code Weibo[][16]={"hello","呀咦 晗昱","weibo.com","/maixianyu","blog.163.com","maixianyu@126",
- "123","456","789"};
- ////////////////////////////////////////////// 200us
- void delay(int ms)
- {
- uchar t;
- while(ms--)
- {
- for(t=0; t<20; t++) ;
- }
- }
- //////////////////////////////////////////////////////////////////
- void Lcd_Write_8bit(uchar msg){
- uint i;
- uchar DataBuffer;
- DataBuffer=msg;
- for(i=0;i<8;i++){
- LCD_RW=(bit)(DataBuffer&0x80);
- LCD_EN=1;
- _nop_();
- _nop_();
- LCD_EN=0;
- _nop_();
- _nop_();
- DataBuffer<<=1;
- }
- }
- /////////////////////////////////////////
- uchar Lcd_Rec(uint RS){
- uint i;
- uchar Data_H,Data_L;
- LCD_RS=1;
- if(RS)
- Lcd_Write_8bit(0xfe);
- else
- Lcd_Write_8bit(0xfc);
- delayNOP();
- for(i=0;i<8;i++){
- LCD_EN=1;
- Data_H=(Data_H<<1)|LCD_RW;
- LCD_EN=0;
- }
- delayNOP();
- for(i=0;i<8;i++){
- LCD_EN=1;
- Data_L=(Data_L<<1)|LCD_RW;
- LCD_EN=0;
- }
- LCD_RS=0;
- DataBuf=(Data_H<<4)|Data_L;
- return DataBuf;
- }
- /////////////////////////////////////////
- bit Lcd_Busy(){
- return Lcd_Rec(0)&0x80;
- }
- /////////////////////////////////////////
- void Lcd_Write(uchar msg,uint dc){ //if dc equals 0,msg is a kind of cmd.
- while(Lcd_Busy());
- if(dc==0)
- DataBuf=0xf8;
- else
- DataBuf=0xfa;
- delayNOP();
- LCD_RS=1;
- Lcd_Write_8bit(DataBuf);
- delayNOP();
- DataBuf=msg&0xf0;
- Lcd_Write_8bit(DataBuf);
- delayNOP();
- DataBuf=(msg<<4)&0xf0;
- Lcd_Write_8bit(DataBuf);
- LCD_RS=0;
- }
- ////////////////////////////////////////////////////
- void Disp_black() //清理上一次画下的痕迹
- {
- uchar i,j;
- Lcd_Write(0x34,0); //写数据时,关闭图形显示
- if(Index_P_Pre<2){
- for(i=0+16*Index_P_Pre; i<16+16*Index_P_Pre; i++)
- {
- Lcd_Write(0x80+i,0);
- Lcd_Write(0x80,0);
- for(j=0;j<16;j++)
- Lcd_Write(0x00,1);
- }
- }else{
- for(i=0+16*(Index_P_Pre-2);i<16+16*(Index_P_Pre-2);i++)
- {
- Lcd_Write(0x80+i,0);
- Lcd_Write(0x88,0);
- for(j=0;j<16;j++)
- Lcd_Write(0x00,1);
- }
- }
- Lcd_Write(0x36,0); //写完数据,开图形显示
- }
- void convertChar(uchar CX) // 反白函数
- {
- uchar halfLineCnt, basicBlock,lcdPosX,lcdPosY;
- // if(f==1)
- Disp_black(); //清绘图区
- lcdPosY = 0x80;
- if (CX == 0)
- {
- CX = 0x80;
- halfLineCnt = 16;
- }
- else if (CX == 1)
- {
- CX = 0x80;
- halfLineCnt = 32;
- }
- else if (CX == 2)
- {
- CX = 0x88;
- halfLineCnt = 16;
- }
- else if (CX == 3)
- {
- CX = 0x88;
- halfLineCnt = 32;
- }
- lcdPosX = CX;
- for (; halfLineCnt != 0; halfLineCnt--)
- {
- basicBlock = 16;
- Lcd_Write(0x34,0);
- Lcd_Write(lcdPosY,0);
- Lcd_Write(lcdPosX,0);
- Lcd_Write(0x30,0);
- for (;basicBlock != 0; basicBlock--)
- {
- if (halfLineCnt > 16)
- {
- Lcd_Write(0x00,1);
- }
- else
- {
- Lcd_Write(0xff,1);
- }
- }
- lcdPosY++;
- }
- Lcd_Write(0x36,0);
- Lcd_Write(0x30,0);
- }
- ///////////////////////////////////////////////////
- void Lcd_Pos(uchar x,uchar y){
- uchar pos;
- if(x==1)
- {x=0x80;}
- else if(x==2)
- {x=0x90;}
- else if(x==3)
- {x=0x88;}
- else if(x==4)
- {x=0x98;}
- pos=x+y;
- Lcd_Write(pos,0);
- }
- ///////////////////////////////////////////
- void Lcd_Menu(uint Head){
- uint j,i;
- uint Index_End=Head+3;
- // if(Index_End<=Index_Num){
- for(j=0;Head<=Index_End;Head++,j++){
- Lcd_Pos(j+1,0);
- i=0;
- while(i<15){
- if(Weibo[Head]!=0)
- Lcd_Write(Weibo[Head][i++],1);
- else{
- Lcd_Write(0x20,1);
- i++;
- }
- }
- delayNOP();
- }
- // }
- }
- ///////////////////////////////////////////
- void Lcd_Init(void){
- LCD_PSB=0;
- LCD_RST=0;
- LCD_RS=0;
- LCD_RW=1;
- LCD_EN=0;
- delay(2);
- LCD_RST=1;
- delay(2);
- Lcd_Write(0x30,0); //ordinary cmd
- delay(2);
- Lcd_Write(0x0c,0); //how to show
- delay(2);
- Lcd_Write(0x01,0); //clear
- delay(2);
- }
- /////////////////////////////////////////
- void main(void){
- Lcd_Init();
- Key1=1;
- Lcd_Menu(Index);
- convertChar(Index_P);
- while(1){
- if(Key1==0&&Index>=0){
- delay(5);
- if(Key1==0&&Index>=0){
- if(Index_P==0&&Index>0)
- Lcd_Menu(--Index);
- if(Index_P>0){
- Index_P_Pre=Index_P;
- convertChar(--Index_P);
- }
- }
- }
- if(Key2==0&&Index_P<=3){
- delay(5);
- if(Key2==0&&Index_P<=3){
- if(Index_P==3&&Index<(Index_Num-3))
- Lcd_Menu(++Index);
- if(Index_P<3){
- Index_P_Pre=Index_P;
- convertChar(++Index_P);
- }
- }
- }
- }
- }
|