LCD1602.c
/** * 文件名:LCD1602.c * 描 述:1602字符液晶驱动模块 */ #include <reg52.h> #include "stdint.h" #include "LCD1602.h" #define LCD1602_DB P0 sbit LCD1602_RS = P1^0; sbit LCD1602_RW = P1^1; sbit LCD1602_E = P1^5; /* 等待液晶准备好 */ void LCDwaitReady() { uint8_t state; LCD1602_DB = 0xFF; LCD1602_RS = 0; LCD1602_RW = 1; do { LCD1602_E = 1; state = LCD1602_DB; //读取状态字 LCD1602_E = 0; } while (state & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止 } /* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */ void LCDwriteCmd(uint8_t cmd) { LCDwaitReady(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_E = 1; LCD1602_E = 0; } /* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */ void LCDwriteDat(uint8_t dat) { LCDwaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_E = 1; LCD1602_E = 0; } /* 设置显示RAM起始地址,亦即光标位置,(col, row)-对应屏幕上的字符坐标 */ void LCDsetCursor(uint8_t col, uint8_t row) { uint8_t addr; if (row == 0) //由输入的屏幕坐标计算显示RAM的地址 addr = col; //第一行字符地址从0x00起始 else addr = 0x40 + col; //第二行字符地址从0x40起始 LCDwriteCmd(addr | 0x80); //设置RAM地址 } /* 在液晶上显示字符串,(col, row)-对应屏幕上的起始坐标,str-字符串指针 */ void LCDprintStr(uint8_t col, uint8_t row, uint8_t * str) { LCDsetCursor(col, row); //设置起始地址 //连续写入字符串数据,直到检测到结束符 while (*str != '\0') { LCDwriteDat(*str++); } } /* 初始化1602液晶 */ void LCDinit() { LCDwriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口 LCDwriteCmd(0x0C); //显示器开,光标关闭 LCDwriteCmd(0x06); //文字不动,地址自动+1 LCDwriteCmd(0x01); //清屏 } /* 清除液晶显示屏上的内容,同时将光标移动到初始位置左上角 */ void LCDclear() { LCDwriteCmd(0x01); } /* 将光标移动到初始位置左上角,但不清除液晶屏幕上的内容 */ void LCDhome() { LCDwriteCmd(0x02); } /* 显示开关控制, * 控制整体显示的开/关,光标的开/关,光标是否闪烁: * 由combine的低三位决定,高电平表示开,低电平表示关 */ void LCDctrlDisplayCursorBlink(uint8_t display, uint8_t cursor, uint8_t blink) { uint8_t combine; combine = ((display << 2) | (cursor << 1) | blink); LCDwriteCmd(0x08 | combine); } void LCDsetCursordirCharshift(uint8_t cursorDir, uint8_t charShift) { uint8_t combine; combine = ((cursorDir << 1) | charShift); LCDwriteCmd(0x04 | combine); } void LCDsetShiftwhatShiftdir(uint8_t shiftWhat, uint8_t shiftDir) { uint8_t combine; combine = ((shiftWhat << 1) | shiftDir); LCDwriteCmd(0x10 | combine); }