|
我不知道哪里錯(cuò)了, 不能在LCD顯示數(shù)值 ,請(qǐng)各位大大幫忙 ,謝謝。
#include
#include
#include "51hei.h"
#define uchar unsigned char
#define uint unsigned int
#define Busy 0x80
#define DD P0
sbit Rs=P3^5;
sbit Rw=P2^7;
sbit E=P3^4;
#include
unsigned char code disp_code[]={
0x30, //0在1602中的十六進(jìn)制碼
0x31, //1在1602中的十六進(jìn)制碼
0x32, //2在1602中的十六進(jìn)制碼
0x33, //3在1602中的十六進(jìn)制碼
0x34, //4在1????602中的十六進(jìn)制碼
0x35, //5在1602中的十六進(jìn)制碼
0x36, //6在1602中的十六進(jìn)制碼
0x37, //7在1602中的十六進(jìn)制碼
0x38, //8在1602中的十六進(jìn)制碼
0x39, //9在1602中的十六進(jìn)制碼
0x41, //A在1602中的十六進(jìn)制碼
0x42, //B在1602中的十六進(jìn)制碼
0x43, //C在1602中的十六進(jìn)制碼
0x44, //D在1602中的十六進(jìn)制碼
0x2A, //*在1602中的十六進(jìn)制碼
0x23, //#在1602中的十六進(jìn)制碼
0x20, // 在1602中的十六進(jìn)制碼
};
uchar keyboard_Scan[]={0xef,0xdf,0xbf,0x7f};
uchar in;
/************************************************* ******************/
/*
*/
/* 延時(shí)子程序
*/
/*
*/
/************************************************* ******************/
void delay(int ms)
{
int i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void delay_1ms(unsigned char i) //最小延時(shí)1ms
{ unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void delay_10ns(unsigned char i) //最小延時(shí)10ns
{
unsigned char j;
while(i--)
for(j=0;j<10; j++);
}
unsigned char ReadDataLCM(void)
{
int i;
Rs = 1;
Rw = 1;
E = 1;
E = 0;
for(i=0;i<100;i++);
E = 1;
return(DD);
}
//讀狀態(tài)
unsigned char ReadStatusLCM(void)
{
int i;
DD= 0xFF;
Rs = 0;
Rw = 1;
E = 1;
E = 0;
for(i=0;i<100;i++);
E = 0;
while (DD & Busy); //檢測(cè)忙信號(hào)
return(DD);
}
void write_com(unsigned char com,p) //寫(xiě)指令
{
int i;
if(p)
if (P) ReadStatusLCM();
delay_10ns(5);
DD = com;
Rs = 0;
Rw = 0;
E = 1;
E = 0;
for(i=0;i<100;i++);
E = 0;
}
void write_date(unsigned char DATA) //寫(xiě)數(shù)據(jù)
{
int i;
ReadStatusLCM();
DD = DATA;
Rs= 1;
Rw= 0;
E = 1;
E = 0; //若晶振速度太高可以在這后加小的延時(shí)
for(i=0;i<100;i++);//延時(shí)
E= 0;
}
void Display_Char(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
X |= 0x80; //算出指令碼
write_com(X, 0); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
write_date(DData);
}
void lcd_init(void)
{
delay_1ms(15);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,1);
write_com(0x08,1);
write_com(0x01,1);
write_com(0x06,1);
write_com(0x0c,1);
}
/************************************************* *********
鍵盤(pán)掃描子函數(shù)
************************************************** ********/
//************************************************ * *********
uchar Keyboard()
{
uchar i,j,pc;
for(i=0;i<4;i++)
{
P3=keyboard_Scan;
pc=P3;
pc=pc<<4|0x0f; //假如說(shuō)有按鍵按下
for(j=0;j<4;j++) //將掃描值與掃描按鍵值比較
{
if(keyboard_Scan[j]==pc)in=i*4+j;
while(keyboard_Scan!=P3) //松手檢測(cè)
{
P3=keyboard_Scan;
}
}
}
return(in);
}
/************************************************* ******************/
/*
*/
/* 主程序
*/
/*
*/
/************************************************* ******************/
void main(void)
{ int i;
int count="0;
" int count1="0;
" int check="0;
" int check1="0;
" guandz();
guanled();
lcd_init(); // 初始化LCD
while(1)
{
Keyboard();
Display_Char(0,0, disp_code[in]);
for(i=0;i<30000;i++);
write_com(0x01,1); //清屏;
}
}
|
|