问题描述
- 基于51单片机运用液晶屏显示字符
-
基于51单片机运用液晶屏显示字符时出了些问题,编译什么的都没问题,但是就是液晶屏没动静,液晶屏是1602,求助,急急急,跪谢 #includesbit lcd_en= P2^7;sbit lcd_rs = P2^6;sbit lcd_rw =P2^5;sbit lcd_data = P0;#define unchar unsigned char#define uint unsigned intunchar code table[]={"baby ChenDan's"};unchar code table1[]={"birthday 0311"};unchar code table2[]={"Happy Birthday"};unchar code table3[]={"be happiness"};unchar num;void delay_10us(uint n) // 10微秒的延时子程序{ uint i,j; for(i=n;i>0;i--) for(j=2;j>0;j--); } void writecmd(char com){ delay_10us(5); lcd_rs=0; lcd_rw=0; lcd_data=com; delay_10us(5); lcd_en=1; delay_10us(5); lcd_en=0;}void writedata(char lcddata){ delay_10us(5); lcd_rs=1; lcd_rw=0; lcd_data=lcddata; delay_10us(5); lcd_en=0; delay_10us(5); lcd_en=1;}void lcdinit(){ writecmd(0x38); writecmd(0x08); writecmd(0x01); writecmd(0x0c); writecmd(0x06);}void main(){ lcdinit(); while(1) { writecmd(0x80); for(num=0;num<15;num++) { writedata(table[num]); delay_10us(50); } writecmd(0x80+0x40); for(num=0;num<16;num++) { writedata(table1[num]); delay_10us(50); } delay_10us(100); writecmd(0x01); writecmd(0x80); for(num=0;num<14;num++) { writedata(table2[num]); delay_10us(50); } writecmd(0x80+0x40); for(num=0;num<16;num++) { writedata(table3[num]); delay_10us(50); } delay_10us(100); writecmd(0x01); }}
解决方案
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="I LIKE MCU!";
uchar code table1[]="WWW.TXMCU.COM";
sbit lcden=P3^4; // E是使能
sbit lcdrs=P3^5; // RS为数据与命令选择端
sbit dula=P2^6;
sbit wela=P2^7;
uchar num;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//命令函数
{
lcdrs=0;//RS为低电平时写指令
P0=com; //送指令
delay(5);
lcden=1;// 拉高E的电平
delay(5);
lcden=0;// 拉低E的电平
}
void write_data(uchar date) //写数据
{
lcdrs=1; //RS为1时写数据
P0=date;
delay(5);
lcden=1; //高脉冲取走指令或数据
delay(5);
lcden=0;
}
void init()
{
dula=0;//关闭数码管,因为USB供电较小,功耗低,不能为数码管与液晶同时供电,否侧造成液晶不亮
wela=0;
lcden=0;
write_com(0x38); //显示模式设置 ,指令码 00111000,功能:设置16*2显示,5*7点阵
write_com(0x0e);//显示开/关及光标设置 指令码:00001DCB和000001NS
write_com(0x06);//
write_com(0x01);//清屏
write_com(0x80+0x10);//write_com(0x80);的意思是将指针初始化 write_com(0x80+0x10);重新定义指针
}
void main()
{
init();
for(num=0;num<11;num++)
{
write_data(table[num]);
delay(20);
}
// write_com(1);
write_com(0x80+0x53); //write_com(0x80+0x40);可用来设置转行
for(num=0;num<13;num++)
{
write_data(table1[num]);//显示1用‘1'输入,即write_data(‘1') 。
delay(20);
}
for(num=0;num<16;num++) //循环移动到液晶显示屏上
{
write_com(0x1c); //整屏左移一位
delay(20);
}
while(1);//没此句,一直动态显示。
}
时间: 2024-11-03 19:32:20