#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit e = P2^1;
sbit rs = P2^0; /*液晶引脚 */
sbit sp1 = P3; /* 键盘扫描按键 */
sbit sp2 = P1^6; /*主持人开关 */
sbit spk = P1^7;
uchar tabe[] = " welcom your ";
uchar tabe1[] = " 00 "; /* 开始显示 */
uchar tabe2[] = "dao ji shiji "; /*14 倒计时 */
uchar tabe3[] = " error ";
uchar tabe4[] = "wu renqian da"; /* 开始答题 */
uchar tabe5[] = " hui da shij "; /*回答时间 */
uchar tabe6[] = " wu ren hui da"; /*无人回答 */
uchar tabe7[] = " end ";
uchar num1,temp;
uchar c;
uchar y;
uchar count,falg;
uchar ss,gg; //定时器 计数
uchar miao,feng,shi; //显示数字
delay1ms(uchar n)
{
uchar i,j;
while(n --)
{
for(i = 1; i > 0 ; i --)
for(j = 248; j > 0 ; j --);
}
}
// 液晶设置
write_com(uchar cm)
{
rs = 0;
P0 = cm;
delay1ms(1);
e = 1;
delay1ms(1);
e = 0;
}
write_dat(uchar da)
{
rs = 1;
P0 = da;
delay1ms(1);
e = 1;
delay1ms(1);
e = 0;
}
add_dat(uchar ad,uchar shuji)
{
uchar ge;
ge = shuji%10;
write_com(0x80 + 0x40+ ad);
write_dat(0x30 + ge);
}
/* 液晶设置 */
init() /*初始化设置 */
{
uchar x;
e = 0;
write_com(0x38);
write_com(0x0e);
write_com(0x06);
delay1ms(1);
write_com(0x01);
delay1ms(10);
for(x = 0; x < 15 ; x ++)
{
write_dat(tabe[x]);
}
write_com(0x80 + 0x43);
for(x = 0; x < 6 ; x ++)
{
write_dat(tabe1[x]);
}
}
init1() /*定时器 初始化 */
{
TMOD = 0x11;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
ET1 = 1;
EA = 1;
ET0 = 1;
gg = 0;
ss = 0 ;
count = 0;
miao= 5;
feng = 9;
shi = 8;
}
uint keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay1ms(1);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num1=1; break;
case 0xde:num1=2; break;
case 0xbe:num1=3; break;
case 0x7e:num1=4; break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay();
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num1=5; break;
case 0xdd:num1=6; break;
case 0xbd:num1=7; break;
case 0x7d:num1=8; break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return num1;
}
dispaly()
{
c = keyscan();
while(c)
{
TR0 = 0;
TR1 = 0;
write_com(0x01);
delay1ms(1);
write_com(0x80 + 0x10); // 抢答错误
for(y = 0; y < 10;y++)
{
write_dat(tabe3[y]);
}
write_com(0x80 + 0x45);
delay1ms(1);
write_dat(0x30 + c);
}
}
dispaly1()
{
c = keyscan();
while(c)
{
delay1ms(10);
write_com(0x01);
delay1ms(1);
write_com(0x80 + 0x10);
for(y = 0; y < 9;y++)
{
write_dat(tabe5[y]);
}
write_com(0x80 + 0x45);
delay1ms(1);
write_dat(0x30 + c);
delay1ms(10);
TR0 = 1;
falg =2;
TR1 = 0;
c = 0;
}
}
speak()
{
uchar i;
for(i = 200; i > 0; i --)
{
spk = ~spk;
delay1ms(5);
}
}
main()
{
init();
init1();
delay1ms(10);
write_com(0x81);
delay1ms(5);
spk = 0;
while(1)
{
if(sp2 == 0)
{
delay1ms(5);
if(sp2 == 0)
{
count ++;
switch(count)
{
case 1: falg = 1;TR1 = 0;TR0 =1; break;
case 2: TR0 = 0;TR1=0;falg = 0; break;
case 3: init();init1(); break;
}
while(sp2 == 0);
do{delay1ms(5);}
while(sp2 == 0);
}
}
}
}
void to() interrupt 1
{
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256; // 50ms
ss++;
if(falg == 1)
{
dispaly();
if(ss == 50)
{ ss= 0;
miao --;
if(miao == 0) // 调至第二步
{
TR1 = 1;
TR0 = 0;
}
write_com(0x80 + 0x01);
for(y = 0 ; y <13 ;y ++) // 显示倒计时 8 秒
{
write_dat(tabe2[y]);
delay1ms(1);
}
add_dat(6,miao);
speak();
}
}
if(falg == 2)
{
if(ss == 50)
{
ss = 0;
shi --;
write_com(0x80 + 0x01);
for(y = 0 ; y <13 ;y ++) // 显示倒计时 8 秒
{
write_dat(tabe5[y]);
delay1ms(1);
}
add_dat(6,shi);
if(shi == 0)
{
write_com(0x80 + 0x01);
for(y = 0 ; y <9 ;y ++) // 结束
{
write_dat(tabe7[y]);
delay1ms(1);
}
TR0 = 0;
TR1 = 0;
falg = 0;
}
}
}
}
void t1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
gg++;
dispaly1();
if(gg == 50)
{
gg= 0;
feng --;
if(feng == 0) // 无人抢答回答
{
TR1 = 0;
TR0 = 0;
write_com(0x80 + 0x01);
for(y = 0 ; y <15 ;y ++)
{
write_dat(tabe7[y]);
delay1ms(1);
}
add_dat(6,feng);
}
write_com(0x80 + 0x01);
for(y = 0 ; y <15 ;y ++) // 显示抢答时时间 8 秒
{
write_dat(tabe4[y]);
delay();
}
add_dat(6,feng);
}
} |