|
單片機(jī)源程序如下:
- /*******************************************************************
- * 描述:
- * LCD1602可以分為8位和4位控制方式,8位控制方式是用D0-D7數(shù)據(jù)線
- * 來傳送控制命令及數(shù)據(jù)。4位控制方式是用D4-D7數(shù)據(jù)線來傳送控制命令
- * 及數(shù)據(jù)。使用4位數(shù)據(jù)線做控制時(shí),需要分兩次來傳送,先送出高4位數(shù)
- * 據(jù),再送出低4位數(shù)據(jù)?梢怨(jié)省單片機(jī)的4根端口線。
- *
- *******************************************************************/
- #include < reg52.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;
- uchar code cdis1[ ] = {" WELCOME TO "};
- uchar code cdis2[ ] = {" WWW*RICHMCU*COM "};
- /**********************************************************
- * 5us 延時(shí)子程序
- **********************************************************/
- void delayNOP()
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- /**********************************************************
- * 延時(shí)子程序
- **********************************************************/
- void delay(uint ms)
- {
- uchar t;
- while(ms--)
- {
- for(t = 0; t < 120; t++);
- }
- }
- /**********************************************************
- * 檢查L(zhǎng)CD忙狀態(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;
- 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)
- {
- uchar Hdata,Ldata;
- while(lcd_busy());
- Hdata=in_data&0xf0; //取高四位
- Ldata=(in_data<<4)&0xf0; //取低四位
- if(start==0)
- LCD_RS = 0; //寫入命令
- else
- LCD_RS = 1; //寫入數(shù)據(jù)
- LCD_RW = 0;
- LCD_EN = 0;
- delayNOP();
- P0 = Hdata; //發(fā)送高四位
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- delayNOP();
- P0 = Ldata; //發(fā)送低四位
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- delayNOP();
- }
- /*************************************************************
- * LCD初始化設(shè)定
- *************************************************************/
- void lcd_init()
- {
- delay(15);
- lcd_write(0,0x28); //16*2顯示,5*7點(diǎn)陣,4位數(shù)據(jù)
- delay(5);
- lcd_write(0,0x28);
- delay(5);
- lcd_write(0,0x28);
- delay(5);
- lcd_write(0,0x0c); //顯示開,關(guān)光標(biāo)
- delay(5);
- lcd_write(0,0x06); //移動(dòng)光標(biāo)
- delay(5);
- lcd_write(0,0x01); //清除LCD的顯示內(nèi)容
- delay(25); //延時(shí)
- }
- /**********************************************************
- * 設(shè)定顯示位置
- **********************************************************/
- void lcd_pos(uchar pos)
- {
- lcd_write(0,pos|0x80); //數(shù)據(jù)指針=80+地址變量
- }
- /**********************************************************
- * 主函數(shù)
- **********************************************************/
- void main()
- {
- uchar m;
- lcd_init(); //LCD1602初始化
- lcd_pos(0x00); //設(shè)置顯示位置為第一行
- for(m=0;m<16;m++)
- lcd_write(1,cdis1[m]);
- lcd_pos(0x40); //設(shè)置顯示位置為第二行
- for(m=0;m<16;m++)
- lcd_write(1,cdis2[m]);
- while(1);
- }
- /*********************************************************/
復(fù)制代碼
下載:
1602LCD串行方式顯示.rar
(14.21 KB, 下載次數(shù): 46)
2018-7-30 17:24 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|