2024-3-20 18:08 上傳
點擊文件名下載附件
液晶顯示不理想,但編譯是通過了的。也不知道哪里出錯了。附源代碼
#include<reg52.h>
#include<intrins.h>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#define LCD_databus P0 //LCD12864的8位數據口
#define uint unsigned int
#define uchar unsigned char
uint heat=000,spurs=000;
uint c;
sbit EN=P2^2; //低電平寫入高電平讀取
sbit RS=P2^0; //高電平數據輸入命令輸入
sbit RW=P2^1; //高電平讀取低電平寫入
sbit CS2=P2^3; //
sbit CS1=P2^4;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit key5=P1^4;
sbit key6=P1^5;
sbit key7=P1^6;
sbit key8=P1^7;
sbit BEEP=P3^0;
char chang=48;
char xiaoshu=0;
char xiaoshu2=0;
uchar f2;
uchar m2;
uchar x2;
uchar count;
char fen=12;
char miao=0;
char flag=24;
unsigned char code time[][16]={
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//"0",
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//"1",
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//"2",
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//"3",
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//"4",
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//"5",
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//"6",
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//"7",
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//"8",
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//"9",
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//"V",10
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//"S",11
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//":",12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//".",13
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//"T"14
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//"N"15
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//"D"16
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//"R"17
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//"H"18
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"空格"19
};
uchar code HZ[][32]={
0x00,0x08,0x08,0x08,0x08,0x08,0x09,0xFE,
0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x40,0x40,0x41,0x41,0x41,0x41,0x41,0x7F,
0x41,0x41,0x41,0x41,0x41,0x40,0x40,0x00,//"主"
0x00,0xFE,0x02,0x22,0xDA,0x06,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x08,0x10,0x88,0x47,0x20,0x18,
0x07,0x00,0x07,0x18,0x20,0x40,0x80,0x00,// "隊",
0x10,0x0C,0x84,0x44,0x3C,0x54,0x95,0x96,
0x94,0x54,0x34,0x14,0x04,0x14,0x0C,0x00,
0x04,0x04,0x02,0x02,0xFD,0x45,0x44,0x44,
0x44,0x45,0xFD,0x02,0x02,0x06,0x02,0x00,//“客”
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"一",
};
void delay_10us(uint n) //延時n個10us@12M晶振
{
uint i;
for(i=n;i>0;i--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
void delay_50us(uint z)
{
uint x,y; for(x=z;x>0;x--)
for(y=19;y>0;y--);
}
void delay_ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay(uint i)
{
while(--i);
} /**?延時函數?**/
void delay_1ms(uint x)
{
uint i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
void Read_busy()//讀“忙”函數-----數據線的最高位DB71則busy
{
P0=0x00;
RS=0;
RW=1;
EN=1;
while(P0 & 0x80);
EN=0;
}
void write_LCD_command(uchar value) //寫命令函數
{
Read_busy(); //對LCD的每次讀寫都要讀忙
RS=0; //選擇命令
RW=0; //讀操作
LCD_databus=value;
EN=1; //EN由1----0鎖存有效數據
_nop_();
_nop_();
EN=0;
}
void write_LCD_data(uchar value)//寫數據函數
{
Read_busy();
RS=1; //選擇數據
RW=0;
LCD_databus=value;
EN=1; //EN由1----0鎖存有效數據
_nop_();
_nop_();
EN=0;
}
void Set_page(uchar page) //設置“頁”LCD12864共8頁,一頁是8行點陣點
{
page=0xb8|page; //頁的首地址為0xB8
write_LCD_command(page);
}
void Set_line(uchar startline) //設置顯示的起始行
{
startline=0xC0|startline; //起始行地址為0xC0
write_LCD_command(startline); //設置從哪行開始:共0--63;一般從0 行開始顯示
}
void Set_column(uchar column) //設置顯示的列
{
column=column &0x3f; //列的最大值為64
column= 0x40|column; //列的首地址為0x40
write_LCD_command(column); //規定顯示的列的位置
}
void SetOnOff(uchar onoff) //顯示開關函數:0x3E是關顯示,0x3F是開顯示
{
onoff=0x3e|onoff; //onoff:1---開顯示;0---關顯示
write_LCD_command(onoff);
}
void SelectScreen(uchar screen) //選擇屏幕
{
switch(screen)
{
case 0: CS1=0;CS2=0;break; //全屏
case 1: CS1=0;CS2=1;break;//左半屏
case 2: CS1=1;CS2=0;break;//右半屏
default:break;
}
}
void ClearScreen(uchar screen) //清屏函數
{
uchar i,j;
SelectScreen(screen); //0--全屏;1---左半屏;2---右半屏
for(i=0;i<8;i++) //控制頁數0-7,共8頁
{
Set_page(i);
Set_column(0);
for(j=0;j<64;j++) //控制列數0-63,共64列
{
write_LCD_data(0x00); //寫入0,地址指針自加1
}
}
}
void init_LCD() //LCD的初始化
{
SetOnOff(1); //開顯示
SelectScreen(0);//
ClearScreen(0); //清屏
Set_line(0); //開始行:0
}
void Display_ASCII(uchar screen,uchar page,uchar column,uchar a[][16],uchar h)
{
uchar i;
SelectScreen(screen);
Set_page(page);
Set_column(column*8);
for(i=0;i<8;i++) //顯示字符上半部分:8*8
{
write_LCD_data(*(a[h]+i));
}
Set_page(page+1); //顯示字符下半部分:8*8
Set_column(column*8);
for(i=0;i<8;i++)
{
write_LCD_data
(*(a[h]+i+8));
}
}
void Display_HZ(uchar screen,uchar page,uchar column,uchar a[][32],uchar h)
{
uchar i;
SelectScreen(screen);
Set_page(page); //寫上半頁:16*8
Set_column(column*16); //控制列
for(i=0;i<16;i++) //控制16列的數據輸出
{
write_LCD_data(*(a[h]+i)); //漢字的上半部分
}
Set_page(page+1); //寫下半頁:16*8
Set_column(column*16); //控制列
for(i=0;i<16;i++) //控制16列的數據輸出
{ write_LCD_data(*(a[h]+16+i)); //漢字的下半部分
}
}
void disp1()
{
Display_HZ(1,0,2,HZ,0);//主
Display_HZ(1,0,3,HZ,1);//隊
Display_ASCII(2,0,0,time,10);
Display_ASCII(2,0,1,time,11);
Display_HZ(2,0,1,HZ,3);//客
Display_HZ(2,0,2,HZ,2);//隊
}
void disp2() //
{
Display_HZ(1,0,2,HZ,3);// 客
Display_HZ(1,0,3,HZ,4);// 隊
Display_ASCII(2,0,0,time,10);
Display_ASCII(2,0,1,time,11);
Display_HZ(2,0,1,HZ,0);//主
Display_HZ(2,0,2,HZ,1);//隊
}
void disp3()
{
if(chang>36)
{
while(1)
{
Display_ASCII(1,4,0,time,1);
Display_ASCII(1,4,1,time,11);
Display_ASCII(1,4,2,time,14);
}
}
else if(chang>24)
{
while(1)
{
Display_ASCII(1,4,0,time,2);
Display_ASCII(1,4,1,time,15);
Display_ASCII(1,4,2,time,16);
}
}
else if(chang>12)
{
while(1)
{
Display_ASCII(1,4,0,time,3);
Display_ASCII(1,4,1,time,17);
Display_ASCII(1,4,2,time,16);
}
}
else if(chang>0)
{ while(1)
{
Display_ASCII(1,4,0,time,4);
Display_ASCII(1,4,1,time,14);
Display_ASCII(1,4,2,time,18);
}
}
}
void disp4()
{
chang=24;
fen=12;
miao=0;
xiaoshu=0;
xiaoshu2=0;
flag=0;
Display_ASCII(1,4,0,time,3);
Display_ASCII(1,4,1,time,17);
Display_ASCII(1,4,2,time,16);
}
void disp5()
{
if(fen==0&&miao==0&&xiaoshu==0)
{
BEEP=0;
delay_1ms(5000);
fen=12;
miao=0;
xiaoshu=0;
TR0=0;
BEEP=1;
}
if(flag==0&&xiaoshu2==0)
{
BEEP=0;
f2=fen;
m2=miao;
x2=xiaoshu;
delay_1ms(5000);
TR0=0;
fen=f2;
miao=m2;
xiaoshu=x2;
flag=24;
xiaoshu2=0;
BEEP=1;
}
}
void dingshiqi()
{
TMOD=0X01; //T1 jishu T0 jishi
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=0;
}
void Key1()
{
if(key1==0)
{
delay_ms(10);
if(key1==0)
{
while(!key1)
{
while(!key1)
{}
disp2();
disp4();
c=spurs;
spurs=heat;
heat=c;
}
}
}
}
void Key2()
{
if(key2==0)
{
delay_ms(10);
if(key2==0)
{
while(!key2)
{ while(!key2)
{ }
heat++;
}
}
}
}
void Key3()
{
if(key3==0)
{
delay_ms(10);
if(key3==0)
{
while(!key3)
{
while(!key3)
{}
if (heat==0)
{}
else
{
heat--;
}
}
}
}
}
void Key4()
{
if(key4==0)
{
delay_ms(10);
if(key4==0)
{
while(!key4)
{
while(!key4)
{ }
spurs++;
}
}
}
}
void Key5()
{
if(key5==0)
{
delay_ms(10);
if(key5==0)
{
while(!key5)
{
while(!key5){ }
if (spurs==0)
{}
else
{spurs--;}
}
}
}
}
void Key6()
{
if(key6==0)
{
delay_ms(10);
if(key6==0)
{
while(!key6)
{
while(!key6)
{ }
TR0=0;
}
}
}
}
void Key7()
{
if(key7==0)
{
delay_ms(10);
if(key7==0)
{
while(!key7)
{
while(!key7)
{}
TR0=1;
}
}
}
}
void Key8()
{
if(key8==0)
{
delay_ms(10);
if(key8==0)
{
while(!key8)
{
while(!key8)
{ }
flag=24;
xiaoshu2=0;
}
}
}
}
void main(void)
{
uchar A,B,C,D,E,F;
uchar G,H,I,J,K,L;
init_LCD();
dingshiqi();
disp1();
disp3();
while(1)
{
disp5();
Key1();
Key2();
Key3();
Key4();
Key5();
Key6();
Key7();
Key8();
A=heat/100;
B=heat/10%10;
C=heat%10;
D=spurs/100;
E=spurs/10%10;
F=spurs%10;
G=fen/10;
H=fen%10;
I=miao/10;
J=miao%10;
K=flag/10;
L=flag%10;
Display_ASCII(1,2,5,time,A);
Display_ASCII(1,2,6,time,B);
Display_ASCII(1,2,7,time,C);//bifen
Display_ASCII(2,2,1,time,12);
Display_ASCII(2,2,3,time,D);
Display_ASCII(2,2,4,time,E);
Display_ASCII(2,2,5,time,F);
Display_ASCII(1,4,4,time,G);
Display_ASCII(1,4,5,time,H);
Display_ASCII(1,4,6,time,12);
Display_ASCII(1,4,7,time,I);
Display_ASCII(2,4,0,time,J);
Display_ASCII(2,4,1,time,13);
Display_ASCII(2,4,2,time,xiaoshu);
Display_ASCII(2,4,4,time,K);
Display_ASCII(2,4,5,time,L);
Display_ASCII(2,4,6,time,13);
Display_ASCII(2,4,7,time,xiaoshu2);
}
}
void timer0() interrupt 1 //3定時器1的中斷號1定時器0的中斷號0外部中斷1 2外部中 斷2 4串口中斷
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==2)
{
count=0;
xiaoshu--;
if(xiaoshu==-1)
{ xiaoshu=9; miao--; } //計時時間小數
if(miao==-1)
{
miao=59;
fen--;
chang--;
if(fen==-1)
{
fen=12;
}
} //分鐘跳動1st 2nd 3rd 4th
xiaoshu2--;
if(xiaoshu2==-1)
{
xiaoshu2=9;
flag--;
} //24秒時間小數
if(flag==-1)
{
flag=24;
xiaoshu2=0;
} //24秒重置
}
}
不排除proteus設置出錯,但我也不清楚是哪里出錯了
一般這種情況應該怎么處理?