本文引用地址:http://m.butianyuan.cn/article/201611/315615.htm //頭文件部分 #include //====================================================== //定義常用符號(hào),LCD控制字符號(hào)常量,TIME0定時(shí)賦值常量 #define uchar unsigned char #define uint unsigned int
#define DISPON 0x3f #define DISPOFF 0x3e #define DISPFIRSTLINE 0xc0 #define LCDBUZY 0x80 #define LIMIT 0x80 //128列限制 #define TL0NUMB (65536-50000)%256 //定時(shí)50MS,20次溢出為一秒定時(shí)中斷 #define TH0NUMB (65536-50000)/256 #define TCOUNTF 10 //========================================================= //定義引腳 sbit lck = P3^5; //鎖存信號(hào)引腳
//========================================================= //定義全局變量 uchar combyte; //LCD命令變量 uchar data status; //狀態(tài)變量,左右半屏標(biāo)志量 uchar tcount = 0; //限制定時(shí)時(shí)間標(biāo)志 //uchar pagebyte = 0; //上下移頁(yè)控制量 uchar colbyte = 120; //左右移列控制量
//========================================================= //定義圖形參數(shù) uchar code A[]={0xe0, 0x0f, 0xf0, 0x0f, 0x98, 0x00, 0x8c, 0x00, // A,此處的參數(shù)是按照列輸入來(lái)寫(xiě)的,英文內(nèi)碼是按照行輸入來(lái)寫(xiě)的 0x98, 0x00, 0xf0, 0x0f, 0xe0, 0x0f, 0x00, 0x00};
//========================================================= //函數(shù)原型 void IniLcd(void); //初始化LCD void ClrLcd(uchar); //清屏LCD void WrComL(uchar); //寫(xiě)左半屏幕命令字 void WrComR(uchar); //寫(xiě)右半屏幕命令字 void WrData(uchar,uchar,uchar); //寫(xiě)數(shù)據(jù) void Locate(uchar,uchar); //LCD屏定位 void delayus(uint); //微妙級(jí)延遲 void delay(void); //延遲個(gè)幾秒 //void PicturePage(void); //圖形下移函數(shù) void PictureCol(void); //圖像左移函數(shù)
void IniTime0(void); //初始化定時(shí)器0,利用MCU的內(nèi)部定時(shí)完成圖像的移動(dòng)過(guò)程 //========================================================= //主程序部分 void main(void) { IniLcd(); IniTime0(); while(1); //對(duì)LCD的操作 /*{ Picture(); delay(); ClrLcd(0x00); }*/ } /******************************************************/ /* 以下部分是LCD12864的驅(qū)動(dòng)模塊 */ /******************************************************/
//========================================================= //初始化定時(shí)器0函數(shù) void IniTime0(void) { TMOD = 0x01; //TCON = 0x00; TL0 = TL0NUMB; TH0 = TH0NUMB; TR0 = 1; ET0 =1; EA =1; } //========================================================= //定時(shí)器中斷函數(shù) void Time0(void) interrupt 1 { TR0 = 0; TL0 = TL0NUMB; TH0 = TH0NUMB; tcount++; if(tcount == TCOUNTF) { tcount = 0;
/*PicturePage(); pagebyte++; if(pagebyte > 6) pagebyte = 0;*/ PictureCol(); colbyte--; } TR0 = 1; } //========================================================= //初始化LCD函數(shù) void IniLcd(void) { combyte = DISPOFF; WrComL(combyte); WrComR(combyte);
combyte = DISPON; WrComL(combyte); WrComR(combyte); combyte = DISPFIRSTLINE; WrComL(combyte); WrComR(combyte); ClrLcd(0x00); /*combyte = DISPFIRSTLINE; WrComL(combyte); WrComR(combyte);*/ Locate(0x00,0x00); } //========================================================= //清屏LCD函數(shù) void ClrLcd(uchar cbyte) { uchar i,j; for(i=0;i<8;i++) { delayus(6); for(j=0;j{ delayus(6); WrData(cbyte,i,j); } } }
//========================================================= //微妙級(jí)延遲函數(shù) void delayus(uint t) { while(t--); }
//========================================================= //延遲個(gè)幾秒函數(shù) void delay(void) { delayus(50000); delayus(50000); delayus(50000); delayus(50000); delayus(50000); delayus(50000); }
//========================================================= //寫(xiě)左半屏幕命令字函數(shù) void WrComL(uchar cbyte) { lck = 0; P1 = 0x29; //E = 1;R/W = 1;D/I = 0; CS1 = 1; CS2 = 0; lck = 1; while(P0&LCDBUZY); lck = 0; P1 = 0x00; //E = 0; lck = 0;
lck = 0; P1 = 0x21; //E = 1;R/W = 0;D/I = 0; CS1 = 1; CS2 = 0; lck = 1; P0 = cbyte; lck = 0; P1 = 0x00; //E = 0; lck = 1; } //========================================================= //寫(xiě)右半屏幕命令字函數(shù) void WrComR(uchar cbyte) { lck = 0; P1 = 0x2a; //E = 1;R/W = 1;D/I = 0; CS1 = 0; CS2 = 1; lck = 1; while(P0&LCDBUZY); lck = 0; P1 = 0x00; //E = 0; lck = 0;
lck = 0; P1 = 0x22; //E = 1;R/W = 0;D/I = 0; CS1 = 0; CS2 = 1; lck = 1; P0 = cbyte; lck = 0; P1 = 0x00; //E = 0; lck = 1; } //========================================================= //LCD屏幕點(diǎn)定位函數(shù) void Locate(uchar page,uchar col) { uchar com_page,com_col,right; switch(col&0xc0) { case 0x00: //左半屏幕 { right = 0; break; } case 0x40: //右半屏幕 { right = 1; break; } default: break; } com_page = page&0x07|0xb8; //將頁(yè)數(shù)據(jù)轉(zhuǎn)換為頁(yè)命令字 com_col = col&0x3f|0x40; //將列數(shù)據(jù)轉(zhuǎn)換為列命令字
switch(right) { case 0x00: //左半屏幕 { WrComL(com_page); WrComL(com_col); status = 0; break; } case 0x01: //右半屏幕 { WrComR(com_page); WrComR(com_col); status = 1; break; } default: break; } } //========================================================= //寫(xiě)數(shù)據(jù)函數(shù) void WrData(uchar databyte,uchar page,uchar col) { Locate(page,col);
P0 = databyte; lck = 0; if(status) P1 = 0x26; //E = 1; R/W = 0; D/I = 1; CS1 = 0; CS2 = 1; else P1 = 0x25; //E = 1; R/W = 0; D/I = 1; CS1 = 1; CS2 = 0; lck = 1; //P0 = databyte; lck = 0; P1 = 0x00; lck = 1; } //========================================================= //圖形函數(shù),圖像按頁(yè)下移 /*void PicturePage(void) { uchar col,page; uchar i = 0; ClrLcd(0x00); for(col=64;col<72;col++) { for(page=pagebyte;page{ WrData(A[i++],page++,col); } } }*/
//========================================================= //圖形左移函數(shù) void PictureCol(void) { uchar col,page; uchar i = 0; ClrLcd(0x00); for(col=colbyte;(col=0);) //列控制 { for(page=0;page<2;page++) //頁(yè)控制 { WrData(A[i++],page,col); //寫(xiě)數(shù)據(jù),“i++”同步跟新數(shù)據(jù) } col++; } if(colbyte==0) colbyte = 120;
} |
評(píng)論