Page 1 of 1

6522 VIA timer

Posted: Mon Sep 05, 2005 12:36 am
by asmlang_6
I am thinking of building a 6502-based digital clock. For the timing, I would like the timing to be done by a 6522 VIA. How do I program Timer 1 of the 6522 to tick once a second? Oh, and I don't want to use any approaches that modify PA or PB, since I'd like to hook up those to a BCD decoder.

Re: 6522 VIA timer

Posted: Mon Sep 05, 2005 2:25 am
by RichCini
asmlang_6 wrote:
I am thinking of building a 6502-based digital clock. For the timing, I would like the timing to be done by a 6522 VIA. How do I program Timer 1 of the 6522 to tick once a second? Oh, and I don't want to use any approaches that modify PA or PB, since I'd like to hook up those to a BCD decoder.
Start by determining the 16-bit value that will give you a 1Hz timeout. This is dependent on the clock frequency, which if it's a stock 6522 would be 1MHz. Disable Timer 1 interrupts in the IER unless you want/need to respond to a timeout interrupt. Set the ACR for the proper mode (probably $C0 for free-running, PB7 output enabled). Then, store the count value in register address +4 and +5 (low byte and high byte latches). Writing to the high-order latch starts the counter. PB7 is the output pin which may need conditioning/buffering depending on the circuit loading.

Although I haven't pulled the 6522 data sheet, as I recall it had some good information on this (no code unfortunately), but it's pretty straightforward.

Posted: Mon Sep 05, 2005 5:33 am
by GARTHWILSON
Quote:
How do I program Timer 1 of the 6522 to tick once a second?
You'd have to use a very low clock speed (about 65kHz max!) to get it up to a 1-second timeout.  Otherwise, use a much faster time-out rate and count up in software, changing the display only when the count reaches a high enough value to increment the seconds' byte.

The 6522 VIA's T1 can be used in different ways, including free-running with the time-out frequency being determined by the phase-2 frequency and the T1 latch value you store.  (The time-out frequency is actually ph2/(n+2), where ph2 is the phase-2 frequency, and n is the latch value.  The latch (and counter) however, being only 16-bit, cannot count nearly high enough to go a whole second unless the clock frequency is really low.  On my 5MHz workbench computer, I have VIA1 cause an interrupt every 10ms (.01 second) for keeping time, since the VIA can't even count up to the next round number of 20ms at 5MHz, or even to 1/60th of a second.  Every time T1 times out, an interrupt is generated, and software updates the memory locations that keep the time from the hundredths-of-seconds place to the years' place.  This is covered in the 6502 interrupts primer, which also covers the code for how to set up the 6522.  The "Tip of the Day" column at http://www.6502.org/forum/viewtopic.php?t=342 may also be helpful, particularly tips numbers 9 and 21.  (The purpose of the "Tip of the Day" was to give ideas of what can be done, not to give all the details of how.)


Quote:
since I'd like to hook up those to a BCD decoder.
Why would you use a BCD decoder?  Just use the software to figure out the decimal output and which LED segments to light up to get the right display.  Use the software also to strobe the LED matrix so you don't need so many connections.  Then a single 6522 will have enough I/O lines to take care of the display, the buttons, and the beeper or speaker (also connected directly, with no other hardware).