1602按鍵輸入顯示
單片機源程序如下:
- /*-----------------------------------------------
- 名稱:LCD1602
- 內容:通過矩陣鍵盤輸入,依次顯示0-F16中字符
- 引腳定義如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
- ------------------------------------------------*/
- #include<reg52.h> //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義
- #include<intrins.h>
- sbit RS = P2^4; //定義端口
- sbit RW = P2^5;
- sbit EN = P2^6;
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- #define DataPort P0
- #define KeyPort P1
- unsigned char code dofly_code[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//轉換成液晶顯示的字符
- /*------------------------------------------------
- uS延時函數,含有輸入參數 unsigned char t,無返回值
- unsigned char 是定義無符號字符變量,其值的范圍是
- 0~255 這里使用晶振12M,精確延時請使用匯編,大致延時
- 長度如下 T=tx2+5 uS
- ------------------------------------------------*/
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- /*------------------------------------------------
- mS延時函數,含有輸入參數 unsigned char t,無返回值
- unsigned char 是定義無符號字符變量,其值的范圍是
- 0~255 這里使用晶振12M,精確延時請使用匯編
- ------------------------------------------------*/
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延時1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- /*------------------------------------------------
- 判忙函數
- ------------------------------------------------*/
- bit LCD_Check_Busy(void)
- {
- DataPort= 0xFF;
- RS_CLR;
- RW_SET;
- EN_CLR;
- _nop_();
- EN_SET;
- return (bit)(DataPort & 0x80);
- }
- /*------------------------------------------------
- 寫入命令函數
- ------------------------------------------------*/
- void LCD_Write_Com(unsigned char com)
- {
- while(LCD_Check_Busy()); //忙則等待
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort= com;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
- 寫入數據函數
- ------------------------------------------------*/
- void LCD_Write_Data(unsigned char Data)
- {
- while(LCD_Check_Busy()); //忙則等待
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort= Data;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
- 清屏函數
- ------------------------------------------------*/
- void LCD_Clear(void)
- {
- LCD_Write_Com(0x01);
- DelayMs(5);
- }
- /*------------------------------------------------
- 寫入字符串函數
- ------------------------------------------------*/
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- while (*s)
- {
- LCD_Write_Data( *s);
- s ++;
- }
- }
- /*------------------------------------------------
- 寫入字符函數
- ------------------------------------------------*/
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- /*------------------------------------------------
- 初始化函數
- ------------------------------------------------*/
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38); /*顯示模式設置*/
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x08); /*顯示關閉*/
- LCD_Write_Com(0x01); /*顯示清屏*/
- LCD_Write_Com(0x06); /*顯示光標移動設置*/
- DelayMs(5);
- LCD_Write_Com(0x0C); /*顯示開及光標設置*/
- }
-
- /*------------------------------------------------
- 按鍵掃描函數,返回掃描鍵值
- ------------------------------------------------*/
- unsigned char KeyScan(void) //鍵盤掃描函數,使用行列反轉掃描法
- {
- unsigned char cord_h,cord_l;//行列值中間變量
- KeyPort=0x0f; //行線輸出全為0
- cord_h=KeyPort&0x0f; //讀入列線值
- if(cord_h!=0x0f) //先檢測有無按鍵按下
- {
- DelayMs(10); //去抖
- if((KeyPort&0x0f)!=0x0f)
- {
- cord_h=KeyPort&0x0f; //讀入列線值
- KeyPort=cord_h|0xf0; //輸出當前列線值
- cord_l=KeyPort&0xf0; //讀入行線值
- while((KeyPort&0xf0)!=0xf0);//等待松開并輸出
- return(cord_h+cord_l);//鍵盤最后組合碼值
- }
- }return(0xff); //返回該值
- }
- /*------------------------------------------------
- 按鍵值處理函數,返回掃鍵值
- ------------------------------------------------*/
- unsigned char KeyPro(void)
- {
- switch(KeyScan())
- {
- case 0x7e:return 0;break;//0 按下相應的鍵顯示相對應的碼值
- case 0x7d:return 1;break;//1
- case 0x7b:return 2;break;//2
- case 0x77:return 3;break;//3
- case 0xbe:return 4;break;//4
- case 0xbd:return 5;break;//5
- case 0xbb:return 6;break;//6
- case 0xb7:return 7;break;//7
- case 0xde:return 8;break;//8
- case 0xdd:return 9;break;//9
- case 0xdb:return 10;break;//a
- case 0xd7:return 11;break;//b
- case 0xee:return 12;break;//c
- case 0xed:return 13;break;//d
- case 0xeb:return 14;break;//e
- case 0xe7:return 15;break;//f
- default:return 0xff;break;
- }
- }
- /*------------------------------------------------
- 主函數
- ------------------------------------------------*/
- void main(void)
- {
- unsigned char i,j,num;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
1602按鍵輸入顯示.zip
(19.52 KB, 下載次數: 32)
2017-12-20 11:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|