Page 2 of 2

Re: Delay routines

Posted: Thu Oct 15, 2020 10:59 pm
by tokafondo
I just read this thread to try to understand delays by using hardware timers. The w65c265s has several timers and I just can't get an idea about how to properly use them.

Re: Delay routines

Posted: Fri Oct 16, 2020 12:02 am
by GARTHWILSON
tokafondo wrote:
I just read this thread to try to understand delays by using hardware timers. The w65c265s has several timers and I just can't get an idea about how to properly use them.

Maybe we should start with what you want to do, and what length of delays you're thinking of. The answer will probably be what has already been written, but with examples to make it more clear.

Re: Delay routines

Posted: Fri Oct 16, 2020 12:34 am
by tokafondo
Thanks.

Well, I need to set a delay during the initialization of the EPSON graphics chip I'm using with a '265.

I first write a series of values to the registers by using LDA and STA, and then at a point I should wait 2500 microseconds (us) and then continue writing values to those registers.

I'm currently running the '265s at 4.9152mhz but I would like to be able to get the delay not clock speed related: no matter how fast or slow is the MCU running, the delay routine should last always 2500 us. And that leads to me to think that the way to do it is by using a hardware timer.

It seems to me that the first thing I should do is to initialize the timer hardware itself, because it should adapt to the clock speed, shouldn't I?

But then I just don't know how the timer thing works. Is it something like... "wait xxxx microseconds and then continue"?

I'll have to read the links put earlier in this thread.

Re: Delay routines

Posted: Fri Oct 16, 2020 2:11 am
by GARTHWILSON
Then timer the will need an external time reference, rather than counting phase-2 pulses. I'm not familiar with the '265; but from a quick look at an old data sheet, it looks like T4 gives that option, with TCR0=1, and then it give you P60, whatever that is. However, if this is only during the initialization, you won't have anything else running and urgent yet, meaning you could still lock it to phase 2 or even use a software delay, and make the delay long enough at even the fastest frequency you expect to ever use, and it will work for all frequencies below that. After the LCD is set up, you won't need to do it again while applications are running, right?

But if you still want to use a hardware timer, you could in this case just set the initial count value and then keep reading it in a loop to see when it timed out. Reading the status register would probably be easier than reading two or more bytes of counter value. You could set it up to interrupt, but I don't think there's any point in this case, because it's only on startup and you won't have anything else running yet that will need attention while you're waiting for the time to expire. It's just a very non-critical situation.

Re: Delay routines

Posted: Fri Oct 16, 2020 4:25 am
by Chromatix
My favoured approach for this is to set up an RTC chip such as the DS1501 or DS1511. This, together with several other advantages, can be configured to emit a 32768 Hz signal which can be used as a Phi2-independent timing reference. Count 81 of those and you'll satisfy the 2.5ms delay interval you need.

I have a reasonably good handle of how to do that with a 6522 VIA. To do it with a '265 might be similar.

Re: Delay routines

Posted: Fri Oct 16, 2020 4:50 am
by Dr Jefyll
tokafondo wrote:
I would like to be able to get the delay not clock speed related: no matter how fast or slow is the MCU running, the delay routine should last always 2500 us.
It's worth learning how to produce a delay that's not clock speed related, but you needn't do so now unless you're looking for extra challenge. Remember, 2500 is merely the minimum delay required by the LCD. That's why Garth suggested you could simply use a software delay, set up to produce 2500 us at the fastest clock frequency. At lower frequencies it'll produce a needlessly long delay, but that's unlikely to bother you.

-- Jeff

Re: Delay routines

Posted: Fri Oct 16, 2020 9:46 am
by BitWise
265 timers decrement a counter until it reaches zero then generate an interrupt and reload for the counter for the next period.

Most of the timers are linked to special functions so best to avoid those which leaves T2 and T7 as general purpose.

The easliest way to work out counter values is from the frequency of the required interrupts. If you want an interrupt every millisecond then thats 1000 Hz so the counter is your oscillator speed divided by the frequency minus one.

Counter = OSC / FREQ - 1 = 4MHz / 1000Hz -1 = 4000000 / 1000 - 1 = 3999

T2 is prescaled so it decrements every 16 FCLK periods so for this one calculate

Counter = (OSC / 16) / FREQ - 1 = OSC / (16 * FREQ) - 1

I usually program a timer for a regular interrupt every 1mS or 10mS and use that to generate longer delays by incrementing a time value in memory and comparing against a target delay end time.

Re: Delay routines

Posted: Fri Oct 16, 2020 10:02 am
by tokafondo
Thanks all for your answers.

The '265 is fed with two crystals: one CLK 32768hz and one FCLK the mhz speed you want it to run at, up to 8mhz.

Well I look for program examples on how to do that.

Re: Delay routines

Posted: Fri Oct 16, 2020 2:15 pm
by leepivonka
On the '265, running the built-in Mensch monitor:

The '265 timer T1 is set up to count CLK (slow clock, the 32768Hz oscillator) to generate 1 second interrupts for real-time clock maintenance.
This means the hi byte of the T1 counter changes about every 7 msec.

Here is a small routine that might work for the LCD waits:

Code: Select all

Delay7Msec: ; delay between 7 & 14 msec
    lda T1CH  ; wait for a change, 0 to 7msec
@1: cmp T1CH
    beq @1
    lda T1CH  ; wait for another change, about 7msec later
@2: cmp T1CH
    beq @2
    rts

Re: Delay routines

Posted: Fri Oct 16, 2020 5:30 pm
by Chromatix
If you want something a bit tighter, try working with the low byte of T1:

Code: Select all

Delay2500us:
  LDA T1CL
  CLC
  ADC #82
: CMP T1CL
  BNE :-
  RTS
The above code works reliably as long as the timer ticks much slower than the CPU clock, at most once per inner loop (7 cycles). That's true if the timer is 32kHz and the CPU runs at 250kHz or more.

Re: Delay routines

Posted: Sat Oct 17, 2020 11:11 pm
by tokafondo
Thanks.

@leepivonka 7msec seems to be more than right for this.

@Chromatix The CPU runs at 4.9152 mhz. So it's way more than 250khz.