51單片機(jī) 1602液晶顯示控制代碼2
[這里要額外的把單片機(jī)p1的8個(gè)腳和矩陣鍵盤(pán)的8個(gè)腳相連]
本文引用地址:http://m.butianyuan.cn/article/201611/318332.htm1 /*--2 名稱(chēng):LCD16023 論壇:www.doflye.net4 編寫(xiě):shifang5 內(nèi)容:通過(guò)矩陣鍵盤(pán)輸入,依次顯示0-F16中字符6 引腳定義如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK7 */8 #include//包含頭文件,一般情況不需要改動(dòng),頭文件包含特殊功能寄存器的定義9 #include 10 11 sbit RS = P2^4; //定義端口 12 sbit RW = P2^5;13 sbit EN = P2^6;14 15 #define RS_CLR RS=0 16 #define RS_SET RS=117 18 #define RW_CLR RW=0 19 #define RW_SET RW=1 20 21 #define EN_CLR EN=022 #define EN_SET EN=123 24 #define DataPort P025 #define KeyPort P126 27 unsigned char code dofly_code[]={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F};//轉(zhuǎn)換成液晶顯示的字符28 /*29 uS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無(wú)返回值30 unsigned char 是定義無(wú)符號(hào)字符變量,其值的范圍是31 0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編,大致延時(shí)32 長(zhǎng)度如下 T=tx2+5 uS 33 */34 void DelayUs2x(unsigned char t)35 { 36 while(--t);37 }38 /*39 mS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無(wú)返回值40 unsigned char 是定義無(wú)符號(hào)字符變量,其值的范圍是41 0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編42 */43 void DelayMs(unsigned char t)44 {45 46 while(t--)47 {48 //大致延時(shí)1mS49 DelayUs2x(245);50 DelayUs2x(245);51 }52 }53 /*54 判忙函數(shù)55 */56 bit LCD_Check_Busy(void) 57 { 58 DataPort= 0xFF; 59 RS_CLR; 60 RW_SET; 61 EN_CLR; 62 _nop_(); 63 EN_SET;64 return (bit)(DataPort & 0x80);65 }66 /*67 寫(xiě)入命令函數(shù)68 */69 void LCD_Write_Com(unsigned char com) 70 { 71 // while(LCD_Check_Busy()); //忙則等待72 DelayMs(5);73 RS_CLR; 74 RW_CLR; 75 EN_SET; 76 DataPort= com; 77 _nop_(); 78 EN_CLR;79 }80 /*81 寫(xiě)入數(shù)據(jù)函數(shù)82 */83 void LCD_Write_Data(unsigned char Data) 84 { 85 //while(LCD_Check_Busy()); //忙則等待86 DelayMs(5);87 RS_SET; 88 RW_CLR; 89 EN_SET; 90 DataPort= Data; 91 _nop_();92 EN_CLR;93 }94 /*95 清屏函數(shù)96 */97 void LCD_Clear(void) 98 { 99 LCD_Write_Com(0x01); 100 DelayMs(5);101 }102 /*103 寫(xiě)入字符串函數(shù)104 */105 void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) 106 { 107 if (y == 0) 108 { 109 LCD_Write_Com(0x80 + x); 110 }else 112 { 113 LCD_Write_Com(0xC0 + x); 114 } 115 while (*s) 116 { 117 LCD_Write_Data( *s); 118 s ++; 119 }120 }121 /*122 寫(xiě)入字符函數(shù)123 */124 void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) 125 { 126 if (y == 0) 127 { 128 LCD_Write_Com(0x80 + x); 129 } 130 else 131 { 132 LCD_Write_Com(0xC0 + x); 133 } 134 LCD_Write_Data( Data); 135 }136 /*137 初始化函數(shù)138 */139 void LCD_Init(void) 140 {141 LCD_Write_Com(0x38); /*顯示模式設(shè)置*/ 142 DelayMs(5); 143 LCD_Write_Com(0x38); 144 DelayMs(5); 145 LCD_Write_Com(0x38); 146 DelayMs(5); 147 LCD_Write_Com(0x38); 148 LCD_Write_Com(0x08); /*顯示關(guān)閉*/ 149 LCD_Write_Com(0x01); /*顯示清屏*/ 150 LCD_Write_Com(0x06); /*顯示光標(biāo)移動(dòng)設(shè)置*/ 151 DelayMs(5); 152 LCD_Write_Com(0x0C); /*顯示開(kāi)及光標(biāo)設(shè)置*/153 }154 155 /*156 按鍵掃描函數(shù),返回掃描鍵值157 */158 unsigned char KeyScan(void) //鍵盤(pán)掃描函數(shù),使用行列反轉(zhuǎn)掃描法159 {160 unsigned char cord_h,cord_l;//行列值中間變量161 KeyPort=0x0f; //行線輸出全為0162 cord_h=KeyPort&0x0f; //讀入列線值163 if(cord_h!=0x0f) //先檢測(cè)有無(wú)按鍵按下164 { 165 DelayMs(10); //去抖166 if((KeyPort&0x0f)!=0x0f)167 {168 cord_h=KeyPort&0x0f; //讀入列線值169 KeyPort=cord_h0xf0; //輸出當(dāng)前列線值170 cord_l=KeyPort&0xf0; //讀入行線值171 172 while((KeyPort&0xf0)!=0xf0);//等待松開(kāi)并輸出173 174 return(cord_h+cord_l);//鍵盤(pán)最后組合碼值175 }176 }return(0xff); //返回該值177 }178 /*179 按鍵值處理函數(shù),返回掃鍵值180 */181 unsigned char KeyPro(void)182 {183 switch(KeyScan())184 {185 case 0x7e:return 0;break;//0 按下相應(yīng)的鍵顯示相對(duì)應(yīng)的碼值186 case 0x7d:return 1;break;//1187 case 0x7b:return 2;break;//2188 case 0x77:return 3;break;//3189 case 0xbe:return 4;break;//4190 case 0xbd:return 5;break;//5191 case 0xbb:return 6;break;//6192 case 0xb7:return 7;break;//7193 case 0xde:return 8;break;//8194 case 0xdd:return 9;break;//9195 case 0xdb:return 10;break;//a196 case 0xd7:return 11;break;//b197 case 0xee:return 12;break;//c198 case 0xed:return 13;break;//d199 case 0xeb:return 14;break;//e200 case 0xe7:return 15;break;//f201 default:return 0xff;break;202 }203 }204 205 /*206 主函數(shù)207 */ 208 void main(void) 209 { 210 unsigned char i,j,num; 211 212 LCD_Init(); 213 LCD_Write_Com(0x0F);//光標(biāo)開(kāi),光標(biāo)閃爍開(kāi)214 215 LCD_Write_String(0,0,"Press the key !");216 217 while (1) 218 { 219 num=KeyPro();220 if(num!=0xff)221 {if((i==0)&&(j==0))//回到第一個(gè)字符時(shí)清屏223 LCD_Clear();//清屏224 LCD_Write_Char(0+i,0+j,dofly_code[num]);//依次顯示輸入字符225 i++;226 if(i==16)//如果第一行顯示滿,轉(zhuǎn)到第二行227 {228 i=0;j++;229 if(j==2)//如果2行都顯示滿,清屏后重新從第一行顯示230 {231 j=0;232 }233 }234 }235 }236 }
評(píng)論