程序:
#include<reg52.h>
#include<stdio.h>
#define KEY_PORT P2
#define uint unsigned int
#define uchar unsigned char
#define LCD_DATA P0 //LCD的数据口
sbit LCD_BUSY=LCD_DATA^7; //LCD忙信号位
sbit LCD_RS=P3^0; //LCD寄存器选择
sbit LCD_RW=P3^1; //LCD读写控制
sbit LCD_EN=P3^5; //LCD使能信号
void LCD_check_busy(void) //检测LCD状态,看它是不是还在忙呢
{
while(1)
{
LCD_EN=0;
LCD_RS=0; //指令寄存器通信
LCD_RW=1; //read data
LCD_DATA=0xff;
LCD_EN=1;
if(!LCD_BUSY)break;
}
LCD_EN=0;
}
void LCD_cls(void) //LCD清屏
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0;
LCD_DATA=1;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_instruction(unsigned char LCD_instruction) //写指令到LCD
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0; //写数据
LCD_DATA=LCD_instruction;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_data(unsigned char LCD_data) //输出一个字节数据到LCD
{
LCD_check_busy();
LCD_RS=1;
LCD_RW=0;
LCD_DATA=LCD_data;
LCD_EN=1;
LCD_EN=0;
}
void LCD_set_position(unsigned char x) //LCD光标定位到x处
{
LCD_write_instruction(0x80+x);
}
void LCD_printc(unsigned char lcd_data) //输出一个字符到LCD
{
LCD_write_data(lcd_data);
}
void LCD_prints(unsigned char *lcd_string) //输出一个字符串到LCD
{
unsigned char i=0;
while(lcd_string!=0x00)
{
LCD_write_data(lcd_string);
i++;
}
}
void LCD_initial(void) //初始化LCD
{
LCD_write_instruction(0x3c);
LCD_write_instruction(0x0c);
LCD_write_instruction(0x06);//显示屏一定要不移动。
LCD_cls();
}
uchar key_scan(void)
{
uchar keyPressed=0;
KEY_PORT=0xE0;
keyPressed=KEY_PORT&0xE0;
if(keyPressed==0xE0)
return 0;
else
return 1;
}
uchar key_assert(void)
{
uchar keyvalue;
KEY_PORT=0xEF;
if((keyvalue=KEY_PORT&0x0F)!=0x0F)
{
while((KEY_PORT&0x0F)!=0x0F);
return (keyvalue|0xE0);
}
KEY_PORT=0XDF;
if((keyvalue=KEY_PORT&0x0F)!=0x0F)
{
while((KEY_PORT&0x0F)!=0x0F);
return (keyvalue|0xD0);
}
KEY_PORT=0xBF;
if((keyvalue=KEY_PORT&0x0F)!=0x0F)
{
while((KEY_PORT&0x0F)!=0x0F);
return (keyvalue|0xB0);
}
KEY_PORT=0x7F;
if((keyvalue=KEY_PORT&0x0F)!=0x0F)
{
while((KEY_PORT&0x0F)!=0x0F);
return (keyvalue|0x70);
}
}
void delay(uint N)
{
uint x,y;
for(x=N;x>0;x--);
for(y=110;y>0;y--);
}
void main(void)
{
uchar key_value=0;
LCD_initial();
//LCD_set_position(0);
while(1);
{
if(key_scan())
{
delay(20);
if(key_scan())
key_value=key_assert();
LCD_set_position(0);
P0=key_value;//输出到P0
switch(key_value)
{
case 0xEE:
printf("1");
break;
case 0xED:
printf("2");
break;
case 0xEB:
printf("3");
break;
case 0xE7:
printf("KEY 4 PRESSED!\n");
break;
case 0xDE:
printf("KEY 5 PRESSED!\n");
break;
case 0xDD:
printf("KEY 6 PRESSED!\n");
break;
case 0xDB:
printf("KEY 7 PRESSED!\n");
break;
case 0xD7:
printf("KEY 8 PRESSED!\n");
break;
case 0xBE:
printf("KEY 9 PRESSED!\n");
break;
case 0xBD:
printf("KEY 10 PRESSED!\n");
break;
case 0xBB:
printf("KEY 11 PRESSED!\n");
break;
case 0xB7:
printf("KEY 12 PRESSED!\n");
break;
case 0x7E:
printf("KEY 13 PRESSED!\n");
break;
case 0x7D:
printf("KEY 14 PRESSED!\n");
break;
case 0x7B:
printf("KEY 15 PRESSED!\n");
break;
case 0x77:
printf("KEY 16 PRESSED!\n");
break;
}
}
}
}
请问以上的程序有什么地方不对,为什么就是不能在液晶上显示出来呢? |