1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

reaction timer - multiplex inputs

Discussion in 'Oshonsoft' started by cybersky, May 30, 2011.

  1. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    Hi all

    I am trying to create a reaction timer using the 16f874.

    what i am thinking of coding -
    Code (text):

    dim rtim byte                 'keep track of clock cycle w.r.t instruction timing?
    dim rtimes(10) as byte     'an array to store 10 timings
    getbutton:
    if portb.1 = 1 then
      rtim = 2                      'using the step simuation it shows if using 2cycles
      rtimes(0) = rtim + 2      
    end if
    rtim = rtim + 2
    goto getbutton

     
    So i will "count" the clock cycles using the rtim then store that in the array and send it to pc via serial. then my pc prog can multiply that by .05us to get actual reaction time.
    I have simplified the code to show my idea - so i understand the code is incomplete.
    The question being will this wok?

    thanks
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    Using a 4MHz crystal the internal PIC cycle time would be 1MHz [ 1 uSec], BUT you have a number of cycles in the short increment loop, each one taking 1uSec so you would not be counting clock cycles.
    How accurate a time interval and how long is the measuring period going to be.???
     
  3. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    Hi
    Basically we will have "different" response lengths. i have a board with 36 leds and 36 push buttons (no).
    the user will send a string to the pic representing the led to turn on. with the string we will send the desired "on delay" value.
    so i have to chk if the desired time has passed or not - if the time is not up i will save the time into the array at the same index as the led "frame" index.
    if the time is expired i will write max value.
    then after all frames have played i will send to PC.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK

    hi,
    How accurate a time interval and how long is the measuring period going to be.???
    example 1uSec over 10 seconds say.??

    Post your full program.
     
  6. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    normal human reaction at its fastest is arounf 100 - 120ms, so we do not really have to be as precise as uSecs ? But we do want a fair amount of accuracy. Hope i am making sense.
     
    Last edited: May 30, 2011
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Use a 1mS interrupt and a 16 bit variable and you will get mS accuracy and up to a 1 minute interval. It is very easy to setup timer2 to generate a 1mS interrupt or to just set a bit every mS if interrupts aren't desirable.

    Mike.
     
    Last edited: May 30, 2011
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Assume a 100mSec base start, then test at 1mSec intervals for 32 periods, giving 132mSec max

    Please post your full program.
     
  9. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    100mS base is ok but atleast 200-300ms to respond?
    I have not used the timer functions yet - only been using oshonsoft and 16f84's but would that work if i have 36 buttons (6x6multiplex) ?
    The problem i have has to be solved in oshonsoft because i can only code in basic.
    Would it be a problem to count the cycles for timing?
     
    Last edited by a moderator: May 30, 2011
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Please post your full Basic program and we can work through what you have already done.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    Eric! why don't we push him into a timer... Thats really the only way to do this..

    ie.. Light LED, start timer.... button press, read timer... calculate response .... if timer timeouts ... then fail..
     
  12. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    With a 4MHz crystal you only have to setup two registers to get a 1mS signal. Set T2CON = 5 and PR2 = 249 and then PIR1.TMR2IF will get set every 1mS. It really is that easy to use a hardware timer. In your code you can do something like,
    Code (text):

        if PIR1.TMR2IF = 1 then
            PIR1.TMR2IF = 0
            Ms = Ms+1
            if Ms = 1000 then
                Ms=0
                Seconds = Seconds+1
            endif
        endif
     
    Counting cycles is so last year and also pretty difficult.

    Mike.
     
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    Feel free to push...:rolleyes:

    If you read between the OP's lines, I suspect this is an assignment which has to completed using Oshonsoft Basic.

     
  14. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Eric,

    Can you not do as I suggest above in Oshonsoft? I.E. set individual registers and test an individual bit?

    Mike.
     
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi Mike,
    Yes, you can use ASM lines in the Basic text OK, in OSH Basic.

    I keep asking the OP to post the coding he has done.... still no response.
    I don't mind helping debug what he is doing but I dont intend to write the full program based on his hazy description.

    Eric
     
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Hi Eric,

    I assumed you wouldn't need asm and could just do,
    Code (text):

        T2CON = 5
        PR2 = 249

    // and then

        if PIR1.TMR2IF = 1 then
            1mS has passed

    //or

        if TMR2IF = 1 then
            etc.
     
    Mike.
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    That would work OK.

    I'm trying to avoid doing some ones assignment. [from scratch]

    Eric
    Hope all is ship shape after the flooding.
     
  18. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Yes, probably best to help rather than spoon feed.

    All good after floods. In fact, lost a few kilos due to living on the 14th floor and not having lifts for two weeks.

    Mike.
     
  19. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    Hi
    Thanks for the replies!. just to clarify this is not homework - and i will post the code as soon as i have done it. as i asked in the 1st post was "if counting cycles would work". and i like using oshonsoft so that is why i am asking for basic code.
    If i use the timer function will that run "independently" ie - when i do activate an led i can just reset the second or msecods variables ?
    thx - my code is on it way to you!
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Yes, run it under interrupts.
    Looking forward to seeing the code.
     
  21. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    hi Ian, that is exactly what i am looking for - my question was (from my post) is
    Light LED, start timer by adding "cycles" to variable - while waiting for either a keypress or timeout......save cycles to array
    {CODE}
    getkey:
    if portb.0 = 1 then
    savecycle
    endif
    rtim = rtim + 2 'rtim is the cycle variable
    if rtim = maxtim then
    goto nextLed 'i will clear rtim here before lighting the next led (dont need code for that, only asking about best way for getting the time)
    end if
    rtim = rtim + 2 'rtim is the cycle variable
    goto getkey
    {CODE}
     

Share This Page