LCD1602模块驱动程序代码
时间: 2023-10-15 07:05:53 浏览: 418
LCD1602驱动代码
以下是 LCD1602 模块驱动程序代码,基于 C 语言:
```c
#include <reg52.h>
#include <intrins.h>
#define LCD_DataPort P0
#define LCD_DataBus P0
#define LCD_DataPort_Mode P0M0
#define LCD_DataPort_Type P0T0
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
void delay_ms(unsigned int t) {
unsigned int i, j;
for (i = 0; i < t; i++) {
for (j = 0; j < 114; j++);
}
}
void LCD_WriteCommand(unsigned char cmd) {
LCD_RS = 0;
LCD_RW = 0;
LCD_DataPort = cmd;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
delay_ms(1);
}
void LCD_WriteData(unsigned char dat) {
LCD_RS = 1;
LCD_RW = 0;
LCD_DataPort = dat;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
delay_ms(1);
}
void LCD_Init() {
LCD_DataPort_Mode = 0x00;
LCD_DataPort_Type = 0xFF;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
delay_ms(20);
LCD_WriteCommand(0x38); // Function Set: 8-bit, 2-line, 5x8 font
LCD_WriteCommand(0x0C); // Display On/Off Control: Display ON, Cursor OFF, Blink OFF
LCD_WriteCommand(0x06); // Entry Mode Set: Increment cursor position, No display shift
LCD_WriteCommand(0x01); // Clear Display
delay_ms(2);
}
void LCD_SetCursor(unsigned char x, unsigned char y) {
unsigned char addr;
if (y == 0) {
addr = 0x80 + x;
} else {
addr = 0xC0 + x;
}
LCD_WriteCommand(addr);
}
void LCD_WriteString(unsigned char x, unsigned char y, unsigned char *str) {
LCD_SetCursor(x, y);
while (*str) {
LCD_WriteData(*str++);
}
}
void main() {
LCD_Init();
LCD_WriteString(0, 0, "Hello, World!");
while(1);
}
```
这个程序包含了 LCD1602 模块的初始化,写入命令和数据,设置光标位置以及写入字符串等函数。你可以根据自己的需要进行修改和扩展。
阅读全文