标题: 求助单片机程序解释,尽可能详细点儿, [打印本页]
作者: 89c51123 时间: 2015-6-4 07:27
标题: 求助单片机程序解释,尽可能详细点儿,
#include<REG52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
/************************变量定义************************************/
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit sw3=P1^2;
sbit sw4=P1^3;
sbit sw5=P1^4;
sbit sw6=P1^5;
sbit sw7=P1^6;
sbit sw8=P1^7;
sbit start_button=P3^0;
sbit props_button=P3^1;
sbit snatch_button=P3^2;
sbit answer_button=P3^3;
sbit push_button=P3^4;
sbit reduce_button=P3^5;
sbit stop_button=P3^6;
sbit BEEP=P3^7;
uint flag=0;
uint snatch_flag=1;
uint answer_flag=0;
uint a,b,c,d,x;
ucharstate[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x71,0x76,0x00,0x79,0x54,0x5e};/**数据依次表示数字0--9和字母F、H,以及 End。**/
/************************函数声明***********************************/
void initUart(); //初始化函数
void time(uint ucMs); //延时函数
void Play(); //蜂鸣器发声函数
void Start(); //开始函数
void Stop(); //终止函数
void Props(); //道具函数
void Snatch(); //抢答置数函数
void Answer(); //回答置数函数
void Push(); //时间加1S函数
void Reduce(); //时间减1S函数
void DisPlay(); //显示器显示函数
void Keyscan(); //选手按钮扫描函数
/**************************主函数**********************************/
void main()
{
x=30;
initUart();
while(1)
{
if(!flag)
{
Keyscan();
}
if(a!=10&&!flag)
{
for(;a!=12;)
{
Play();
}
}
if(!start_button&&a==10)
{
flag=1;
Start();
}
if(!snatch_button)
{
flag=0;
Snatch();
}
if(!answer_button)
{
flag=1;
Answer();
}
if(!push_button)
{
Push();
}
if(!reduce_button)
{
Reduce();
}
DisPlay();
}
}
/**************************初始化程序**********************************/
void initUart()
{
a=10,b=10,c=10,d=10;
BEEP=0;
P2=0xee;
P0=state[10];
time(1);
P2=0xed;
P0=state[10];
time(1);
P2=0xeb;
P0=state[10];
time(1);
P2=0xe7;
P0=state[10];
time(1);
}
/**************************延时程序**********************************/
/******************delay_5us*********************/
void delay_5us(void) //延时5us
{
_nop_();
_nop_();
}
/******************delay_50us********************/
void delay_50us(void) //延时50us
{
unsigned char i;
for(i=0;i<4;i++)
{
delay_5us();
}
}
/******************延时100us********************/
void delay_100us(void) //延时100us
{
delay_50us();
delay_50us();
}
/****************延时单位:ms******************/
voidtime(unsigned int ucMs) //延时1ms
{
unsigned char j;
while(ucMs>0)
{
for(j=0;j<10;j++)
delay_100us();
ucMs--;
}
}
/********************蜂鸣器发声程序**************************/
void Play()
{
uchar i;
for(i=1;i<100;i++)
{
BEEP=~BEEP;
DisPlay();
if(!stop_button)
{
Stop();
}
if(!props_button&&snatch_flag==0&&answer_flag==1)
{
Props();
}
elseif(snatch_flag==1&&answer_flag==0)
{
Keyscan();
}
}
for(i=1;i<150;i++)
{
DisPlay();
if(!stop_button)
{
Stop();
}
if(!props_button&&snatch_flag==0&&answer_flag==1)
{
Props();
}
elseif(snatch_flag==1&&answer_flag==0)
{
Keyscan();
}
}
BEEP=0;
}
/**********************抢答开始程序*****************************/
void Start()
{
uint i,j,y=x;
for(i=0;i<=y&&a==10;i++)
{
c=x/10;
d=x%10;
x--;
if(x<5)
{
Play();
}
else
{
for(j=1;j<250&&a==10;j++)
{
DisPlay();
if(!stop_button)
{
Stop();
}
if(!props_button&&snatch_flag==0&&answer_flag==1)
{
Props();
}
elseif(snatch_flag==1&&answer_flag==0)
{
Keyscan();
}
}
}
}
}
/********************************终止程序************************************/
void Stop()
{
a=12;
b=13;
c=14;
d=15;
flag=1;
}
/***************道具使用程序(回答时可以重置倒计时时间)**********************/
void Props()
{
x=x+16;
c=x/10;
d=x%10;
while(!props_button)
{
DisPlay();
}
}
/**********************抢答置数程序***************************************/
void Snatch()
{
x=30;
a=10;
b=10;
c=3;
d=0;
snatch_flag=1;
answer_flag=0;
}
/**********************回答置数程序***************************************/
void Answer()
{
x=60;
a=10;
b=10;
c=6;
d=0;
snatch_flag=0;
answer_flag=1;
}
/**********************时间加1S程序***************************************/
void Push()
{
uint y;
x++;
y=x;
c=y/10;
d=y%10;
if(x==99)
{
x=0;
}
while(!push_button)
{
DisPlay();
}
}
/**********************时间减1S程序***************************************/
void Reduce()
{
x=x-1;
time(1);
c=x/10;
d=x%10;
if(x==-1)
{
x=99;
}
while(!reduce_button)
{
DisPlay();
}
}
/**********************按键扫描程序***************************************/
void Keyscan()
{
if(sw1==0)
{
a=1;
b=11;
}
if(sw2==0)
{
a=2;
b=11;
}
if(sw3==0)
{
a=3;
b=11;
}
if(sw4==0)
{
a=4;
b=11;
}
if(sw5==0)
{
a=5;
b=11;
}
if(sw6==0)
{
a=6;
b=11;
}
if(sw7==0)
{
a=7;
b=11;
}
if(sw8==0)
{
a=8;
b=11;
}
}
/*************************显示器显示程序************************************/
void DisPlay()
{
P2=0xee;
P0=state[a];
time(1);
P2=0xed;
P0=state;
time(1);
P2=0xeb;
P0=state[c];
time(1);
P2=0xe7;
P0=state[d];
time(1);
}
作者: 欢腾智能科技 时间: 2015-6-6 17:49
哪里看不懂的可以提出来; 看起来像给人下任务一样!
作者: 89c51123 时间: 2015-6-7 17:25
主要是都不太懂,所以想找人帮个大忙,好让答辩能够过了。。。初始化程序里a=10,b=10,c=10,d=10有什么用?
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |