单片机原理与接口技术人体反应速度测试仪设计Word格式.docx
《单片机原理与接口技术人体反应速度测试仪设计Word格式.docx》由会员分享,可在线阅读,更多相关《单片机原理与接口技术人体反应速度测试仪设计Word格式.docx(14页珍藏版)》请在冰豆网上搜索。
此时可以松手,记灯熄灭到松手的时间差为反应时间。
代码如下:
矩阵键盘模块:
#include"
project18s.h"
#include<
intrins.h>
ucharkey_value=0x10;
uchartemp,a;
ucharkeyboard()
{
uchari,t=4;
P2=0xFF;
//P2口写1
a=0x7F;
//p_value赋初始值
i=1;
while(t--)
{
a=_crol_(a,1);
//循环左移一位,检测下一行
key_scan();
i++;
if(i==5){i=1;
a=0x7f;
}
}
returnkey_value;
}
voidkey_scan()
P2=a;
//P2.0置零
temp=P2;
//读P2口的值
temp=temp&
0xF0;
//取高四位,都得用temp,即temp=p2,temp=temp&
0xf0;
while(temp!
=0xF0)//如果高四位不全为0,则有键下
{
delay(5);
//延时消抖
while(temp!
=0xF0)//再读一次,如果高四位还是不全为0,真的有键按下了
{
temp=P2;
//读P2口的值给temp
switch(temp)
{
case0xEE:
key_value=0;
break;
case0xDE:
key_value=1;
break;
case0xBE:
key_value=2;
case0x7E:
key_value=3;
case0xED:
key_value=4;
case0xDD:
key_value=5;
case0xBD:
key_value=6;
case0x7D:
key_value=7;
case0xEB:
key_value=8;
case0xDB:
key_value=9;
case0xBB:
key_value=10;
case0x7B:
key_value=11;
case0xE7:
key_value=12;
case0xD7:
key_value=13;
case0xB7:
key_value=14;
case0x77:
key_value=15;
}
while(temp!
=0xf0)//松手检测
{
temp=P2;
temp=temp&
}
seed+=times;
key_hit=1;
times--;
}
}
voiddelay(uintz)
{
uintm,n;
for(m=z;
m>
0;
m--)
for(n=78;
n>
n--);
数码管显示模块:
sbitseg_oe=P1^3;
//数码管显示使能端,低电平有效
sbitduanxuan=P1^4;
//数码管的段选端
sbitweixuan=P1^5;
//数码管的位选端
ucharcodesmg_duan[17]={0x3F,0x06,0x5B,0x4F,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76};
//数码管段选码表,我们的数码管是共阴极数码管
ucharcodesmg_wei[8]={0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe};
//选择亮哪一个数码管
voidseg_disp(ucharwei,ucharduan)//单个数码管的显示
weixuan=1;
P0=smg_wei[wei];
weixuan=0;
duanxuan=1;
P0=smg_duan[duan];
duanxuan=0;
seg_oe=0;
delay10ms();
seg_oe=1;
voiddelay10ms()
uchari,j;
for(i=1;
i<
40;
i++)
for(j=1;
j<
j--);
头文件:
#ifndefPro_H
#definePro_H
reg52.h>
#defineucharunsignedchar
#defineuintunsignedint
#definegame_times2//游戏次数
externucharkey_hit;
externintseed;
externinttimes;
voidseg_disp(uchar,uchar);
voiddelay10ms();
ucharkeyboard();
voidkey_scan();
voiddelay(uint);
#endif
主程序代码:
/**项目18单片机人体反应速度测试仪的设计
***包含模块:
矩阵键盘,数码管,定时器*/
stdlib.h>
stdio.h>
ucharkey_hit=0;
intseed=12;
inttimes=game_times;
uchardisp[5];
intmstime=0;
voiddisplay(uintms)//显示反应时间
uchari;
disp[3]=ms/1000;
disp[2]=(ms%1000)/100;
disp[1]=(ms%100)/10;
disp[0]=ms%10;
for(i=0;
4;
{
seg_disp(i,disp[i]);
unsignedlongrandom(uchark)//生成随机数
uintrt;
srand(k);
rt=rand();
rt=rt*rand();
returnrt;
intaverage(inttime[])//求反应时间的平均值
inti,sum=0;
game_times-1;
sum+=time[i];
return(sum/game_times);
voidmain()
inttime[game_times]=0;
uchardisp_num,hit_num;
uchardisp_ms;
TMOD=0x11;
TH0=0xfc;
TL0=0x17;
TR0=1;
ET0=1;
EA=1;
while
(1)
while(times)
while
(1)
disp_num=random(seed)%16;
hit_num=keyboard();
seg_disp(5,hit_num);
seg_disp(7,disp_num);
if(!
key_hit)
if(TF0==1)
{
TR0=1;
TF0=0;
if(key_hit)
{
TR0=0;
key_hit=0;
disp_ms=mstime;
break;
}
}
if(disp_num==hit_num)
display(disp_ms);
time[times]=disp_ms;
else
display(9999);
time[times]=9999;
mstime=0;
TR0=1;
}
seg_disp(5,hit_num);
seg_disp(7,disp_num);
display(average(time));
voidInt_T0(void)interrupt1
++mstime;
//(65535-1000)/256=252=0xfc定时时间1ms
//(65535-1000)%256=23=0x17
书上的代码经调试可用,如下:
project18.h"
sbitkey=P2^0;
sbitled=P1^0;
typedefunsignedcharbyte;
typedefunsignedintword;
staticbytedisp[5];
codebytetable[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
bytebot(void)
if(key==0)
return0;
else
return1;
voiddisplay(wordms)
unsignedlongrandom(void)
wordrt;
bytek=0;
srand(50000);
voidINIT_TMR1(void)
TH1=0xfc;
TL1=0x66;
TR1=1;
voiddelay1ms(void)
INIT_TMR1();
if(TF1==1)
break;
voidmain(void)
k=bot();
P2=0xff;
wordmstime=0,j;
wordr;
while(bot());
led=0;
r=random();
for(j=r;
j>
--j)
delay1ms();
k=bot();
if(k==1)
mstime=9999;
gotoloop;
led=1;
INIT_TMR1();
while
(1)
if(TF1==1)
TH1=0xfc;
TL1=0x18;
TR1=1;
TF1=0;
++mstime;
if(k=bot())
break;
loop:
led=1;
k=bot();
display(mstime);
mstime=0;
P2=0xff;
头文件代码:
数码管模块:
ucharcodesmg_duan[16]={0x3F,0x06,0x5B,0x4F,
0x39,0x5e,0x79,0x71};
voidseg_disp(ucharwei,ucharduan)
8;
{
10;
for(j=10;