查看: 1804|回复: 11
|
【原创】Reaction Time Tester ,PIC16F84A
[复制链接]
|
|
本帖最后由 电子达人 于 19-8-2010 04:53 PM 编辑
UPDATE 2010/8/19 -发现忘记加上电路中的hardware debouncing capacitor C1 ,现已加上去。
最近觉得很闷,我很想把我DIY的东西分享出来,好让大家欣赏!
这次,我在此分享的是一个很简单的MCU 制作,那就是----Reaction Time Tester ...
这个电路,顾名思义,就是测量人体对stimulus反应的速度。需要两个人来玩。
电路里有两个开关: INT0/RB0 那边的作为start counting 和 stop counting 的switch(SW1)是给想要被测量的人拿的;RA4那边的作为trigger (SW2),是给"Gamemaster"拿的.
如何玩:1. 电源接上时,所有的LED全部一起闪。这是idle mode。
2.(如测量Player A的反应时间:)player A 需要按下Sw1, 使所有的LED都同时亮起。这时他要专注于LED。
3. Player B 需要按下sw2(最好让player A 不知道几时按), 这时候所有的LED都会灭掉,同时开始计时,并显示在LED上。(第一个LED亮代表50ms,第二个100ms,以此类推。次序是:RB7->RB1,RA3->RA1)
4. Player A 看到LED灭后,要马上按下SW1 来停止计时。所显示的时间即是反应时间。
5. 再次按下SW1以进入idle mode。
![](http://img806.imageshack.us/img806/540/img1p.jpg)
以下是PIC16F84A 的源码(HI-TECH C):- #include "htc.h"
- __CONFIG(WDTDIS&RC&PWRTEN&UNPROTECT);
- #define bitset(var,bitno)((var)|=1UL<<(bitno))
- #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
- #define testbit_on(data,bitno) ((data>>bitno)&0x01)
- #define _XTAL_FREQ 80000
- bit flashing=0,started=0,incre=0,testing=0,stop=0,qui=0,blink=0,debnc=0;
- void init(void)
- {
- TRISA=0B11110001;
- TRISB=0B00000001;
- OPTION=0B01010100; //SET RBPU,CLOCK SELECT,PRESCALER=64 for flashing LEDs
- INTCON=0B00010000; //only int0 is enabled
- PORTB=0B0;
- PORTA=0B0;
- flashing=0;started=0;incre=0;testing=0;stop=0;qui=0;blink=0;
- }
- void interrupt isr_a(void)
- {
- if(T0IF)
- {
- if(flashing)
- {
- TMR0=0X63; //for flashing all leds at 2hz
- PORTA=~PORTA;
- PORTB=~PORTB;
- }
- if(testing)
- {
- TMR0=5;
- incre=1;
- }
-
- T0IF=0;
- }
- if(INTF)
- {
- if(stop)
- qui=1;
- if(testing)
- {
- stop=1;
- }
- else
- {
- started=1;
- }
-
- INTF=0;
- }
- }
- void incdisplay(void)
- {
- if(!started)
- {
- if(!(testbit_on(PORTA,1))&&!(blink))
- {
- if((PORTB-1))
- {
- if(testbit_on(PORTB,1))
- {
- PORTB=0;
- bitset(PORTA,3);
- }
- else
- PORTB>>=1;
- }
- else
- {
- PORTA>>=1;
- }
- }
- else
- {
- if(!blink) OPTION+=2; // THIS IS TO SLOW THE BLINKING DOWN, OPTION WILL BE INCREMENTED ONLY ONCE AFTER THE FIRST ENTRANCE OF THIS PART
- if(testbit_on(PORTA,1)) bitclr(PORTA,1);
- else bitset(PORTA,1);
- blink=1;
-
- }
- }
- else
- {
- bitset(PORTB,7);
- started=0;
- }
- incre=0;
- }
- void main(void)
- {
- init();
- T0IE=1;
- TMR0=0X63;
- ei();
- flashing=1;
- checkstart:
- if(!started)goto checkstart;
- waiting: //all the LEDs light for a period of time, then turns off, then the reaction time measurement will be started
- GIE=0;
- flashing=0;
- //waiting=1;
- PORTA=0XFF;
- PORTB=0XFF;
- // __delay_ms(23500); //automatic delay about 3sec
- while(testbit_on(PORTA,4)) ; //manual delay, triggered by a switch at RA4
- TRISA=0B11100000;
- PORTA=0B0;PORTB=0B0;
- testrespond:
- //waiting=0
- bitclr(OPTION,2); //SETUP THE TIMER+PRESCALER
- bitclr(OPTION,1) ; //PRESCALER = 1:8 "010"
- bitset(OPTION,0); //for the reaction time with 0.1s scale, use tmr0=5 as initial value& prescaler 1:8
- TMR0=5 ; //for 0.05 second scales,use TMR0=5,PRESCALER= 1:4 (001) DONT FORGET TO CHANGE IT IN THE INTERRUPT
- INTCON&=0B11111001; //CLEAR ALL INTERRUPT FLAGS EXCEPT RBIF
- GIE=1;
- started=1;testing=1 ;
- while(1)
- {
- if(incre)
- incdisplay();
- if(qui)
- {
- GIE=0;
- __delay_ms(2000);
- INTCON&=0B11111001;
- T0IE=0;
- GIE=1;
- break;
- }
- if(stop)
- {
- if(T0IE)
- {
- T0IE=0;
- GIE=0;
- __delay_ms(2000);
- T0IE=0;
- INTE=1;
- GIE=1;
- }
- }
- }
- }
复制代码 |
评分
-
查看全部评分
|
|
|
|
|
|
|
发表于 17-8-2010 07:42 AM
|
显示全部楼层
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 17-8-2010 03:42 PM
|
显示全部楼层
哈哈,其实,我做得不是很好看。。。PCB+VEROBOD ,有点丑... |
|
|
|
|
|
|
|
发表于 17-8-2010 05:37 PM
|
显示全部楼层
哈哈,其实,我做得不是很好看。。。PCB+VEROBOD ,有点丑...
电子达人 发表于 17-8-2010 03:42 PM ![](http://cforum3.cari.com.my/images/common/back.gif)
为什么用16F84A?哪里买的? |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 17-8-2010 06:26 PM
|
显示全部楼层
因为那时候我不会上farnell 买。。。只好到kg. benggali (槟城北海)去买。价钱还可以。。。RM9.80 ... |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 17-8-2010 06:39 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 17-8-2010 06:44 PM
|
显示全部楼层
本帖最后由 pic 于 17-8-2010 06:45 PM 编辑
再接再厉哦。。![](static/image/smiley/default/wink.gif) |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 17-8-2010 07:30 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 18-8-2010 04:24 PM
|
显示全部楼层
哦,原来楼主也是北海人啊。加油加油 ![](static/image/smiley/default/loveliness.gif) |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 18-8-2010 08:06 PM
|
显示全部楼层
回复 9# waiweng83
是呀,但准确一点,是北赖。 |
|
|
|
|
|
|
|
发表于 20-8-2010 03:53 PM
|
显示全部楼层
请问楼主kg. benggali那一间有卖pic chip。有什么model的。
楼主,加油加油 ![](static/image/smiley/default/smile.gif) |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 20-8-2010 04:47 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|