6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 16, 2024 10:46 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Mar 30, 2016 8:54 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
I'm trying to understand how to use the '22 for a few different devices.

I'd like to use one to read a legacy TI-99 4/a keyboard I have (matrix), some joysticks, etc.

But I'm having trouble finding these kind of tutorials.

I've read the code samples on this site but it just isn't clicking with me.

Anyway, let's say for example I had this computer on a breadboard:

http://wilsonminesco.com/6502primer/potpourri.html (first schematic on that page)

This is a simple design that contains a VIA mapped to some headers.

Now, what would be great is a tutorial that teaches:

1) How to read a button on PA0, for example.

2) How to read a classic Atari 2600 joystick on PA.

3) How to read a keyboard matrix (like this one: http://mainbyte.com/ti99/keyboard/keyboard.html).

4) How to display byte to PA and light some LED's (8 LED's for debugging).

I hope I'm making sense. The few code samples I've seen seem to assume I already know what a one-shot timer is for the VIA, etc. I mean, I'm familiar with timers and IRQ (I've used them on Arduino's and I've written raster demos for C64, etc.).

But reading the datasheet just isn't doing it for me.

Any tips or pointers would be appreciated.

Thanks!

_________________
Cat; the other white meat.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 1:50 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
You're not in the proper "hardware" state of mind! I know, it eludes me too. Regarding the one-shot timer concept: I think (perhaps incorrectly) that a one-shot example would be reading an analog joy-stick the old-fashioned Apple ][ way. Smack the pin and start busy-counting until it RC's its way back to rest. The count is your A to D value.

I always tend to see things through "software" goggles, as I suspect you do too.

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 5:32 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8541
Location: Southern California
There are lots of ways to do all of these.

Quote:
1) How to read a button on PA0, for example.

Use the pushbutton switch to ground the PA0 pin, and have a passive pull-up resistor of say 10K. Then read the port and test the bit and branch something like this:
Code:
    LDA  PA0
    AND  #1
    BNE/BEQ

or, if you already have 1 in A, you could do
Code:
    BIT  PA0
    BNE/BEQ

Using BIT has the advantage that it does not change the value in A, so you can re-use it over and over; for example, you could have a loop that keeps checking the bit without having to re-load A every time through the loop, and check until the button gets pressed. The computer can't do anything else while it's twiddling its thumbs waiting for this input this way, but it's sure simple.

Quote:
2) How to read a classic Atari 2600 joystick on PA.

If I may save the time to research it myself, can you point to a page showing how the joystick works. It would have a pair of potentiometers I'm sure, but does it have any other electronics, to act as some cheap form of A/D (analog-to-digital) converter? [I started writing this before Michael posted above but I got called away.] I seem to remember the Apple II discharged a capacitor, then let it charge up through the pot, and there was a comparator that detected when the voltage on the capacitor reached a threshold, and the comparator's output went to a VIA pin. The lower the resistance of the pot at the moment, the less time it took for the capacitor to charge up to that level. Then the processor would use one of the VIA timer/counter references to see how long it took and therefore the position of the pot.

Quote:
3) How to read a keyboard matrix (like this one: http://mainbyte.com/ti99/keyboard/keyboard.html).

One way would be to have PB set as outputs going to the columns, and PA set as inputs going to the rows. Put passive pull-ups on the rows. Clear the output bit corresponding to the column you want to check, then read PA and see if it's something other than all 1's. If it is, you can do something like shift left repeatedly and branch on the C bit to see which key was pressed. Otherwise, go on to check the next column the same way, cycling through the keyboard. Actually, since most of the time there won't be any key at all pressed, the program would be more efficient if you first activate all the columns, and see if the rows byte is $FF. If it is, there's no point in checking every row individually.

To prevent shorting two column outputs together with multiple simultaneous key presses, keep PB holding 00 and write to DDRB so only make one column at a time is an output while the others are inputs (meaning they're basically inactive for this application).

Quote:
4) How to display byte to PA and light some LED's (8 LED's for debugging).

