/**********************************************
LCD1602 八路抢答器
制作组 : 电子设计王者组
**********************************************/
#include<reg51.h>
#include<lcd1602.h>
sbit time_add=P2^3;
sbit time_dec=P2^2;
sbit START=P2^1;
sbit RESET=P2^0;
sbit beep=P2^4;
unsigned char num[]={'0','1','2','3','4','5','6','7','8','9'},num2;
unsigned char model,time,temp;
void init() //初始化函数
{
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
ET0=1;
TR0=0;
EA=1;
P2=0x0f;
model=0;
time=30;
}
void delay(unsigned char i) //延时
{
unsigned char j,k;
while(i--)
for(j=100;j>0;j--)
for(k=100;k>0;k--);
}
void star() //启动
{
lcd_xml(0x80);lcd_xsj(" welcome ");
delay(100);
lcd_xml(0xc0);lcd_xsj("===>> ");
delay(30);
lcd_xml(0xc0);lcd_xsj("=======>> ");
delay(30);
lcd_xml(0xc0);lcd_xsj("=========>> ");
delay(30);
lcd_xml(0xc0);lcd_xsj("===========>> ");
delay(30);
lcd_xml(0xc0);lcd_xsj("==============>>");
delay(30);
lcd_xml(0xc0);lcd_xsj(" Good Luck! ");
lcd_xml(0x80);lcd_xsj(" 8 Way Responder");
delay(80);
}
void led_num() //刷新倒计时
{
lcd_xml(0x80);lcd_xsj(" Countdown : ");
lcd_xml(0x8d);lcd_xsj2(num[time/10]);
lcd_xml(0x8e);lcd_xsj2(num[time%10]);
}
void reset() //复位
{
TR0=0;
P2=0x0f;
P3=0x00;
model=0;
time=30;
led_num();
lcd_xml(0xc0);lcd_xsj(" Not started ");
}
void Not_started()
{
if(time_add==0)
{
delay(10);
if(time_add==0)
{
time++;
if(time==100)
time=0;
led_num();
}
}
if(time_dec==0)
{
delay(10);
if(time_dec==0)
{
time--;
if(time==0)
time=99;
led_num();
}
}
if(START==0)
{
delay(10);
if(START==0)
{
model=1;
lcd_xml(0xc0);
lcd_xsj(" Are you ready! ");
delay(80);
TR0=1;
lcd_xml(0xc0);
lcd_xsj(" Start Responder");
beep=1;delay(20);beep=0;
}
}
}
void anjian()
{
if(P1 != 0xff)
{
delay(5);
if(P1 != 0xff)
{
temp=~P1;
model=3;
beep=1;delay(10);beep=0;
switch(temp)
{
case 0x01:num2=1;break;
case 0x02:num2=2;break;
case 0x04:num2=3;break;
case 0x08:num2=4;break;
case 0x10:num2=5;break;
case 0x20:num2=6;break;
case 0x40:num2=7;break;
case 0x80:num2=8;break;
}
lcd_xml(0xc0);lcd_xsj(" Competitor: ");
lcd_xml(0xce);lcd_xsj2(num[num2]);
lcd_xml(0x80);lcd_xsj("Ans successfully");
time=60;
delay(80);
P3=temp;
}
}
}
void finish()
{
if(!time)
{
lcd_xml(0xc0);
lcd_xsj(" Time Up! ");
}
}
void main()
{
init();
lcd_init();
star();
reset();
while(1)
{
if(RESET==0) //复位键检测
{
delay(1);
if(RESET==0)
reset();
}
switch(model)
{
case 0:Not_started();break;
case 1:anjian();break;
case 2:finish();break;
case 3:break;
default : model=0;break;
}
}
}
void timee() interrupt 1 //1s函数中断
{
unsigned char a;
TH0=0x3c;
TL0=0xb0;
a++; //是否满1s
if(a==24)
{
a=0;
if(model==1 || model==3)
{
time--;
led_num();
if(time==0)
{
model=2;
beep=1;delay(20);beep=0;
}
}
}
}
#define lcd_db P0 //数据口定义
sbit lcd_rs=P2^6;
sbit lcd_rw=P2^5;
sbit lcd_e=P2^7;
//延时函数
void lcd_delay()
{
unsigned char i;
for(i=0;i<100;i++);
}
//写数据串函数
void lcd_xsj(unsigned char dat[])
{
unsigned char a;
for(a=0;a<16;a++)
{
lcd_rs=1;
lcd_rw=0;
lcd_db=dat[a];
lcd_e=0;lcd_delay();
lcd_e=1;lcd_delay();
lcd_e=0;lcd_delay();
}
}
//写数据函数
void lcd_xsj2(unsigned char dat)
{
lcd_rs=1;
lcd_rw=0;
lcd_db=dat;
lcd_e=0;lcd_delay();
lcd_e=1;lcd_delay();
lcd_e=0;lcd_delay();
}
//写命令函数
void lcd_xml(unsigned char ml)
{
lcd_rs=0;
lcd_rw=0;
lcd_db=ml;
lcd_e=0;lcd_delay();
lcd_e=1;lcd_delay();
lcd_e=0;lcd_delay();
}
//初始化函数
void lcd_init()
{
lcd_xml(0x38);
lcd_xml(0x0c);
lcd_xml(0x01);
lcd_xml(0x06);
}
|