需要注意,1602对应xy。x表示对应的列,y对应的行。
共16列,2行。
下标从0开始!
lcd1602.c
#include "config.h"
void LcdWaitReady()
{
u8 sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do{
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
}while(sta & 0x80);
}
void LcdWriteCmd(u8 cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(u8 dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdSetCursor(u8 x, u8 y)
{
u8 addr;
if(y == 0)
{
addr = 0x00 + x;
}
else
{
addr = 0x40 + x;
}
LcdWriteCmd(addr | 0x80);
}
//注意x是1602的列,y是1602的行 --- 1602共16列,2行
void LcdShowStr(u8 x, u8 y, u8 *str, u8 len)
{
LcdSetCursor(x, y);
while(len--)
{
LcdWriteDat(*str++);
}
}
void LcdAreaClear(u8 x, u8 y, u8 len)
{
LcdSetCursor(x, y);
while(len--)
{
LcdWriteDat();
}
}
void LcdFullClear()
{
LcdWriteCmd(0x01);
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
lcd1602.h
#ifndef LCD1602_H
#define LCD1602_H
void InitLcd1602();
void LcdShowStr(u8 x, u8 y, u8 *str, u8 len);
void LcdAreaClear(u8 x, u8 y, u8 len);
void LcdFullClear();
#endif
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』