STC15F2K60S2单片机LCD1602显示代码
时间: 2023-09-27 14:07:06 浏览: 282
下面是STC15F2K60S2单片机LCD1602显示的代码,您可以参考一下:
```
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS=P3^5;
sbit RW=P3^6;
sbit EN=P3^4;
void delay_ms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void Write_Command(uchar Command)
{
RS=0;
RW=0;
P0=Command;
EN=1;
_nop_();
_nop_();
EN=0;
}
void Write_Data(uchar Data)
{
RS=1;
RW=0;
P0=Data;
EN=1;
_nop_();
_nop_();
EN=0;
}
void Init_Lcd1602()
{
Write_Command(0x38);
delay_ms(5);
Write_Command(0x38);
delay_ms(5);
Write_Command(0x38);
delay_ms(5);
Write_Command(0x0c);
delay_ms(5);
Write_Command(0x01);
delay_ms(5);
Write_Command(0x06);
delay_ms(5);
}
void Display_Lcd1602(uchar x,uchar y,uchar *String)
{
uchar i;
if(x==0) Write_Command(0x80+y);
else Write_Command(0xc0+y);
for(i=0;String[i]!='\0';i++)
Write_Data(String[i]);
}
void main()
{
Init_Lcd1602();
Display_Lcd1602(0,0,"Hello,World!");
Display_Lcd1602(1,3,"Goodbye!");
while(1);
}
```
在此代码中,我们使用了P3.4、P3.5和P3.6三个引脚来控制LCD1602屏幕的显示。其中,RS接在P3.5上,RW接在P3.6上,EN接在P3.4上。
在主函数中,我们首先调用Init_Lcd1602()函数来初始化LCD1602屏幕。然后,我们使用Display_Lcd1602()函数来在屏幕上显示"Hello,World!"和"Goodbye!"两个字符串。
请注意,此代码中的delay_ms()函数是用来延时的,它的作用是等待一段时间,以确保LCD1602屏幕的操作被执行。在实际应用中,您可以根据需要调整延时的时间。
阅读全文
相关推荐















