[51单片机] 1602液晶显示控制代码

>_<:引脚的连接方式

>_<:下面是对其各个引脚进行说明:

  1 /*--------------------------------------------------------------------------
  2 REG52.H
  3 
  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 only
 52 
 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 only
 66 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 only
102 sbit T2    = P1^0; // 8052 only
103              
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;
111 sbit C_T2   = T2CON^1;
112 sbit CP_RL2 = T2CON^0;
113 
114 #endif
reg52.h 一般自带直接引用就含有这个文件啦
 1 /*--------------------------------------------------------------------------
 2 INTRINS.H
 3 
 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
intrins.h 一般自带 
  1 /*-----------------------------------------------
  2 名称:LCD1602
  3 论坛:www.doflye.net
  4 编写:shifang
  5 内容:通过标准程序静态显示字符
  6 引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
  7 ------------------------------------------------*/
  8 #include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
  9 #include<intrins.h>
 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=1
 17 
 18 #define RW_CLR RW=0 
 19 #define RW_SET RW=1 
 20 
 21 #define EN_CLR EN=0
 22 #define EN_SET EN=1
 23 
 24 #define DataPort P0
 25 /*------------------------------------------------
 26 uS延时函数,含有输入参数 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延时函数,含有输入参数 unsigned char t,无返回值
 37 unsigned char 是定义无符号字符变量,其值的范围是
 38 0~255 这里使用晶振12M,精确延时请使用汇编
 39 ------------------------------------------------*/
 40 void DelayMs(unsigned char t)
 41 {  
 42     while(t--)
 43     {
 44         //大致延时1mS
 45         DelayUs2x(245);
 46         DelayUs2x(245);
 47     }
 48 }
 49 /*------------------------------------------------
 50 判忙函数
 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 写入命令函数
 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 写入数据函数
 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 清屏函数
 93 ------------------------------------------------*/
 94 void LCD_Clear(void) 
 95 { 
 96     LCD_Write_Com(0x01); 
 97     DelayMs(5);
 98 }
 99 /*------------------------------------------------
100 写入字符串函数
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);      //表示第二行
111     }        
112     while (*s) 
113     {     
114         LCD_Write_Data( *s);     
115         s ++;     
116     }
117 }
118 /*------------------------------------------------
119 写入字符函数
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 初始化函数
135 ------------------------------------------------*/
136 void LCD_Init(void) 
137 {
138     LCD_Write_Com(0x38);    /*显示模式设置*/ 
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);    /*显示关闭*/ 
146     LCD_Write_Com(0x01);    /*显示清屏*/ 
147     LCD_Write_Com(0x06);    /*显示光标移动设置*/ 
148     DelayMs(5); 
149     LCD_Write_Com(0x0C);    /*显示开及光标设置*/
150 }
151 
152 /*------------------------------------------------
153 主函数[上面已经把显示功能封装好了,今后再写其他显示就直接调用]
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 }

 

posted @ 2014-04-02 18:05  beautifulzzzz  阅读(9638)  评论(11编辑  收藏  举报