51單片機 1602液晶顯示控制代碼
>_<:下面是對其各個引腳進行說明:
1 /*--2 REG52.H3 4 Header file for generic 80C52 and 80C32 microcontroller.5 Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.6 All rights reserved.7 --*/8 9 #ifndef __REG52_H__10 #define __REG52_H__11 12 /* BYTE Registers */13 sfr P0 = 0x80;14 sfr P1 = 0x90;15 sfr P2 = 0xA0;16 sfr P3 = 0xB0;17 sfr PSW = 0xD0;18 sfr ACC = 0xE0;19 sfr B = 0xF0;20 sfr SP = 0x81;21 sfr DPL = 0x82;22 sfr DPH = 0x83;23 sfr PCON = 0x87;24 sfr TCON = 0x88;25 sfr TMOD = 0x89;26 sfr TL0 = 0x8A;27 sfr TL1 = 0x8B;28 sfr TH0 = 0x8C;29 sfr TH1 = 0x8D;30 sfr IE = 0xA8;31 sfr IP = 0xB8;32 sfr SCON = 0x98;33 sfr SBUF = 0x99;34 35 /* 8052 Extensions */36 sfr T2CON = 0xC8;37 sfr RCAP2L = 0xCA;38 sfr RCAP2H = 0xCB;39 sfr TL2 = 0xCC;40 sfr TH2 = 0xCD;41 42 43 /* BIT Registers */44 /* PSW */45 sbit CY = PSW^7;46 sbit AC = PSW^6;47 sbit F0 = PSW^5;48 sbit RS1 = PSW^4;49 sbit RS0 = PSW^3;50 sbit OV = PSW^2;51 sbit P = PSW^0; //8052 only52 53 /* TCON */54 sbit TF1 = TCON^7;55 sbit TR1 = TCON^6;56 sbit TF0 = TCON^5;57 sbit TR0 = TCON^4;58 sbit IE1 = TCON^3;59 sbit IT1 = TCON^2;60 sbit IE0 = TCON^1;61 sbit IT0 = TCON^0;62 63 /* IE */64 sbit EA = IE^7;65 sbit ET2 = IE^5; //8052 only66 sbit ES = IE^4;67 sbit ET1 = IE^3;68 sbit EX1 = IE^2;69 sbit ET0 = IE^1;70 sbit EX0 = IE^0;71 72 /* IP */73 sbit PT2 = IP^5;74 sbit PS = IP^4;75 sbit PT1 = IP^3;76 sbit PX1 = IP^2;77 sbit PT0 = IP^1;78 sbit PX0 = IP^0;79 80 /* P3 */81 sbit RD = P3^7;82 sbit WR = P3^6;83 sbit T1 = P3^5;84 sbit T0 = P3^4;85 sbit INT1 = P3^3;86 sbit INT0 = P3^2;87 sbit TXD = P3^1;88 sbit RXD = P3^0;89 90 /* SCON */91 sbit SM0 = SCON^7;92 sbit SM1 = SCON^6;93 sbit SM2 = SCON^5;94 sbit REN = SCON^4;95 sbit TB8 = SCON^3;96 sbit RB8 = SCON^2;97 sbit TI = SCON^1;98 sbit RI = SCON^0;99 100 /* P1 */101 sbit T2EX = P1^1; // 8052 only102 sbit T2 = P1^0; // 8052 only103 104 /* T2CON */105 sbit TF2 = T2CON^7;106 sbit EXF2 = T2CON^6;107 sbit RCLK = T2CON^5;108 sbit TCLK = T2CON^4;109 sbit EXEN2 = T2CON^3;110 sbit TR2 = T2CON^2;sbit C_T2 = T2CON^1;112 sbit CP_RL2 = T2CON^0;113 114 #endif
1 /*--2 INTRINS.H3 4 Intrinsic functions for C51.5 Copyright (c) 1988-2004 Keil Elektronik GmbH and Keil Software, Inc.6 All rights reserved.7 --*/8 9 #ifndef __INTRINS_H__10 #define __INTRINS_H__11 12 extern void _nop_ (void);13 extern bit _testbit_ (bit);14 extern unsigned char _cror_ (unsigned char, unsigned char);15 extern unsigned int _iror_ (unsigned int, unsigned char);16 extern unsigned long _lror_ (unsigned long, unsigned char);17 extern unsigned char _crol_ (unsigned char, unsigned char);18 extern unsigned int _irol_ (unsigned int, unsigned char);19 extern unsigned long _lrol_ (unsigned long, unsigned char);20 extern unsigned char _chkfloat_(float);21 extern void _push_ (unsigned char _sfr);22 extern void _pop_ (unsigned char _sfr);23 24 #endif
1 /*--2 名稱:LCD16023 論壇:www.doflye.net4 編寫:shifang5 內(nèi)容:通過標準程序靜態(tài)顯示字符6 引腳定義如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK7 */8 #include//包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義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 /*26 uS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值27 unsigned char 是定義無符號字符變量,其值的范圍是28 0~255 這里使用晶振12M,精確延時請使用匯編,大致延時29 長度如下 T=tx2+5 uS 30 */31 void DelayUs2x(unsigned char t)32 { 33 while(--t);34 }35 /*36 mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值37 unsigned char 是定義無符號字符變量,其值的范圍是38 0~255 這里使用晶振12M,精確延時請使用匯編39 */40 void DelayMs(unsigned char t)41 { 42 while(t--)43 {44 //大致延時1mS45 DelayUs2x(245);46 DelayUs2x(245);47 }48 }49 /*50 判忙函數(shù)51 */52 bit LCD_Check_Busy(void) 53 { 54 DataPort= 0xFF; 55 RS_CLR; 56 RW_SET; 57 EN_CLR; 58 _nop_(); 59 EN_SET;60 return (bit)(DataPort & 0x80);61 }62 /*63 寫入命令函數(shù)64 */65 void LCD_Write_Com(unsigned char com) 66 { 67 // while(LCD_Check_Busy()); //忙則等待68 DelayMs(5);69 RS_CLR; 70 RW_CLR; 71 EN_SET; 72 DataPort= com; 73 _nop_(); 74 EN_CLR;75 }76 /*77 寫入數(shù)據(jù)函數(shù)78 */79 void LCD_Write_Data(unsigned char Data) 80 { 81 //while(LCD_Check_Busy()); //忙則等待82 DelayMs(5);83 RS_SET; 84 RW_CLR; 85 EN_SET; 86 DataPort= Data; 87 _nop_();88 EN_CLR;89 }90 91 /*92 清屏函數(shù)93 */94 void LCD_Clear(void) 95 { 96 LCD_Write_Com(0x01); 97 DelayMs(5);98 }99 /*100 寫入字符串函數(shù)101 */102 void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) 103 { 104 if (y == 0) 105 { 106 LCD_Write_Com(0x80 + x); //表示第一行107 }108 else 109 { 110 LCD_Write_Com(0xC0 + x); //表示第二行} 112 while (*s) 113 { 114 LCD_Write_Data( *s); 115 s ++; 116 }117 }118 /*119 寫入字符函數(shù)120 */121 void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) 122 { 123 if (y == 0) 124 { 125 LCD_Write_Com(0x80 + x); 126 } 127 else 128 { 129 LCD_Write_Com(0xC0 + x); 130 } 131 LCD_Write_Data( Data); 132 }133 /*134 初始化函數(shù)135 */136 void LCD_Init(void) 137 {138 LCD_Write_Com(0x38); /*顯示模式設(shè)置*/ 139 DelayMs(5); 140 LCD_Write_Com(0x38); 141 DelayMs(5); 142 LCD_Write_Com(0x38); 143 DelayMs(5); 144 LCD_Write_Com(0x38); 145 LCD_Write_Com(0x08); /*顯示關(guān)閉*/ 146 LCD_Write_Com(0x01); /*顯示清屏*/ 147 LCD_Write_Com(0x06); /*顯示光標移動設(shè)置*/ 148 DelayMs(5); 149 LCD_Write_Com(0x0C); /*顯示開及光標設(shè)置*/150 }151 152 /*153 主函數(shù)[上面已經(jīng)把顯示功能封裝好了,今后再寫其他顯示就直接調(diào)用]154 */ 155 void main(void) 156 { 157 LCD_Init(); 158 LCD_Clear();//清屏159 while (1) 160 { 161 LCD_Write_Char(7,0,o);162 LCD_Write_Char(8,0,k);163 LCD_Write_String(1,1,"www.doflye.net");164 while(1);165 }166 }
評論