You can connect them directly from the PA pins to ground or Vcc, but put a 470Ω in series to control the current.

See what I say about debugging in the http://wilsonminesco.com/6502primer/debug.html]debugging chapter of the 6502 primer though. LEDs are not as dependable as a software beeper, since they are more likely to be turned on and off by wrong program behavior than the beeps, making it look like things are fine when they're not. The software beeper can be made to produce different frequencies and lengths of beeps to have different signatures. LEDs tend to be better when you can't take the time for a beep, or when you're using them more as status lights than trying to figure out where something is crashing.

_________________
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 7:11 am 
Offline
User avatar

Joined: Tue Feb 10, 2015 5:27 pm
Posts: 80
Location: Germany
As the VIA is used in a VIC-20 you can have a look in the PCB layout for the hardware side. Same applies for programming. (You also could look into how C64 CIAs work - for reading keyboard and joysticks they work similar.)

Atari2600 joysticks are digital joysticks, so you have 5 buttons PA0-5 fpr up, down, right, left, fire. You can use the snippets already posted. Or look into C64/VC20: codebase64


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 9:48 am 
Offline

Joined: Tue Sep 03, 2002 12:58 pm
Posts: 336
GARTHWILSON wrote:
Quote:
1) How to read a button on PA0, for example.

Use the pushbutton switch to ground the PA0 pin, and have a passive pull-up resistor of say 10K. Then read the port and test the bit and branch something like this:
Code:
    LDA  PA0
    AND  #1
    BNE/BEQ


But make sure you've set PA0 to input. If you're not using the rest of PA for anything (or you want to use all of the other pins as inputs as well), then
Code:
    LDA #0
    STA DDRA

will do the job. You only need to do this in initialisation, not every time you read.

Quote:
Quote:
2) How to read a classic Atari 2600 joystick on PA.

It would have a pair of potentiometers I'm sure, but does it have any other electronics, to act as some cheap form of A/D (analog-to-digital) converter?


It's much easier than that. A 2600 joystick is just five switches. Up/down/left/right and fire, with a common ground. Connect them to five inputs (with pullup resistors) and read them exactly as you would for the single button.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 12:20 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
Thanks everyone for your responses! This has been immensely helpful.

barrym95838 wrote:
You're not in the proper "hardware" state of mind!


That is true. I am a software developer by trade. :-D

@GARTHWILSON, @John West

Thanks for your detailed information!

You helped me realize that it's not as hard as it seems. At least for simple tasks like this. I have to admit, not knowing how the VIA works has prevented me from actually experimenting with it which is silly. I just need to get the thing on the board and start programming it. Having programmed a C64 for years, it's always been LDA <somewhere>....STA <somewhere else>. Not really caring HOW or WHY those addresses worked.

I will get a circuit going soon and will ask questions if I run into trouble.

@Hobbit1972

Thanks! I forgot that the VIC-20 used the 6522. I have a VIC-20, mint in box. I might pull it out and tinker with it too.
In fact, the Wikipedia article for the VIC-20 used to have MY actual computer and box as the "official" picture but someone replaced it.

_________________
Cat; the other white meat.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 31, 2016 8:41 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8541
Location: Southern California
John West wrote:
But make sure you've set PA0 to input. If you're not using the rest of PA for anything (or you want to use all of the other pins as inputs as well), then
Code:
    LDA #0
    STA DDRA

will do the job. You only need to do this in initialisation, not every time you read.

Or even easier, STZ DDRA. However, the data-direction registers come out of reset with 00 anyway, for all bits being inputs.

cbmeeks wrote:
You helped me realize that it's not as hard as it seems. At least for simple tasks like this. I have to admit, not knowing how the VIA works has prevented me from actually experimenting with it which is silly. I just need to get the thing on the board and start programming it.

The data sheet is very clear. Have at it, and don't forget the tips, starting at viewtopic.php?f=7&t=342 .

_________________
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?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: