[51 단편기] 1602 LCD 디스플레이 제어 코드2

36835 단어 단편기

 
이것은 0-F의 16개의 문자를 입력하고 화면에 표시하는 4X4 키입니다. [여기는 단편기 p1의 8개의 발과 행렬 키보드의 8개의 발을 추가로 연결해야 합니다.]
 
  1 /*-----------------------------------------------
  2   :LCD1602
  3   :www.doflye.net
  4   :shifang
  5   :        ,    0-F16   
  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 #define KeyPort P1
 26 
 27 unsigned char code dofly_code[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//          
 28 /*------------------------------------------------
 29 uS    ,       unsigned char t,    
 30 unsigned char           ,      
 31 0~255       12M,         ,    
 32      T=tx2+5 uS 
 33 ------------------------------------------------*/
 34 void DelayUs2x(unsigned char t)
 35 {   
 36     while(--t);
 37 }
 38 /*------------------------------------------------
 39 mS    ,       unsigned char t,    
 40 unsigned char           ,      
 41 0~255       12M,         
 42 ------------------------------------------------*/
 43 void DelayMs(unsigned char t)
 44 {
 45     
 46     while(t--)
 47     {
 48         //    1mS
 49         DelayUs2x(245);
 50         DelayUs2x(245);
 51     }
 52 }
 53 /*------------------------------------------------
 54     
 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       
 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       
 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     
 96 ------------------------------------------------*/
 97 void LCD_Clear(void) 
 98 { 
 99     LCD_Write_Com(0x01); 
100     DelayMs(5);
101 }
102 /*------------------------------------------------
103        
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     }
111     else 
112     {     
113         LCD_Write_Com(0xC0 + x);     
114     }        
115     while (*s) 
116     {     
117         LCD_Write_Data( *s);     
118         s ++;     
119     }
120 }
121 /*------------------------------------------------
122       
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      
138 ------------------------------------------------*/
139 void LCD_Init(void) 
140 {
141     LCD_Write_Com(0x38);    /*      */ 
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);    /*    */ 
149     LCD_Write_Com(0x01);    /*    */ 
150     LCD_Write_Com(0x06);    /*        */ 
151     DelayMs(5); 
152     LCD_Write_Com(0x0C);    /*        */
153 }
154 
155 /*------------------------------------------------
156 157 ------------------------------------------------*/
158 unsigned char KeyScan(void)  //
159 {
160     unsigned char cord_h,cord_l;//       
161     KeyPort=0x0f;            //      0
162     cord_h=KeyPort&0x0f;     //     
163     if(cord_h!=0x0f)    //         
164     {                                                                                                              
165         DelayMs(10);        //  
166         if((KeyPort&0x0f)!=0x0f)
167         {
168             cord_h=KeyPort&0x0f;  //     
169             KeyPort=cord_h|0xf0;  //       
170             cord_l=KeyPort&0xf0;  //     
171             
172             while((KeyPort&0xf0)!=0xf0);//       
173             
174             return(cord_h+cord_l);//        
175         }
176     }return(0xff);     //    
177 }
178 /*------------------------------------------------
179 180 ------------------------------------------------*/
181 unsigned char KeyPro(void)
182 {
183     switch(KeyScan())
184     {
185     case 0x7e:return 0;break;//0               
186     case 0x7d:return 1;break;//1
187     case 0x7b:return 2;break;//2
188     case 0x77:return 3;break;//3
189     case 0xbe:return 4;break;//4
190     case 0xbd:return 5;break;//5
191     case 0xbb:return 6;break;//6
192     case 0xb7:return 7;break;//7
193     case 0xde:return 8;break;//8
194     case 0xdd:return 9;break;//9
195     case 0xdb:return 10;break;//a
196     case 0xd7:return 11;break;//b
197     case 0xee:return 12;break;//c
198     case 0xed:return 13;break;//d
199     case 0xeb:return 14;break;//e
200     case 0xe7:return 15;break;//f
201     default:return 0xff;break;
202     }
203 }
204 
205 /*------------------------------------------------
206    
207 ------------------------------------------------*/ 
208 void main(void) 
209 { 
210     unsigned char i,j,num; 
211     
212     LCD_Init(); 
213     LCD_Write_Com(0x0F);//
214     
215     LCD_Write_String(0,0,"Press the key !");
216     
217     while (1) 
218     {  
219         num=KeyPro();
220         if(num!=0xff)
221         {
222             if((i==0)&&(j==0))//          
223                 LCD_Clear();//  
224             LCD_Write_Char(0+i,0+j,dofly_code[num]);//        
225             i++;
226             if(i==16)//
227             {
228                 i=0;j++;
229                 if(j==2)//  2     ,           
230                 {
231                     j=0;
232                 }
233             }
234         }
235     }
236 }

 
 

좋은 웹페이지 즐겨찾기