| 本帖最后由 gdqinci 于 2010-6-17 11:25 编辑 
 以下是第22章
 
 在51 MCU DEMO试验板上实现16X2LCM演示程序1
 
 #include <REG51.H>
 #include <intrins.H>
 #define uchar unsigned char
 #define uint unsigned int
 #define DataPort P0
 sbit LCM_RS=P2^0;
 sbit LCM_RW=P2^1;
 sbit LCM_EN=P2^2;
 uchar code str0[]={"-This is a LCD-!"};
 uchar code str1[]={"-Design by ZXH-!"};
 uchar code str2[]={"                "};
 //--------------------------------------
 void delay(uint k)
 {
 uint data i,j;
 for(i=0;i<k;i++)
 {
 for(j=0;j<121;j++)
 {;}
 }
 }
 //--------------------------------------
 void WaitForEnable(void)
 {
 DataPort=0xff;
 LCM_RS=0;
 LCM_RW=1;_nop_();
 LCM_EN=1;_nop_();_nop_();
 while(DataPort&0x80);
 LCM_EN=0;
 }
 //---------------------------------------
 void WriteCommandLCM(uchar CMD,uchar Attribc)
 {
 if(Attribc)WaitForEnable();
 LCM_RS=0;
 LCM_RW=0;_nop_();
 DataPort=CMD;_nop_();
 LCM_EN=1;_nop_();_nop_();
 LCM_EN=0;
 }
 //--------------------------------------------------
 void WriteDataLCM(uchar dataW)
 {
 WaitForEnable();
 LCM_RS=1;
 LCM_RW=0;_nop_();
 DataPort=dataW;_nop_();
 LCM_EN=1;_nop_();_nop_();
 LCM_EN=0;
 }
 //------------------------------------------------
 void InitLcd()
 {
 WriteCommandLCM(0x38,1);
 WriteCommandLCM(0x08,1);
 WriteCommandLCM(0x01,1);
 WriteCommandLCM(0x06,1);
 WriteCommandLCM(0x0c,1);
 }
 //-----------------------------------------------
 void DisplayOneChar(uchar X,uchar Y,uchar DData)
 {
 Y&=1;
 X&=15;
 if(Y)X|=0x40;
 X|=0x80;
 WriteCommandLCM(X,0);
 WriteDataLCM(DData);
 }
 //--------------------------------------------------
 void DisplayListChar(uchar X,uchar Y,uchar code *DData)
 {
 uchar ListLength=0;
 Y&=0x1;
 X&=0xF;
 while(X<=15)
 {
 DisplayOneChar(X,Y,DData[ListLength]);
 ListLength++;
 X++;
 }
 }
 //--------------------------------------------------
 void main(void)
 {
 char i,m;
 delay(500);
 InitLcd();
 //-----------------
 for(i=15;i>=0;i--)
 {
 WriteCommandLCM(0x01,1);
 DisplayOneChar(i,0,0x20);
 DisplayListChar(i,0,str0);
 DisplayListChar(i,1,str1);
 delay(200);
 }
 delay(2800);
 //------------------------------------
 for(i=0;i<16;i++)
 {
 WriteCommandLCM(0x01,1);
 DisplayOneChar(i,0,0x20);
 DisplayListChar(i,0,str0);
 DisplayListChar(i,1,str1);
 delay(200);
 }
 WriteCommandLCM(0x01,1);
 delay(3000);
 //-----------------------------------
 for(i=0;i<10;i++)
 {
 WriteCommandLCM(0x01,1);
 delay(500);
 DisplayListChar(0,0,str0);
 DisplayListChar(0,1,str1);
 delay(500);
 i++;
 }
 delay(3000);
 //**************************************
 while(1)
 {
 //**************************************
 for(i=15;i>=0;i--)
 {
 WriteCommandLCM(0x01,1);
 DisplayOneChar(i,0,0x20);
 DisplayListChar(i,0,str0);
 DisplayListChar(i,1,str1);
 delay(200);
 }
 //---------------------------------
 for(i=1;i<16;i++)
 {
 m=16-i;
 WriteCommandLCM(0x01,1);
 DisplayOneChar(0,0,0x20);
 DisplayListChar(0,0,&str0);
 DisplayListChar(0,1,&str1);
 DisplayListChar(m,0,str2);
 DisplayListChar(m,1,str2);
 delay(200);
 }
 WriteCommandLCM(0x01,1);
 delay(200);
 }
 }
 |