51单片机1602四线驱动问题,我自己写了个驱动加矩阵按键的程序,运行结果是LCD第一行显示的字符串地址紊 20
第二行显示是正常的,按键驱动正常,但是按键后的显示也有一点小问题。具体程序如下:#include<reg52.h>#include<intrins.h>sbitRS=P0...
第二行显示是正常的,按键驱动正常,但是按键后的显示也有一点小问题。具体程序如下:
#include <reg52.h>
#include <intrins.h>
sbit RS = P0^2 ;
sbit EN = P0^3 ;
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit key4=P2^3;
unsigned char code word1[]={"Welcome to"};
unsigned char code word2[]={"ZJGSU"};
unsigned char code word3[]={"please press"};
unsigned char code word4[]={"a key"};
unsigned char code word5[]={"No key"};
unsigned char code word6[]={"was pressed"};
unsigned char code ch[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l'};
sbit D_4=P0^4;
sbit D_5=P0^5;
sbit D_6=P0^6;
sbit D_7=P0^7;
void delay_400ms(void) //延时程序
{
unsigned char i = 5;
unsigned int j;
while(i--)
{
j=6000;
while(j--) {_nop_( );_nop_( ); }
}
}
void delay_1us(void) //1us延时函数
{ _nop_(); }
void delay_nus(unsigned int n) //N us延时函数
{ unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
/**
* 写数据
*/
void w_dat(unsigned char dat)
{
delay_nus(16);
RS = 1;
EN = 1;
P0&=0X0f;
P0|=dat&0xf0;
EN = 0;
dat=dat<<4;
EN = 1;
P0&=0x0f;
P0|=dat&0xf0;
EN = 0;
}
/**
* 写命令
*/
void w_cmd(unsigned char cmd)
{
delay_nus(16);
RS = 0;
EN = 1;
P0&=0X0f;
P0|=cmd&0xf0;
EN = 0;
cmd=cmd<<4;
EN = 1;
P0&=0x0f;
P0|=cmd&0xf0;
EN = 0;
}
/**
* 发送字符串到LCD
*/
void w_string(unsigned char addr_start, unsigned char *p)
{
unsigned char *pp;
pp = p;
w_cmd(addr_start);
while (*pp != '\0')
{
w_dat(*pp++);
}
}
void w_char(unsigned char addr_start, unsigned char p)
{
w_cmd(addr_start);
w_dat(p);
}
/**
* 初始化1602
*/
void Init_LCD1602(void)
{
w_cmd(0x01); // 清屏
w_cmd(0x28); // 16*2显示,5*7点阵,4位数据接口
w_cmd(0x0C); // 显示器开、光标开、光标允许闪烁
w_cmd(0x06); // 文字不动,光标自动右移
} 展开
#include <reg52.h>
#include <intrins.h>
sbit RS = P0^2 ;
sbit EN = P0^3 ;
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit key4=P2^3;
unsigned char code word1[]={"Welcome to"};
unsigned char code word2[]={"ZJGSU"};
unsigned char code word3[]={"please press"};
unsigned char code word4[]={"a key"};
unsigned char code word5[]={"No key"};
unsigned char code word6[]={"was pressed"};
unsigned char code ch[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l'};
sbit D_4=P0^4;
sbit D_5=P0^5;
sbit D_6=P0^6;
sbit D_7=P0^7;
void delay_400ms(void) //延时程序
{
unsigned char i = 5;
unsigned int j;
while(i--)
{
j=6000;
while(j--) {_nop_( );_nop_( ); }
}
}
void delay_1us(void) //1us延时函数
{ _nop_(); }
void delay_nus(unsigned int n) //N us延时函数
{ unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
/**
* 写数据
*/
void w_dat(unsigned char dat)
{
delay_nus(16);
RS = 1;
EN = 1;
P0&=0X0f;
P0|=dat&0xf0;
EN = 0;
dat=dat<<4;
EN = 1;
P0&=0x0f;
P0|=dat&0xf0;
EN = 0;
}
/**
* 写命令
*/
void w_cmd(unsigned char cmd)
{
delay_nus(16);
RS = 0;
EN = 1;
P0&=0X0f;
P0|=cmd&0xf0;
EN = 0;
cmd=cmd<<4;
EN = 1;
P0&=0x0f;
P0|=cmd&0xf0;
EN = 0;
}
/**
* 发送字符串到LCD
*/
void w_string(unsigned char addr_start, unsigned char *p)
{
unsigned char *pp;
pp = p;
w_cmd(addr_start);
while (*pp != '\0')
{
w_dat(*pp++);
}
}
void w_char(unsigned char addr_start, unsigned char p)
{
w_cmd(addr_start);
w_dat(p);
}
/**
* 初始化1602
*/
void Init_LCD1602(void)
{
w_cmd(0x01); // 清屏
w_cmd(0x28); // 16*2显示,5*7点阵,4位数据接口
w_cmd(0x0C); // 显示器开、光标开、光标允许闪烁
w_cmd(0x06); // 文字不动,光标自动右移
} 展开
1个回答
2013-07-30
展开全部
第二行正常,第一行应该跟时序有关系,仔细找找吧
本来想把你的软件模拟一下的,结果最后还是图片,不想输代码
本来想把你的软件模拟一下的,结果最后还是图片,不想输代码
更多追问追答
追问
主函数太长传不上去了。。。时序有问题为什么第二行会正常显示,只影响第一行?
追答
给你私信了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询