Author Topic: Help using crystal timers to regulate the speed of a loop.  (Read 2954 times)

0 Members and 1 Guest are viewing this topic.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Help using crystal timers to regulate the speed of a loop.
« on: December 18, 2013, 07:20:49 pm »
I'm trying to make the main loop of my program run at 30Hz. Here is how I set up timer 1 at the beginning of my app:
Code: [Select]
  ;set timer 1 to not loop, not interrupt
  ld a, $00
  out ($31), a
  ;set timer 1 to 993Hz
  ld a, $41
  out ($30), a
  ;set timer 1 count down from 33 for effective framerate of ~30Hz
  ld a, 33
  out ($32), a

And at the beginning of my loop (which is copied to appBackupScreen), I have this:
Code: [Select]
  in a, ($32)
  cp 0
  jp nz, appBackupScreen
  ld a, 33
  out ($32), a
  ld a, $00
  out ($31), a
  ;rest of code goes here

I'm pretty sure what I am doing is setting the timer to 993Hz, disabling repeats and interrupts, and at the beginning of the loop polling the timer until it is zero, then continuing with the rest of the code. However, it gets stuck in the polling loop. According to Wabbitemu's debugger, register A has a value of 33 (decimal), indicating the timer is not counting down. If I jump over the polling loop in my code, everything else runs perfectly.
« Last Edit: December 18, 2013, 07:23:05 pm by fb39ca4 »

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Help using crystal timers to regulate the speed of a loop.
« Reply #1 on: December 18, 2013, 07:34:31 pm »
I believe it may be the case that the counter never actually hits 0, and instead goes from 1 straight to the last value that was written to it. Anyway, the proper way to detect if a crystal timer has finished is to check its bit in port 4.
« Last Edit: December 18, 2013, 07:36:16 pm by Runer112 »

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Help using crystal timers to regulate the speed of a loop.
« Reply #2 on: December 18, 2013, 07:38:35 pm »
If it is resetting to the previous value, how do you start it again?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Help using crystal timers to regulate the speed of a loop.
« Reply #3 on: December 18, 2013, 07:44:18 pm »
Your code to restart it seems fine.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Help using crystal timers to regulate the speed of a loop.
« Reply #4 on: December 18, 2013, 07:47:27 pm »
If I am understanding this correctly, reading the counter port will not give the current count of the timer, only the value you wrote into it, correct?

EDIT: Thanks, worked perfectly!
« Last Edit: December 18, 2013, 07:58:36 pm by fb39ca4 »