#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define out P3
sbit RS =P1^1;
sbit E =P1^3; uchar key=0;
sbit RW = P1^2;
bit anjian=1; sbit anj=P1^0;
sbit b0=P2^0;
sbit b1=P2^1;
sbit b2=P2^2;
sbit b3=P2^3;
sbit b4=P2^4;
sbit b5=P2^5;
sbit b6=P2^6;
sbit b7=P2^7;
sbit led=P1^4;
sbit buzzer=P1^7;
uchar people[8];
char count=0;
char sum=0;
void delay(uint j){
//1ms延时函数
uchar i =250;
for(;j>0;j--)
while(--i);
i=249;
while(--i);
i=250;}
void check()
{
anjian=anj;
if(b0==0){people[0]=1;}
if(b1==0){people[1]=1;}
if(b2==0){people[2]=1;}
if(b3==0){people[3]=1;}
if(b4==0){people[4]=1;}
if(b5==0){people[5]=1;}
if(b6==0){people[6]=1;}
if(b7==0){people[7]=1;}
}
void check_busy(void) //检查忙标志函数
{
uchar dt;
do{
dt =0xff;
E=0;
RS=0;
RW=1;
E=1;
dt=out;}
while(dt&0x80);
E=0;
}
void write_command(uchar com)//写命令函数
{
check_busy();
E=0;
RS=0;
RW=0;
out=com;
E=1;
nop_();
E=0;
delay(1);
}
void write_data(uchar dat)//写显示数据函数
{
check_busy();
E=0;
RS=1;
RW=0;
out=dat;
E=1;
nop_();
E=0;
delay(1);
}
void string(uchar ad,uchar *s)//输出显示字符串的函数
{
write_command(ad);
while(*s>0)
write_data(*s++); //输出字符串,且指针增1
delay(100);
}
void string1(uchar ad,uchar ascall)//输出显示字符串的函数
{
write_command(ad);
write_data(ascall);//输出字符串,且指针增1
delay(100);
}
void Show()
{
string(0x80,"vote");
string(0x88,"waiver");
string(0x8f,"s");
string1(0xC1,48+sum);
string1(0xCA,48);
string1(0xCe,(count)/10+48);
string1(0xCf,(count)%10+48);
}
void ledo(void)
{
led=0;
buzzer=1;
}
void make(char *sum)
{
char i;
Show();
string1(0xCA,56-*sum);
key=2;
ledo();
while(1)
{
if(anj==0)
{
buzzer=0;
led=1;
break;
}
}
*sum=0;
for(i=0;i<8;i++)
{
people[i]=0;
}
Show();
}
void lcd_initial (void){
write_command(0x38); //写人命令0x38:8位两行显示,5×7点阵 //字符
write_command(0x0C); //写入命令0x0C:开整体显示,光标关,无 //闪烁
write_command(0x06); //写入命令0x06:光标右移
write_command(0x01); //写入命令0x01:清屏
delay(1);}
void DelayMS(uint ms)
{
uchar i;
while(ms--) for( i=0;i<120;i++);
}
void main(){
uchar i;
uint t=300;
buzzer=0;
P2=0xff;anj=1;
lcd_initial();
while(1){
anjian=anj;
if(anjian==0){DelayMS(100);
if(anjian==0){key=1;count=10;}}
if(key==1){
check();
DelayMS(300);
check();
DelayMS(100);
check();
DelayMS(100);//规定时间到达
count=count-1;
sum=0;
for( i=0;i<8;i++){
sum=sum +people[i];
}
Show();
if(count==0)make(&sum);
}
}
}