|
下载
1602lcd.rar
(1.1 KB, 下载次数: 19)
- #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 延时子程序
- **********************************************************/
- void lcd_delayNOP()
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- /**********************************************************
- * 延时子程序
- **********************************************************/
- void lcd_delay(uint ms)
- {
- uchar t;
- while(ms--)
- {
- for(t = 0; t < 120; t++);
- }
- }
- /**********************************************************
- * 检查LCD忙状态
- * lcd_busy为1时,忙,等待。
- * lcd-busy为0时,闲,可写指令与数据
- **********************************************************/
- 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);
- }
- /*********************************************************
- * 写指令或数据
- * start=0, 写入命令; start=1, 写入数据
- *********************************************************/
- void lcd_write(bit start, uchar in_data)
- {
- while(lcd_busy());
- if(start==0)
- LCD_RS = 0; //写入命令
- else
- LCD_RS = 1; //写入数据
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
- P0 = in_data; //发送8位
- lcd_delayNOP();
- LCD_EN = 1;
- lcd_delayNOP();
- LCD_EN = 0;
- }
- /*************************************************************
- * LCD初始化设定
- *************************************************************/
- void lcd_init()
- {
- lcd_delay(15);
- lcd_write(0,0x38); //16*2显示,5*7点阵,8位数据
- lcd_delay(5);
- lcd_write(0,0x38);
- lcd_delay(5);
- lcd_write(0,0x38);
- lcd_delay(5);
- lcd_write(0,0x0c); //显示开,关光标
- lcd_delay(5);
- lcd_write(0,0x06); //移动光标
- lcd_delay(5);
- lcd_write(0,0x01); //清除LCD的显示内容
- lcd_delay(25); //延时
- }
- /**********************************************************
- * 设定显示的行、列位置 (行的范围: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); //数据指针=80+地址变量
- else if(hang==2&&(lie-1)<=127&&(lie)>0)
- lcd_write(0,(lie-1)|0xC0); //数据指针=C0+地址变量
- }
- /*********************************************************
- 专门写单个数字
- *********************************************************/
- void lcdwrite_sz(uchar in_data)
- {
- while(lcd_busy());
- if(0<=in_data<=9)
- {
- LCD_RS = 1; //写入数据
-
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
-
- P0 = in_data+0x30; //发送8位
- lcd_delayNOP();
- LCD_EN = 1;
- lcd_delayNOP();
- LCD_EN = 0;
- }
- }
- /*********************************************************
- 专门写单个字母(大小写的“a-z”)
- *********************************************************/
- void lcdwrite_zm(uchar in_data)
- {
- while(lcd_busy());
- if(0x20<=in_data<=0x7f)
- {
- LCD_RS = 1; //写入数据
-
- LCD_RW = 0;
- LCD_EN = 0;
- lcd_delayNOP();
-
- P0 = in_data; //发送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的显示内容
- lcd_delay(25); //延时
- }
复制代码
|
|