Delay routines
Re: Delay routines
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.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Delay routines
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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Delay routines
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.
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.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Delay routines
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.
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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Delay routines
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.
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
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.
-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
- BitWise
- In Memoriam
- Posts: 996
- Joined: 02 Mar 2004
- Location: Berkshire, UK
- Contact:
Re: Delay routines
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.
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.
Last edited by BitWise on Fri Oct 16, 2020 10:10 am, edited 1 time in total.
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
Re: Delay routines
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.
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.
-
leepivonka
- Posts: 168
- Joined: 15 Apr 2016
Re: Delay routines
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:
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
If you want something a bit tighter, try working with the low byte of T1: 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.
Code: Select all
Delay2500us:
LDA T1CL
CLC
ADC #82
: CMP T1CL
BNE :-
RTS
Re: Delay routines
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.
@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.