高手速进!!关于液晶LCD1602 4总线模式乱码
#include <reg51.h>
#include <intrins.h>
/********************************************************************/
#define LCDIO P1
sbit LCD1602_RS=P1^6;
sbit LCD1602_RW=P1^5;
sbit LCD1602_EN=P1^4;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
/********************************************************************/
void LCD_delay(void);
void LCD_en_command(unsigned char command);
void LCD_en_dat(unsigned char temp);
void LCD_set_xy( unsigned char x, unsigned char y );
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_init(void);
/********************************************************************/
void delay_nms(unsigned int n);
/********************************************************************/
unsigned char keyscan()
{
unsigned char key;
P1=P1|0x0f;
while( (key1==1) && (key2==1) && (key3==1) && (key4==1) )
P1=P1|0x0f;
if(key1==0)
key=1;
else if(key2==0)
key=2;
else if(key3==0)
key=3;
else
key=4;
P1=P1|0x0f;
while( (key1==0) || (key2==0) || (key3==0) || (key4==0) );
return key;
}
void main(void)
{
unsigned char key;
LCD_init();
LCD_en_command(0x01);
while(1)
{
//LCD_en_command(0x01);
delay_nms(2);
key=keyscan();
key=key+'0';
LCD_write_char( 0,0,key);
//LCD_init();
//Delay400ms();
//LCD_init();
//LCD_write_string(0,0,"starting...... ");
//Delay400ms();
//LCD_write_string(0,1,"press a key ");
//LCD_write_string(10,0,"10sec");
/*
LCD_write_string(0,0," create by ");
LCD_write_string(0,1," fenyman ");
delay_nms(200);
LCD_en_command(0x01);
delay_nms(2);
LCD_write_string(0,0," LCD1602 ");
LCD_write_string(0,1," test ");
delay_nms(200);
*/
}
}
/******************** LCD PART *************************************/
void LCD_delay(void)
{
unsigned char i;
for(i=40;i>0;i--)
;
}
/********************************************************************/
void LCD_en_command(unsigned char command)
{
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_EN=0;
LCDIO |=(command & 0xf0) >> 4 ;
LCDIO &=( (command & 0xf0) >> 4 ) | 0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO |= (command & 0x0f);
LCDIO &= (command & 0x0f) | 0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_EN=0;
LCDIO |=(dat & 0xf0) >> 4 ;
LCDIO &=( (dat & 0xf0) >> 4 ) |0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO |=(dat & 0x0f);
LCDIO &=(dat & 0x0f) | 0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y ==0)
address = 0x80 + x;
else
address = 0xC0 + x;
LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //set address
while (*s) // write character
{
//LCDIO=*s; //太恶心了!!!!这句话!!!
LCD_en_dat(*s);
s ++;
}
}
/********************************************************************/
void LCD_init(void)
{
/*
delay_nms(5);
LCD_en_command(0x38);
delay_nms(5);
LCD_en_command(0x38);
delay_nms(5);
LCD_en_command(0x38);
delay_nms(5);
*/
P1=0xff;
LCD_en_command(0x01);
delay_nms(5);
LCD_en_command(0x01);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x0C);
delay_nms(5);
LCD_en_command(0x80);
delay_nms(5);
LCD_en_command(0x01);
delay_nms(5);
}
/********************************* *********************************/
void delay_nms(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<1140;j++);
}
/********************************************************************/
为什么程序第一次运行显示乱码?要复位一次以后在运行就可以显示了?
问题点数:75、回复次数:5Top
1 楼rei1984(Only C)回复于 2006-11-30 18:10:29 得分 0
高手们快点啊Top
2 楼foxdeng(江洋大刀)回复于 2006-11-30 20:49:24 得分 20
明天聊聊吧,估计是在某个位置延时不够。qq 285170438Top
3 楼icesnows(逝去日子)回复于 2006-12-01 16:27:26 得分 20
上电复位时间是不是不够?或者某个内存地址初始化不对Top
4 楼ldqmoon(ldqmoon)回复于 2006-12-04 22:42:16 得分 35
你的这个是4位接法?
感觉时序上很乱.延时不是最关键的.建议看看这个ic的时序图
你用0x38, 0x0f, 0x01三个来初始化屏幕
ps:有些书上对1602的时序介绍是错的哦~~~这个是要看ic的Top
5 楼rei1984(Only C)回复于 2006-12-10 09:52:52 得分 0
还是不对,
老古网上的4线 fenyman 写的也有问题 ,就是需要复位一下才行,谁知道 fenyman的联系方法?Top





