Shop OBEX P1 Docs P2 Docs Learn Events
Simple demo of interupt triggered by RTCC - fails — Parallax Forums

Simple demo of interupt triggered by RTCC - fails

John KauffmanJohn Kauffman Posts: 653
edited 2006-04-13 11:26 in General Discussion
File is self-explanatory, I think.
Symptom: LED stays on.


' =========================================================================
'
'·· File...... INT-RTCC-01.SXB
'·· Purpose... Demonstrate simple use of interupt triggered by RTCC
'·· Author.... John Kauffman
'·· E-mail....
'·· Started...
'·· Updated... 2006 04Apr 12 - 17:42
'
' =========================================================================

'
' Program Description
'
' Purpose = Demonstrate simple use of interupt triggered by RTCC
' objective: divide down RTCC interupts until one flash per 4 seconds.
' 4,000,000 / 256 / 256 / 256 = 0.25 = once per 4 seconds.
' RTCC prescaler set to 256
' INterupt increments ISRCOunter by 1
' In MAIN program when ISRCOUnter = 255 then increment ISRMultiplier1
' In MAIN program when ISRMultiplier1 = 255 then increment ISRMultiplier2
' When ISRMultiplier2 gets to 255 LED flashes
'
' Device Settings
'
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
LED··VAR·RC.0···
' LED circuit:
' RC.0 - 470ohm - LEDcath - LEDanode - Vdd
'
' Constants
'

'
' Variables
'
ISRCounter VAR byte
ISRMultiplier1 VAR byte
ISRMultiplier2 VAR byte
' =========================================================================
INTERRUPT
ISR_Start:
ISRCounter = ISRCounter + 1
ISR_Exit:
· RETURNINT ' {cycles}································
' =========================================================================
PROGRAM Start
Start:
'Pin····· 76543210
' Following line sets only RC.0 (LED) to be an output
TRIS_C = $11111110·'Alternate = $FE
OPTION = $100000111
'bit····· 76543210
' option bits (left-7 to right-0)
' RTW·1 = RTCC ( not W)
' RTI·0 = RTCC roll-over interupt is enabled
' RTS·0 = Increment is form internal cycle (not expternal pin)
' RTE·0 = Increments on low to high transition (should have no effect since not on pin
' PSA·0 = Prescaler assigned to RTCC (not W)
' 1 = Prescaler = 1:256
' 1 = Prescaler = 1:256
' 1 = Prescaler = 1:256
Main:
' 4_000_000 / 256 / 256 /256 = 0.238 = once per 4 seconds
' first divide by 256 is in prescaler
IF ISRCOUnter = 255 Then
· ISRMultiplier1 = ISRMultiplier1+1
· IF ISRMultiplier1 = 255 THen
····· ISRMultiplier2 = ISRMultiplier2+1
····· IF ISRMultiplier2 = 255 THen
·HIGH LED
·Pause 500
·LOW LED
····· Endif
· Endif
Endif

