写一个lcd1602的51 程序
时间: 2023-05-23 14:03:34 浏览: 103
基于51单片机的1602液晶显示程序
以下是一个简单的LCD1602 51程序,希望能够帮到您:
#include <reg52.h>
#include <intrins.h>
#define LCD1602_DATAPORT P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
void delay(unsigned int xms)
{
unsigned int i, j;
for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--);
}
void writeLCD1602_CMD(unsigned char cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DATAPORT = cmd;
LCD1602_E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD1602_E = 0;
}
void writeLCD1602_DAT(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPORT = dat;
LCD1602_E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD1602_E = 0;
}
void initLCD1602()
{
writeLCD1602_CMD(0x38);
delay(5);
writeLCD1602_CMD(0x38);
delay(5);
writeLCD1602_CMD(0x38);
delay(5);
writeLCD1602_CMD(0x0c);
delay(5);
writeLCD1602_CMD(0x06);
delay(5);
writeLCD1602_CMD(0x01);
delay(5);
}
void displayLCD1602(unsigned char x,unsigned char y,unsigned char *str)
{
unsigned char addr;
if (y == 0)
{
addr = 0x80 + x;
writeLCD1602_CMD(addr);
}
else if (y == 1)
{
addr = 0xC0 + x;
writeLCD1602_CMD(addr);
}
while (*str != '\0')
{
writeLCD1602_DAT(*str);
str++;
}
}
void main()
{
initLCD1602();
displayLCD1602(0,0,"Welcome to CSDN!");
displayLCD1602(0,1,"Have a nice day!");
while(1);
}
阅读全文