|
下載
1602lcd.rar
(1.1 KB, 下載次數(shù): 19)
2017-4-24 02:34 上傳
點(diǎn)擊文件名下載附件
11 下載積分: 黑幣 -5
- #include < reg51.h >
- #include < intrins.h >
- #define uchar unsigned char
- #define uint unsigned int
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- /**********************************************************
- * 5us 延時(shí)子程序
- **********************************************************/
- void lcd_delayNOP()
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- /**********************************************************
- * 延時(shí)子程序
- **********************************************************/
- void lcd_delay(uint ms)
- {
- uchar t;
- while(ms--)
- {
- for(t = 0; t < 120; t++);
- }
- }
- /**********************************************************
- * 檢查LCD忙狀態(tài)
- * lcd_busy為1時(shí),忙,等待。
- * lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)
- **********************************************************/
- bit lcd_busy()
- {
- bit result;
- LCD_RS = 0;
- LCD_RW = 1;
- LCD_EN = 1;
- lcd_delayNOP();
- result = (bit)(P0&0x80);
- LCD_EN = 0;
- return(result);
- }
- /*********************************************************
- * 寫指令或數(shù)據(jù)
- * start=0, 寫入命令; start=1, 寫入數(shù)據(jù)
- *********************************************************/
- void lcd_write(bit start, uchar in_data)
- {
- while(lcd_busy());
- if(start==0)
- LCD_RS = 0; //寫入命令
- else
- LCD_RS = 1; //寫入數(shù)據(jù)
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
- P0 = in_data; //發(fā)送8位
- lcd_delayNOP();
- LCD_EN = 1;
- lcd_delayNOP();
- LCD_EN = 0;
- }
- /*************************************************************
- * LCD初始化設(shè)定
- *************************************************************/
- void lcd_init()
- {
- lcd_delay(15);
- lcd_write(0,0x38); //16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)
- lcd_delay(5);
- lcd_write(0,0x38);
- lcd_delay(5);
- lcd_write(0,0x38);
- lcd_delay(5);
- lcd_write(0,0x0c); //顯示開,關(guān)光標(biāo)
- lcd_delay(5);
- lcd_write(0,0x06); //移動(dòng)光標(biāo)
- lcd_delay(5);
- lcd_write(0,0x01); //清除LCD的顯示內(nèi)容
- lcd_delay(25); //延時(shí)
- }
- /**********************************************************
- * 設(shè)定顯示的行、列位置 (行的范圍:1——2, 列的范圍:1——16)
-
- **********************************************************/
- void lcd_pos(uchar hang,uchar lie)
- {
- if(hang==1&&(lie-1)<=16&&(lie)>0)
- lcd_write(0,(lie-1)|0x80); //數(shù)據(jù)指針=80+地址變量
- else if(hang==2&&(lie-1)<=127&&(lie)>0)
- lcd_write(0,(lie-1)|0xC0); //數(shù)據(jù)指針=C0+地址變量
- }
- /*********************************************************
- 專門寫單個(gè)數(shù)字
- *********************************************************/
- void lcdwrite_sz(uchar in_data)
- {
- while(lcd_busy());
- if(0<=in_data<=9)
- {
- LCD_RS = 1; //寫入數(shù)據(jù)
-
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
-
- P0 = in_data+0x30; //發(fā)送8位
- lcd_delayNOP();
- LCD_EN = 1;
- lcd_delayNOP();
- LCD_EN = 0;
- }
- }
- /*********************************************************
- 專門寫單個(gè)字母(大小寫的“a-z”)
- *********************************************************/
- void lcdwrite_zm(uchar in_data)
- {
- while(lcd_busy());
- if(0x20<=in_data<=0x7f)
- {
- LCD_RS = 1; //寫入數(shù)據(jù)
-
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
-
- P0 = in_data; //發(fā)送8位
- lcd_delayNOP();
- LCD_EN = 1;
- lcd_delayNOP();
- LCD_EN = 0;
- }
- }
- void lcdwrite_string(unsigned char *str)
- {
- while(*str!='\0')
- {
- lcdwrite_zm(*str);
- str++;
- }
- }
- void lcd_clear()
- {
- lcd_write(0,0x01); //清除LCD的顯示內(nèi)容
- lcd_delay(25); //延時(shí)
- }
復(fù)制代碼
|
-
-
1602lcd.rar
2017-4-24 02:35 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
1.1 KB, 下載次數(shù): 11, 下載積分: 黑幣 -5
|