程序如下:
#include<reg52.h>
#include<intrins.h> //包含_nop_()函数定义的头文件
/*-----------------------------------------------------------
LED定义
------------------------------------------------------------*/
#define Data_P0 P0
sbit K11=P1^0;
sbit K21=P1^1;
sbit K31=P1^2;
sbit K41=P1^3;
sbit K51=P1^4;
sbit K61=P1^5;
sbit K71=P1^6;
sbit LED_Red=P2^5;
sbit LED_Green=P2^4;
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;
sbit KEY_Start=P3^0;
sbit KEY_Reset=P3^1;
sbit Buzzer=P3^7;
unsigned char Agree_Flag=0,Opinion_Flag=0;
bit ONE=0,TWO=0,THREE=0,FOUR=0,FIVE=0,SIX=0,SEVEN=0;
unsigned char NUM=0;
unsigned char sec=11,j=0,oppoin=0;
unsigned char code Table[]={0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0xbf};
void DelayMs(unsigned int x)
{
unsigned char i,j ;
while(x--)
{
for(i=22;i>0;i--)
for(j=10;j>0;j--);
}
}
void DisPlay(unsigned char Temp)
{
if(TR0)
{
LED2=0;
LED1=0;
LED4=0;
LED3=1;
Data_P0=Table[(Temp/10)];
DelayMs(2);
Data_P0=0Xff;
LED2=0;
LED1=0;
LED4=1;
LED3=0;
Data_P0=Table[(Temp%10)];
DelayMs(2);
Data_P0=0Xff;
}
else
{
LED2=1;
LED1=1;
LED3=1;
LED4=1;
}
}
void DisPLAY2(unsigned char Temp,unsigned char t)
{
LED1=1;
LED2=0;
LED3=0;
LED4=0;
Data_P0=Table[10];
DelayMs(2);
Data_P0=0Xff;
LED1=0;
LED2=1;
LED3=0;
LED4=0;
Data_P0=Table[(t%10)];
DelayMs(2);
Data_P0=0Xff;
LED1=0;
LED2=0;
LED3=1;
LED4=0;
Data_P0=Table[(Temp/10)];
DelayMs(2);
Data_P0=0Xff;
LED1=0;
LED2=0;
LED3=0;
LED4=1;
Data_P0=Table[(Temp%10)];
DelayMs(2);
Data_P0=0Xff;
}
void Init(void)
{
Agree_Flag=0;
Opinion_Flag=0;
ONE=0;
TWO=0;
THREE=0;
FOUR=0;
FIVE=0;
NUM=0;
SIX=0;
SEVEN=0;
LED_Green=1;
LED_Red=1;
}
void Timer_Init(void)
{
TMOD=0X01;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
EA=1;
ET0=1;
TR0=0;
}
void KEY_Scan(void)
{
if(ONE==0)
{
if(K11==0)
{
DelayMs(10);
if(K11==0)
{
Agree_Flag++;
ONE=1;
NUM++;
}
while(K11==0);
}
}
if(TWO==0)
{
if(K21==0)
{
DelayMs(10);
if(K21==0)
{
Agree_Flag++;
TWO=1;
NUM++;
}
while(K21==0);
}
}
if(THREE==0)
{
if(K31==0)
{
DelayMs(10);
if(K31==0)
{
Agree_Flag++;
THREE=1;
NUM++;
}
while(K31==0);
}
}
if(FOUR==0)
{
if(K41==0)
{
DelayMs(10);
if(K41==0)
{
Agree_Flag++;
FOUR=1;
NUM++;
}
while(K41==0);
}
}
if(FIVE==0)
{
if(K51==0)
{
DelayMs(10);
if(K51==0)
{
Agree_Flag++;
FIVE=1;
NUM++;
}
while(K51==0);
}
}
if(SIX==0)
{
if(K61==0)
{
DelayMs(10);
if(K61==0)
{
Agree_Flag++;
SIX=1;
NUM++;
}
while(K61==0);
}
}
if(SEVEN==0)
{
if(K71==0)
{
DelayMs(10);
if(K71==0)
{
Agree_Flag++;
SEVEN=1;
NUM++;
}
while(K71==0);
}
}
Opinion_Flag=7-Agree_Flag;
}
void KEY_Start_Reset()
{
if(KEY_Start==0)
{
DelayMs(5);
if(KEY_Start==0)
{
TR0=1;
Buzzer=0;
DelayMs(150);
Buzzer=1;
}
while(KEY_Start==0);
}
if(KEY_Reset==0)
{
DelayMs(5);
if(KEY_Reset==0)
{
TR0=0;
sec=11;
Init();
Buzzer=1;
}
while(KEY_Reset==0);
}
}
void Deal_With(unsigned char x,unsigned char y)
{
if(x>y)
{
LED_Green=0;
Buzzer=0;
oppoin=1;
}
else if(y>x)
{
LED_Red=0;
Buzzer=0;
oppoin=2;
}
}
/*-----------------------------------------------------------
主函数
------------------------------------------------------------*/
void main(void)
{
Init();
DelayMs(10);
sec=11;
Timer_Init();
Buzzer=1;
while(1)
{
while((sec!=11)&&(sec!=0))
{
KEY_Scan();
DisPlay(sec);
}
while(sec==0)
{
Deal_With(Agree_Flag,Opinion_Flag);
DisPLAY2(Agree_Flag,oppoin);
KEY_Start_Reset();
}
while(sec==11)
{
LED1=0;
LED2=0;
LED3=0;
LED4=0;
KEY_Start_Reset();
}
}
}
void Timer0() interrupt 1
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(++j==50)
{
j=0;
if(--sec==0xff)
{
sec=0;
}
}
}
/*-----------------------------------------------------------
----END----
------------------------------------------------------------*/
求大佬们拯救。
|