#include<reg52.h>
#include<intrins.h>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#define LCD_databus P0 //LCD12864的8位數(shù)據(jù)口
#define uint unsigned int
#define uchar unsigned char
int heat=000,spurs=000;
uint c=1;
sbit EN=P2^2; //低電平寫入高電平讀取
sbit RS=P2^0; //高電平數(shù)據(jù)輸入命令輸入
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 xiaoshu=0;
uchar f2;
uchar m2;
uchar x2;
uchar D,M;
uchar count;
char fen=0;
char miao=0;
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,//"主" 0
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,// "隊", 1
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,//“客” 2
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,//"一", 3
0x00,0x10,0x18,0x16,0x10,0x90,0xF0,0x9F,
0x90,0x90,0x92,0x94,0x10,0x18,0x10,0x00,
0x40,0x20,0x90,0x88,0x46,0x41,0x23,0x14,
0x08,0x14,0x22,0x21,0x40,0xC0,0x40,0x00,//"發(fā)" 4
0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"空白" 5
0xFF,0xF7,0xF7,0xF7,0xF7,0xF7,0xF6,0x01,
0xF7,0xF7,0xF7,0xF7,0xF7,0xF7,0xFF,0xFF,
0xBF,0xBF,0xBE,0xBE,0xBE,0xBE,0xBE,0x80,
0xBE,0xBE,0xBE,0xBE,0xBE,0xBF,0xBF,0xFF,//"主"反色 6
0xEF,0xF3,0x7B,0xBB,0xC3,0xAB,0x6A,0x69,
0x6B,0xAB,0xCB,0xEB,0xFB,0xEB,0xF3,0xFF,
0xFB,0xFB,0xFD,0xFD,0x02,0xBA,0xBB,0xBB,
0xBB,0xBA,0x02,0xFD,0xFD,0xFD,0xFD,0xFF,//“客”反色 7
0x08,0x08,0x08,0x08,0x88,0x78,0x0F,0x08,
0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x20,0x50,0x48,0x46,0x41,0x41,0x41,0x41,
0x7F,0x41,0x41,0x41,0x41,0x40,0x40,0x00,//"左" 8
0x08,0x08,0x08,0x08,0xC8,0x38,0x0F,0x08,
0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,
0x08,0x04,0x02,0x01,0xFF,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0xFF,0x00,0x00,0x00,//“右” 9
};
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);
} /**?延時函數(shù)?**/
void delay_1ms(uint x)
{
uint i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
void Read_busy()//讀“忙”函數(shù)-----數(shù)據(jù)線的最高位DB71則busy
{
P0=0x00;
RS=0;
RW=1;
EN=1;
while(P0 & 0x80);
EN=0;
}
void write_LCD_command(uchar value) //寫命令函數(shù)
{
Read_busy(); //對LCD的每次讀寫都要讀忙
RS=0; //選擇命令
RW=0; //讀操作
LCD_databus=value;
EN=1; //EN由1----0鎖存有效數(shù)據(jù)
_nop_();
_nop_();
EN=0;
}
void write_LCD_data(uchar value)//寫數(shù)據(jù)函數(shù)
{
Read_busy();
RS=1; //選擇數(shù)據(jù)
RW=0;
LCD_databus=value;
EN=1; //EN由1----0鎖存有效數(shù)據(jù)
_nop_();
_nop_();
EN=0;
}
void Set_page(uchar page) //設(shè)置“頁”LCD12864共8頁,一頁是8行點陣點
{
page=0xb8|page; //頁的首地址為0xB8
write_LCD_command(page);
}
void Set_line(uchar startline) //設(shè)置顯示的起始行
{
startline=0xC0|startline; //起始行地址為0xC0
write_LCD_command(startline); //設(shè)置從哪行開始:共0--63;一般從0 行開始顯示
}
void Set_column(uchar column) //設(shè)置顯示的列
{
column=column &0x3f; //列的最大值為64
column= 0x40|column; //列的首地址為0x40
write_LCD_command(column); //規(guī)定顯示的列的位置
}
void SetOnOff(uchar onoff) //顯示開關(guān)函數(shù):0x3E是關(guān)顯示,0x3F是開顯示
{
onoff=0x3e|onoff; //onoff:1---開顯示;0---關(guān)顯示
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) //清屏函數(shù)
{
uchar i,j;
SelectScreen(screen); //0--全屏;1---左半屏;2---右半屏
for(i=0;i<8;i++) //控制頁數(shù)0-7,共8頁
{
Set_page(i);
Set_column(0);
for(j=0;j<64;j++) //控制列數(shù)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) //屏幕,頁面,列(起始位置),二維數(shù)組,索引
{
uchar i;
SelectScreen(screen);
Set_page(page);
Set_column(column*8); //因為LCD屏幕上的每個字通常是8*8像素,所以需要乘以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列的數(shù)據(jù)輸出
{
write_LCD_data(*(a[h]+i)); //漢字的上半部分
}
Set_page(page+1); //寫下半頁:16*8
Set_column(column*16); //控制列
for(i=0;i<16;i++) //控制16列的數(shù)據(jù)輸出
{ write_LCD_data(*(a[h]+16+i)); //漢字的下半部分
}
}
void dingshiqi()
{
TMOD=0X01; //T1 jishu T0 jishi
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=0;
}
void Key1()//改變發(fā)球
{
if(key1==0)
{
delay_ms(10);
if(key1==0)
{
while(!key1) //
{
while(!key1)
{}
c=c^1;//異或算法,改變c從而改變主客場發(fā)球
}
}
}
}
void Key2() //持續(xù)檢查按鈕增加heat
{
if(key2==0)
{
delay_ms(10);
if(key2==0)
{
while(!key2)
{ while(!key2)
{ }
heat++;
}
}
}
}
void Key3() //持續(xù)檢查減少heat
{
if(key3==0)
{
delay_ms(10);
if(key3==0)
{
while(!key3)
{
while(!key3)
{}
if (heat==0)
{}
else
{
heat--;
D=heat;
}
}
}
}
}
void Key4() //增加spurs
{
if(key4==0)
{
delay_ms(10);
if(key4==0)
{
while(!key4)
{
while(!key4)
{ }
spurs++;
}
}
}
}
void Key5() //減少spurs
{
if(key5==0)
{
delay_ms(10);
if(key5==0)
{
while(!key5)
{
while(!key5){ }
if (spurs==0)
{}
else
{
spurs--;
M=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)
{}
fen=0;miao=0;xiaoshu=0;
}
}
}
}
void main(void)
{
uchar A,B,C,E,F;
uchar G,H,I,J;
A=1;
init_LCD();
dingshiqi();
D=heat;
M=spurs;
while(1)
{
Key1();
Key2();
Key3();
Key4();
Key5();
Key6();
Key7();
Key8();
B=heat/10%10;
C=heat%10;
E=spurs/10%10;
F=spurs%10;
G=fen/10;
H=fen%10;
I=miao/10;
J=miao%10;
if(heat>D) //發(fā)球
{
if(heat!=D)
{
if(heat%2==0)
{
Display_HZ(1,0,1,HZ,9);//"右"
Display_HZ(1,2,1,HZ,5);//"空白"
}
if(spurs%2==1)
{
Display_HZ(1,0,1,HZ,8);//"左"
Display_HZ(1,2,1,HZ,5);//"空白"
}
c=1;
D=heat;
M=spurs;
}
}
if(spurs>M)
{
if(spurs!=M)
{
if(spurs%2==0)
{
Display_HZ(1,0,1,HZ,5);//"空白"
Display_HZ(1,2,1,HZ,9);//"右"
}
if(spurs%2==1)
{
Display_HZ(1,0,1,HZ,5);//"空白"
Display_HZ(1,2,1,HZ,8);//"左"
}
c=0;
D=heat;
M=spurs;
}
}
if(c==1)
{
if(heat%2==0)
{
Display_HZ(1,0,1,HZ,9);//"右"
Display_HZ(1,2,1,HZ,5);//"空白"
}
if(heat%2==1)
{
Display_HZ(1,0,1,HZ,8);//"左"
Display_HZ(1,2,1,HZ,5);//"空白"
}
Display_HZ(1,0,0,HZ,6);//“主”反色
Display_HZ(1,2,0,HZ,2);//“客”
}
if(c==0)
{
if(spurs%2==0)
{
Display_HZ(1,0,1,HZ,5);//"空白"
Display_HZ(1,2,1,HZ,9);//"右"
}
if(spurs%2==1)
{
Display_HZ(1,0,1,HZ,5);//"空白"
Display_HZ(1,2,1,HZ,8);//"右"
}
Display_HZ(1,0,0,HZ,0);//“主”
Display_HZ(1,2,0,HZ,7);//“客”反色
}
if((heat==0)&&(spurs==0))
{
D=heat;
M=spurs;
}
if(A==1) //比分
{
Display_ASCII(1,0,4,time,B);
Display_ASCII(1,0,5,time,C);//bifen
Display_ASCII(1,2,4,time,E);
Display_ASCII(1,2,5,time,F);
if(((heat>=21)&&(heat-spurs>1))||((spurs>=21)&&(spurs-heat>1))||((spurs==29)&&(heat==30))||((heat==29)&&(spurs==30)))
{
A=2;
heat=0;spurs=0;
B=0;C=0;E=0;F=0;
}
}
if(A==2)
{
Display_ASCII(1,0,7,time,B);
Display_ASCII(2,0,0,time,C);//bifen
Display_ASCII(1,2,7,time,E);
Display_ASCII(2,2,0,time,F);
if(((heat>=21)&&(heat-spurs>1))||((spurs>=21)&&(spurs-heat>1))||((spurs==29)&&(heat==30))||((heat==29)&&(spurs==30)))
{
A=3;
heat=0;spurs=0;
B=0;C=0;E=0;F=0;
}
}
if(A==3)
{
Display_ASCII(2,0,2,time,B);
Display_ASCII(2,0,3,time,C);//bifen
Display_ASCII(2,2,2,time,E);
Display_ASCII(2,2,3,time,F);
if(((heat>=21)&&(heat-spurs>1))||((spurs>=21)&&(spurs-heat>1))||((spurs==29)&&(heat==30))||((heat==29)&&(spurs==30)))
{
A=4;
heat=0;spurs=0;
B=0;C=0;E=0;F=0;
}
} //三局分?jǐn)?shù)展示,追分機(jī)制 。
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);
}
}
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==10)
{ xiaoshu=1; miao++; } //計時時間小數(shù)
if(miao==10)
{
miao=0;
fen++;
} //分鐘
}
}
以上是源代碼 |