Shop OBEX P1 Docs P2 Docs Learn Events
Interrupt RETIW question... — Parallax Forums

Interrupt RETIW question...

cbmeekscbmeeks Posts: 634
edited 2006-09-19 02:51 in General Discussion
I've never really worked with interrupts (well, I did on the C64) but I am realizing that my VGA video circuit needs them.

Follow along and let me know if I am wrong. Let's say I want an interrupt to occur every 900 clocks. Now, I assume I set the prescaler to 1:4 because 256 clocks * 4 = 1024 clocks. Next I run my code and let's say it runs for 850 clocks (under my 900 limit). I would then call a RETIW W, -#124 because 1024 - 900 = 124. Is this correct?

Once again, assuming that logic is correct. What if I wanted to make sure the interrupt ran for 2542 clocks? I am running at 80MHz. So, my code runs for 2541 but the next level up would be 4096 clocks?? (1:16 scaler * 256). I can't do a RETIW W, -#1554.

Does this mean I will just have to do a RETI and make sure my code executes at 2542? I loose the luxery of the -#xxx method?

Something tells me I will have to do 2 interrupts. If so, how??

Thanks!

cbmeeks


*** EDIT ****

I am really tired. smile.gif I think I figured it out. I'm not multiplying, I am dividing.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Coders' Hangout
A place for programmers to hangout!
http://www.codershangout.com

METROID?
Metroid Classic

Post Edited (cbmeeks) : 9/16/2006 2:52:08 AM GMT

Comments

  • BeanBean Posts: 8,129
    edited 2006-09-16 02:56
    cbmeeks,
    I assume you are using the SX48 ? Then the easiest thing to do is use the timer interrupts (they are 16 bit). Like I do in the color NTSC code. You would use:

    TIMER1 TIMER
    TIMER1 PRESCALE, 0
    TIMER1 R1, 2542
    TIMER1 R2, 2542
    T1CNTA = 4 ' Enable interrupts on R1 or R2 compare match

    Of course that uses 1 timer, but hey that's what they are there for. And in my opinion the most under-used feature of the SX48.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • cbmeekscbmeeks Posts: 634
    edited 2006-09-16 03:11
    Burning the midnight oil too? hahaha

    thanks. I am actually using the SX52 and SX48 so that should work. How would I do that in ASM?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Coders' Hangout
    A place for programmers to hangout!
    http://www.codershangout.com

    METROID?
    Metroid Classic
  • cbmeekscbmeeks Posts: 634
    edited 2006-09-16 03:20
    hmm..I will just use SX/B and use inline asm. smile.gif thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Coders' Hangout
    A place for programmers to hangout!
    http://www.codershangout.com

    METROID?
    Metroid Classic
  • pjvpjv Posts: 1,903
    edited 2006-09-16 06:09
    Hi CB;

    RETIW sets the next interrupt to be at whatever negative value you have in w. So if at RETIW, w holds minus 40, then the next interrupt occurs in 40 "ticks"; that is 40 ticks to the next roll-over.. If the scaler is set to 1, then that is 40 clocks. If the scaler is set to 2, then that is 80 clocks, and so on. So obviously, you can only interrupt at intervals of a N times the scaler value. If you need interrupts inconsistent with that, then you need t do multiple base-line interrupts..... a little tricky, but quite do-able.

    Cheers,

    Peter (pjv)
  • PJMontyPJMonty Posts: 983
    edited 2006-09-18 15:18
    CBMeeks said...
    Follow along and let me know if I am wrong. Let's say I want an interrupt to occur every 900 clocks. Now, I assume I set the prescaler to 1:4 because 256 clocks * 4 = 1024 clocks. Next I run my code and let's say it runs for 850 clocks (under my 900 limit). I would then call a RETIW W, -#124 because 1024 - 900 = 124. Is this correct?

    Peter's description is correct, but I thought I would use your example directly. You may have already figured all of this out, but I'm putting it here in case it can elp anyone else.

    Your use of the of the 1:4 prescaler is correct, but the rest of your example is wrong. If you specifically wanted an interrupt of 900 clocks, you would divide your desired number of cycles (900) by the prescaler (4) to give:

    900 / 4 = 225.

    You would then just put the following at the end of your interrupt routine:

    retiw w, #-225
    



    Through the coolness of the SX interrupt hardware, you would now be generating an interrupt every 900 clock cycles regardless of the length of your interrupt code execution as long as it is shorter than the total interrupt duration. In other words, if your interrupt code took 100 cycles to execute one time, and 832 cycles the next, that simple "retiw w, #-225" will automatically put the right value into the hardware so that regardless of where the RTCC count is, the next interrupt will occur on your desired 900 cycle interrupt duration.

    The jitter free interrupt structure of the SX is perhaps the single best and single most misunderstood feature of the SX chip.
      Thanks, PeterM
  • cbmeekscbmeeks Posts: 634
    edited 2006-09-18 15:53
    Thanks! I tried Friday night to get the 16 bit timers to work in SX/B but had no luck. Then, due to severe neck pains, didn't program any Saturday or Sunday. :-/

    Maybe I can try again tonight.

    So, for my example, if I needed 2542 clocks, I would choose 1:16 because 16*256 = 4096. Then, I would take 2542 / 16 = 158.875. I don't like rounding...if I did:

    retiw w, #-159
    
    



    What is that going to give me? This is for video work and sometimes "close enough" isn't close enough. smile.gif

    cbmeeks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Coders' Hangout
    A place for programmers to hangout!
    http://www.codershangout.com

    METROID?
    Metroid Classic
  • BeanBean Posts: 8,129
    edited 2006-09-18 16:19
    cbmeeks,
    · -159 with the prescaler set to 16, would simply be 159*16 = 2544 clocks (off by 2 clocks)
    · For VGA or black and white NTSC that should be close enough. For color NTSC it ain't gonna fly.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • cbmeekscbmeeks Posts: 634
    edited 2006-09-18 16:20
    Thanks...that is what I was thinking. But even for NTSC color, you can be off by two clocks as long as you are off by two clocks on all 262 lines. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Coders' Hangout
    A place for programmers to hangout!
    http://www.codershangout.com

    METROID?
    Metroid Classic
  • PJMontyPJMonty Posts: 983
    edited 2006-09-19 02:38
    CBMeeks,

    Another (potential) option is to use a smaller pre-scale, and multiple interrupts to accumulate the exact time you need.

    For example, if you set your pre-scale to 1:2, then "retiw w, #-250" would give you 250 * 2 = 500 clocks. Do this five times, and you have accumulated a total of 5 * 500 = 2500 clocks. When this happens, you change the return line to be "retiw w, #-21", which will give you 21 * 2 = 42 clocks, for a total of 2542 clocks.

    Of course, this assumes that you can divide up your work in the interrupt handler across multiple interrupts. IF you can't, then you example of the 1:16 pre-scale is the way to go.

      Thanks, PeterM
  • cbmeekscbmeeks Posts: 634
    edited 2006-09-19 02:51
    thanks but I got it to work!!! woohoo!!

    256x240 VGA with 64 colors....all interrupt driven. Only one glitch on the vsync and I will start my SRAM conversion. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Coders' Hangout
    A place for programmers to hangout!
    http://www.codershangout.com

    METROID?
    Metroid Classic
Sign In or Register to comment.