LCD1602字符液晶滾動演示
單片機源程序如下:
- //main.c
- /* 名稱:LCD1602字符液晶滾動演示程序
- 說明:K1~K3按鈕分別實現(xiàn)液晶垂直或水平滾動顯示及暫停與繼續(xù)控制。
- */
- #include<reg51.h>
- #include<string.h>
- #define uchar unsigned char
- #define uint unsigned int
- void Initialize_LCD();
- void DelayMS(uint ms);
- void ShowString(uchar,uchar,uchar *);
- sbit K1=P3^0;
- sbit K2=P3^1;
- sbit K3=P3^2;
- uchar code Prompt[]="Press K1 - K3 To Start Demo Prog";
- //待滾動顯示的信息段落,每行不超過80個字符,共6行
- uchar const Line_Count=6;
- uchar code Msg[][80]=
- {
- "Many CAD users dismiss schematic capture as a necessary evil in the ",
- "process of creating PCB layout but we have always disputed this point ",
- "of view. With PCB layout now offering automation of both component ",
- "can often be the most time consuming element of the exercise.",
- "And if you use circuit simulation to develop your ideas, ",
- "you are going to spend even more time working on the schematic."
- };
- //顯示緩沖(2行)
- uchar Disp_Buffer[32];
- //垂直滾動顯示
- void V_Scroll_Display()
- {
- uchar i,j,k=0;
- uchar *p=Msg[0];
- uchar *q=Msg[Line_Count]+strlen(Msg[Line_Count]);
- //以下僅使用顯示緩沖的前16字節(jié)空間
- while(p<q)
- {
- for(i=0;i<16&&p<q;i++)
- { //消除顯示緩沖中待顯示行首尾可能出現(xiàn)的空格
- if((i==0||i==15)&&*p==' ') p++;
- if(*p!='\0')
- {
- Disp_Buffer[i]=*p++;
- }
- else
- {
- if(++k>Line_Count) break;
- p=Msg[k]; //p指向下一串的首地址
- Disp_Buffer[i]=*p++;
- }
- }
- //不足16個字符時空格補充
- for(j=i;j<16;j++) Disp_Buffer[j]=' ';
- //垂直滾動顯示
- while(F0) DelayMS(5);
- ShowString(0,0," ");
- DelayMS(150);
- while(F0) DelayMS(5);
- ShowString(0,1,Disp_Buffer);
- DelayMS(150);
- while(F0) DelayMS(5);
- ShowString(0,0,Disp_Buffer);
- ShowString(0,1," ");
- DelayMS(150);
- }
- //最后清屏
- ShowString(0,0," ");
- ShowString(0,1," ");
- }
- //水平滾動顯示
- void H_Scroll_Display()
- {
- uchar i,j,k=0,L=0;
- uchar *p=Msg[0];
- uchar *q=Msg[Line_Count]+strlen(Msg[Line_Count]);
- //將32個字符的顯示緩沖前16個字符設(shè)為空格
- for(i=0;i<16;i++) Disp_Buffer[i]=' ';
- while(p<q)
- {
- //忽略緩沖中首尾可能出現(xiàn)的空格
- if((i==16||i==31)&&*p==' ') p++;
- for(i=16;i<32&&p<q;i++)
- {
- if(*p!='\0')
- {
- Disp_Buffer[i]=*p++;
- }
- else
- {
- if(++k>Line_Count) break;
- p=Msg[k]; //p指向下一串的首地址
- Disp_Buffer[i]=*p++;
- }
- }
- //不足32個字符時空格補充
- for(j=i;j<32;j++) Disp_Buffer[j]=' ';
- //水平滾動顯示
- for(i=0;i<=16;i++)
- {
- while(F0) DelayMS(5);
- ShowString(0,L,Disp_Buffer+i);
- while(F0) DelayMS(5);
- DelayMS(20);
- }
- L=(L==0)?1:0; //行號在0,1間交替
- DelayMS(300);
- }
- //如果顯示結(jié)束時停留在第0行,則清除第1行的內(nèi)容
- if(L==1) ShowString(0,1," ");
- }
- //外部中斷0,由K3控制暫停與繼續(xù)顯示
- void EX_INT0() interrupt 0
- {
- F0=!F0; //暫停與繼續(xù)顯示控制標志位
- }
- //主程序
- void main()
- {
- uint Count=0;
- IE=0x81; //允許外部中斷0
- IT0=1; //下降沿觸發(fā)
- F0=0; //暫停與繼續(xù)顯示控制標志位
- Initialize_LCD();
- ShowString(0,0,Prompt);
- ShowString(0,1,Prompt+16);
- while(1)
- {
- if(K1==0)
- {
- V_Scroll_Display();
- DelayMS(300);
- }
- else
- if(K2==0)
- {
- H_Scroll_Display();
- DelayMS(300);
- }
- }
- }
- //LCD1602.c
- /* 名稱:液晶控制與顯示程序
- 說明:本程序是通用的1602液晶控制程序。
- */
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RS=P2^0;
- sbit RW=P2^1;
- sbit EN=P2^2;
- //延時
- void DelayMS(uint ms)
- {
- uchar i;
- while(ms--) for(i=0;i<120;i++);
- }
- //忙檢查
- uchar Busy_Check()
- {
- uchar LCD_Status;
- RS=0; //寄存器選擇
- RW=1; //讀狀態(tài)寄存器
- EN=1; //開始讀
- DelayMS(1);
- LCD_Status=P0;
- EN=0;
- return LCD_Status;
- }
- //寫LCD命令
- void Write_LCD_Command(uchar cmd)
- {
- while((Busy_Check()&0x80)==0x80); //忙等待
- RS=0; //選擇命令寄存器
- RW=0; //寫
- EN=0;
- P0=cmd;EN=1;DelayMS(1);EN=0;
- }
- //發(fā)送數(shù)據(jù)
- void Write_LCD_Data(uchar dat)
- {
- while((Busy_Check()&0x80)==0x80); //忙等待
- RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0;
- }
- //LCD初始化
- void Initialize_LCD()
- {
- Write_LCD_Command(0x38);DelayMS(1);
- Write_LCD_Command(0x01);DelayMS(1); //清屏
- Write_LCD_Command(0x06);DelayMS(1); //字符進入模式:屏幕不動,字符后移
- Write_LCD_Command(0x0c);DelayMS(1); //顯示開,光標關(guān)
- }
- //顯示字符串
- void ShowString(uchar x,uchar y,uchar *str)
- {
- uchar i=0;
- if(y==0) Write_LCD_Command(0x80|x); //設(shè)置顯示起始位置
- if(y==1) Write_LCD_Command(0xc0|x);
- for(i=0;i<16;i++) //輸出字符串
- {
- Write_LCD_Data(str[i]);
- }
- }
復(fù)制代碼
|