| 程序功能:液晶5位顯示,每個(gè)按鍵對(duì)應(yīng)一位。按一下按鍵,相應(yīng)位加1,可進(jìn)位。超出量程,則清0 程序源碼: #include "reg52.h" #define uchar unsigned char typedef bit BOOL ; sbit Key1= P3^2; sbit Key2= P3^3; sbit Key3= P3^4; sbit Key4= P3^5; sbit Key5= P3^7; sbit rs = P2^6 ; sbit rw = P3^6 ; sbit ep = P2^7 ; //各位計(jì)數(shù)變量 uchar dis[5]={0x30,0x30,0x30,0x30,0x30}; void delay(uchar ms) { // 延時(shí)子程序 uchar i ; while(ms--) { for(i = 0 ; i<250;i++) ; } } BOOL lcd_bz() { // 測(cè)試LCD忙碌狀態(tài) BOOL result ; rs = 0 ; rw = 1 ; ep = 1 ; result = (BOOL)(P0 & 0x80) ; ep = 0 ; return result ; } void lcd_wcmd(uchar cmd) { // 寫入指令數(shù)據(jù)到LCD while(lcd_bz()) ; rs = 0 ; rw = 0 ; ep = 0 ; P0 = cmd ; ep = 1 ; ep = 0 ; } void lcd_pos(uchar pos) { //設(shè)定顯示位置 lcd_wcmd(pos 0x80) ; } void lcd_wdat(uchar dat) { //寫入字符顯示數(shù)據(jù)到LCD while(lcd_bz()) ; rs = 1 ; rw = 0 ; ep = 0 ; P0 = dat ; ep = 1 ; ep = 0 ; } void lcd_init() { //LCD初始化設(shè)定 lcd_wcmd(0x38) ; //function set delay(1) ; lcd_wcmd(0x38) ; //function set delay(1) ; lcd_wcmd(0x08) ; //display on/off delay(1) ; lcd_wcmd(0x01) ; //清除LCD的顯示內(nèi)容 delay(1) ; lcd_wcmd(0x06) ; //entry mode set delay(1) ; lcd_wcmd(0x0c) ; //entry mode set delay(1) ; } /*--------------- 函數(shù)名稱:display() 功能 :在LCD上顯示數(shù)組的數(shù)據(jù) 說(shuō)明 :先寫顯示地址,后寫顯示數(shù)據(jù) 調(diào)用 :lcd_wcmd(), lcd_pos() 入口參數(shù):pos 寫入的位置,q指向要寫入的數(shù)據(jù)所在的數(shù)組 返回值 :無(wú) ----------------*/ void display(uchar pos, uchar *q) { uchar i ; //lcd_wcmd(0x01) ; //clear delay(10) ; lcd_pos(pos) ; for(i=0;i<5;i++) { lcd_wdat(*q) ; q++ ; delay(10) ; } } void main() { Key1=1; Key2=1; Key3=1; Key4=1; lcd_init() ; // 初始化LCD while(1) //循環(huán)執(zhí)行 { if(!Key1)//如果鍵1按下,下同 { while(!Key1); ++dis[0]; } if(!Key2) { while(!Key2); ++dis[1]; } if(!Key3) { while(!Key3); ++dis[2]; } if(!Key4) { while(!Key4); ++dis[3]; } if(!Key5) { while(!Key5); ++dis[4]; } if((dis[0]-0x30)>9)//個(gè)位進(jìn)位,下同 { dis[0]=0x30; ++dis[1]; } if((dis[1]-0x30)>9) { dis[1]=0x30; ++dis[2]; } if((dis[2]-0x30)>9) { dis[2]=0x30; ++dis[3]; } if((dis[3]-0x30)>9) { dis[3]=0x30; ++dis[4]; } if((dis[4]-0x30)>9) { dis[0]=0x30; dis[1]=0x30; dis[2]=0x30; dis[3]=0x30; dis[4]=0x30; } display(0x00,dis); //只須調(diào)用顯示函數(shù) } }
文件名:按鍵計(jì)數(shù)(lcd).rar |