Comments

  • BeanBean Posts: 8,129
    edited 2006-04-12 23:25
    John, John.. You must be in a rush.

    TRIS_C = $11111110 'Alternate = $FE
    OPTION = $100000111

    Binary numbers must be preceeded with a "%" not a "$".
    Your OPTION value has 9 bits.
    You don't have a loop to keep check the values
    You have one too many /256 variables. (remember the prescaler divides by 256).



    I took the liberty of editing your code...
    ' =========================================================================
    '
    '   File...... INT-RTCC-01.SXB
    '   Purpose... Demonstrate simple use of interupt triggered by RTCC
    '   Author.... John Kauffman
    '   E-mail.... 
    '   Started...
    '   Updated... 2006 04Apr 12 - 17:42 
    '
    ' =========================================================================
    ' -------------------------------------------------------------------------
    ' Program Description
    ' -------------------------------------------------------------------------
    ' Purpose = Demonstrate simple use of interupt triggered by RTCC
    ' objective: divide down RTCC interupts until one flash per 4 seconds.
    ' 4,000,000 / 256 / 256 / 256 = 0.25 = once per 4 seconds.
    ' RTCC prescaler set to 256
    ' INterupt increments ISRCOunter by 1
    ' In MAIN program when ISRCOUnter = 255 then increment ISRMultiplier1
    ' In MAIN program when ISRMultiplier1 = 255 then increment ISRMultiplier2
    ' When ISRMultiplier2 gets to 255 LED flashes 
    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ            4_000_000
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    LED  VAR RC.0   
    ' LED circuit:
    ' RC.0 - 470ohm - LEDcath - LEDanode - Vdd
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    ISRCounter VAR byte
    ISRMultiplier1 VAR byte
    ' =========================================================================
    INTERRUPT
    ISR_Start:
    ISRCounter = ISRCounter + 1
    ISR_Exit:
      RETURNINT ' {cycles}                                 
    ' =========================================================================
    PROGRAM Start
    Start:
    'Pin      76543210
    ' Following line sets only RC.0 (LED) to be an output
    TRIS_C = %11111110 'Alternate = $FE
    OPTION = %10000111
    'bit      76543210
    ' option bits (left-7 to right-0)
    ' RTW 1 = RTCC ( not W)
    ' RTI 0 = RTCC roll-over interupt is enabled
    ' RTS 0 = Increment is form internal cycle (not expternal pin) 
    ' RTE 0 = Increments on low to high transition (should have no effect since not on pin
    ' PSA 0 = Prescaler assigned to RTCC (not W)
    ' 1 = Prescaler = 1:256
    ' 1 = Prescaler = 1:256
    ' 1 = Prescaler = 1:256
    Main:
    ' 4_000_000 / 256 / 256 /256 = 0.238 = once per 4 seconds
    ' first divide by 256 is in prescaler
    DO
      IF ISRCOUnter = 255 Then
        ISRMultiplier1 = ISRMultiplier1 + 1
        IF ISRMultiplier1 = 255 THen
          HIGH LED
          Pause 500
          LOW LED
        Endif
      Endif
    LOOP
     
    


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video
  • John KauffmanJohn Kauffman Posts: 653
    edited 2006-04-13 01:36
    Yes, those were dumb mistakes. Works like a charm now. I feel a lot more comfortable about using RTCC-timed ISR. Thanks.

    If I'm using the RTCC to generate relatively long times betwee pulses (like 1-100 seconds) is it correct in the ISR only set the prescalar high and do just a simple variable increment in the interupt and then do the scale-up looping in Main? In other words, would it be better to move that scaling into the interupt?

    I'm a little perplexed by the result. I'm getting a flash every 3.8 seconds when I expected every 4.2 seconds. I can't imagine anything happening in processing at an SX's speed that is using up .4 seconds. Anyhow, the RTCC should kick in regardless of what is happening in main, that is the idea of an interupt.

    Sources: ??
    Internal timer is off by 11%
    My math is wrong?
    PAUSE 500 is having an effect on the RCTT?

    I'm also having problems finding in the literature when the RTCC is set to zero. Assuming prescaler of 1:1, does that always happen on the next cycle after count 255, regardless of the execution being in ISR or MAIN and regardless of what command is being executed (understanding, of course, that some commands take several cycles ot complete).


    Next step is to count the flashes and have the count show up on my four digit display.
    Then I want to get the timing solved and get an exact 0.010 second count going in the display
    Last, I want the display timer to start and end with closing of switches.
    Then I will have a timer for the elapse between switch-closing events lasting 0 to 99 seconds displayed to 0.01 seconds.
  • BeanBean Posts: 8,129
    edited 2006-04-13 11:26
    John,
    Yes, Usually the scaling is done in the interrupt. I normally have the interrupt to ALL the work, but if you need cooperation, have the main program clear a flag, then have the interrupt SET the flag after the time has been reached. The main program will look the flag to be set, then perform whatever action, then clear the flag again.


    Also you should check for the counters being zero instead of 255. When they reach 255 that is only dividing by 255 NOT 256.

    IF ISRCOUnter = 0 Then
    · ISRMultiplier1 = ISRMultiplier1 + 1
    · IF ISRMultiplier1 = 0 Then

    PAUSE will not affect RTCC adversely.

    RTCC will be zero the very next cycle after it is 255. When the very first interrupt instruction is executed RTCC is 3. (Remember SX/B does some housekeeping for interrupts so RTCC will be > 3 when the first SX/B instruction is executed).

    John, as for the display (I'm assuming you mean the HC4LED display from me), keep each digit as a seperate variable. This eliminates the need to decode the digits. Something like

    Digits VAR Byte (4)

    INC Digits(0)
    IF Digits(0) = 10 THEN
    · Digits(0) = 0
    · INC Digits(1)
    · IF Digits(1) = 10 THEN
    ··· Digits(1) = 0
    ··· INC Digits(2)
    ··· ' And so on...
    Let me know if you need assistance with the display program.·I don't just want to write if for you, I'd rather you "learn to fish".

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
    Product web site: www.sxvm.com

    Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com

    "I reject your reality, and substitute my own." Mythbusters


    Post Edited (Bean (Hitt Consulting)) : 4/13/2006 11:32:27 AM GMT
Sign In or Register to comment.