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. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,160
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    You need to type [code] and [/code] around your code to keep the formatting.

    Mike.
     
    Last edited by a moderator: Sep 14, 2013
  2. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    Code (text):

    Dim i As Byte
    Dim rtim As Word  'number of cycles
    Dim mtim As Word  'max time allowed (in cycles)
    Dim react(10) As Word  'array to hold reaction times

    'new variables from the forum example
    Dim ms As Word
    Dim seconds As Word

    i = 0
    main:  'i will have to restart the pic to reset - fine for now
    If PORTB.0 = 1 Then
        Goto ledon  'simplified version of final app - using 1 led and 1 button
    Endif  'just want this timing issue sorted please.

    If PORTB.1 = 1 Then
        Goto getnewframes
    Endif
    Goto main
       
    ledon:
        PORTA.1 = 1  'led on

    getkey:
    If PORTB.2 = 1 Then  'reaction button
        PORTA.1 = 0  'turn out the light...
        rtim = ms
        react(i) = ms  'save reaction into array
        i = i + 1
    'WaitMs(3000)  'wait 3sec before led on
    Goto ledon  'start led on again and get another timing
    Endif
    If PIR1.TMR2IF = 1 Then
        PIR1.TMR2IF = 0
        ms = ms + 1
            If ms = 1000 Then
                ms = 0
                seconds = seconds + 1
            Endif
    Endif
    If ms > 2 Then  'no key pressed yet so we goto next
        PORTA.1 = 0  'turn out the light
    'WaitMs(3000)
        Goto ledon
        i = i + 1
    Endif
    Goto getkey
    getnewframes:
    'serial data from pc - code not needed
    'when done we start ledon
     
    Here is my understanding of the code fragment using timer / interrupt.

    When i try to simulate this it hangs at ms = ms + 1.
    I thought timer / interrupt needed the interupt pin to chg state in order to register an "interupt" ??
     
  3. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    Hi I think i have a solution - but could you clarify the interupt timing count w.r.t my version of the code ? ie what would ms = 2 be in realtime?

    Code (text):

    TRISB = 0x00  'set all PORTB pins as outputs
    Config PORTB = Input
    INTCON.T0IE = 1  'enable Timer0 interrupts
    INTCON.GIE = True  'enable all un-masked interrupts
    OPTION_REG.T0CS = False  'set Timer0 clock source to internal instruction cycle clock
    Dim i As Byte
    Dim rtim As Word  'number of cycles
    Dim react(10) As Word  'array to hold reaction times
    Dim ms As Word
    Dim seconds As Word

    i = 0
    main:  'i will have to restart the pic to reset - fine for now
    If PORTB.0 = 1 Then
        Goto ledon  'simplified version of final app - using 1 led and 1 button
    Endif  'just want this timing issue sorted please.

    If PORTB.1 = 1 Then
        Goto getnewframes
    Endif
    Goto main
       
    ledon:
        PORTA.1 = 1  'led on

    getkey:
    If PORTB.2 = 1 Then  'reaction button
        PORTA.1 = 0  'turn out the light...
        rtim = ms
        react(i) = ms  'save reaction into array
        i = i + 1
    'WaitMs(3000)  'wait 3sec before led on
    Goto ledon  'start led on again and get another timing
    Endif
    If ms > 2 Then  'no key pressed yet so we goto next
        PORTA.1 = 0
        ms = 0  'turn out the light
    'WaitMs(3000)
        i = i + 1
        Goto ledon
    Endif
    Goto getkey
    getnewframes:  'serial data from pc - code not needed
    End                                              
    On Interrupt  'interrupt routine
    ms = ms + 1  'decrement the value on PORTB
    INTCON.T0IF = 0  'enable new TMR0 interrupts
    Resume                                            
     
    Interupt is almost like an independent thread? sort of?
    so when it fires i add 1 to ms but what does this represent ?
     
    Last edited: May 30, 2011
  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,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK

    hi,
    Aquick run thru of your code on Oshonsoft,
    Add the 'AllDigital' statement and make PORTA an output in order to drive the LED.

    The program detects PORTB.0 ok, but checks PORTB.1 intermittently.
    The ISR is being called OK.
    For Simulation, add Define SIMULATION_WAITMS_VALUE = 1
     
  6. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    thanks for checking the code - however would the value in ms = mseconds? based on 4mhz?
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    I didn't check that out yet, you can get a good idea of the time period by using the 'Real Time Duration & Clock cycles' on the IDE window.

    Or set up the IDE oscilloscope and directly measure any delays. OK.

    EDIT:
    For testing varying delay periods, why dont you set adc0 as an analog input, connect a 10Kpot from +5V and 0V, wiper to the adc input pin.

    Use the pot to vary the test delay period [ use the 10bit adc value] and find the best delay for the project response.

    Do you follow OK.?
     
    Last edited: May 30, 2011
  8. cybersky

    cybersky Member

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

    basically we will be varying the delays from a software pattern sort of test. So i will be sendng the pic the ondelay value and the delay between led on's. then a string representing the "sequence" of led to flash.
    So we have 36 leds with 36 switches in a multiplex setup and measuring the response per "led" based on the max reaction time. we using a pic16f874A - It is for a thesis project (not mine, just helping with the dev/building of rig)

    thanks for the response. I have just wrapped my mind around the timer thing - but the adc will follow soon coz i want to build a temp sensor (which has been done to death - but i want my own and in basic ! LOL).
     
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
  10. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    thanks for the link.
    just noticed something in the sim, can i "use" the timer/interupt pin? or should it be left open? Using the Pic 16f874A.
     
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    You could leave it open, or you could toggle it by clicking on the 'T' on the microview.

    I use this external module for 'pulsing' input pins, give it a try.
     

    Attached Files:

    Last edited: Jun 2, 2011
  12. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    thanks - but in general is it ok to use the timer pins?
    and
    INTCON.T0IE = 1 'enable Timer0 interrupts
    Then
    INTCON.T0IE = 2 'enable Timer1 interrupts ?? if pic has more than one?
     
    Last edited: Jun 2, 2011
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Yes, providing you test for the source of the interrupt, ie: tmro0 or tmr1
     
  14. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    as i understand it... the timer function will fire "automatically". If i use the timer pin as an input and it is triggered would that not also cause the timer function to fire aswell ?? sorry for the "simple" question, but i would rather ask.
    could you pls show a sml piece of code for somthing like: on interrupt 1 , on interrupt 2 ?
     
    Last edited: Jun 2, 2011
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    You can preload the Timer counters and count DOWN to cause an interrupt at 0000 > FFFF
     
  16. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    hi
    do you perhaps have some documentation on the INTCON ?
    INTE = 1 'rb0 pin will trigger interrupt
    INTCON.TOIE = 1 'enable the interrupt to fire based on timer

    OPTION_REG.T0CS = False 'set Timer0 clock source to internal instruction cycle clock
    so
    OPTION_REG.T"1"CS = False 'set "Timer1" clock source to internal instruction cycle clock ?

    how do you use the TMR0 Prescalers ?
    with the lack of literature on Oshonsoft, small code bits are a real help.
     
  17. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    repeat after me : "Google is your friend...", heheh
    I guess my input to oshonsoft would be to help us "lazy" programmers by providing an intellisense feature so that we can check what other attributes there are to functions and SFR's.
     
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    All the INTCON and SFR's are covered in the PIC's datasheets, do you have a copy of the 16F874A sheet.?

    I could post some code fragments for the Timers, what exactly are you wanting to do with them.???

    I know its a reaction timer, but how are the switches , displays etc, connected to the PIC.????
     
  19. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    we have them multiplexed - 6rows on RB with 6cols on RA then a mix of RC & RD for the switch inputs.
    the code samples are just for information sake and to increase my "oshonsoft" knowledge base. - I have downloaded all the datasheets and some tutorials on the SFR and Timers. Perhaps I am needing the "syntax" for oshonsoft more than perhaps the code, but sample do help. if i could ref. multiple timers the possiblities...
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    I guess you have the OSH Basic manual for the 16F series of PIC's.?

    I will try to put together a couple of demo programs, I am busy at the moment, so it could take a couple of days.
     
  21. cybersky

    cybersky Member

    Joined:
    Apr 1, 2009
    Messages:
    64
    Likes:
    0
    yes i have read the oshonsoft manual but it makes you want to flip the monitor around to see where the "missing" pages are ! LOL.
    I have made headway using the datasheets and OSH. timer 1 and timer 2 .
    BUT looking forward to your pearls of code - don't mind the wait just very happy that you were able to assist! thank you.
     

Share This Page