Page 1 of 2

Pic interface: keyboard, mouse, i2c...

Posted: Wed Aug 17, 2011 2:48 pm
by Dajgoro
Hello!

Since i am building a 6502 project of my own, i wanted to have keyboard interface, so i decided to build the Daryl's AVR PC Keyboard IO. I went to the shop but they didn't have any Attinys. So i asked Daryl could i use some other microcontroller, he said me i could use Atmega. So i bought Atmega8, and downloaded Avr studio. I did the proper modifications to make it work on Atmega, but when i hit compile all kind of crazy errors started popping up, and i was unable to fix it...
So since i am using the Microchip pic for a while, i decided to make my own keyboard interface, that would have more options, like switching Z and Y (in Croatia and Germany is switched). So i went to the shop, i spent a lot of money buying two 16f628 (one for later). When i finished my program(using proton ide) of course i now need to program the pic and test if it works! So i put my new 16f628 in my ebay miracle programmer(it cad program form eproms to avr pic, it can test ic...) and hit program! But something went wrong and the pic kinda got fried, ok i put the other one, it programed id, just to be sure i tried to reprogram it, but it seems my new pic didn't like my chinese miracle programmer...
That is kinda weird, because i programmed eproms eeproms atmega pic, and i never had problems with it... After that it was nearly the end of my semester, and i had to study for the final exams... So now when i am home i thought, what if i used a pic16f877 (my favorite), so i bought one from ebay at 1/3 of the price that i could find it at the electronic store.
But this is a powerful microcontroller and it is a waste to use it just as a keyboard interface, so what if i use all of its resources, and have a keyboard and mouse interface, lcd interface, i2c bus interface, a/d converters... And i started doing some schematics, but then i realized that i exactly don't know how does (for example) EhBasic (or other) handle the character input, who has to empty the register when the character is read? Does EhBasic automaticly store 00, or it must empty automaticly after reading? So when i solve this (and some other) mystery i can finally finish planning this and start soldering...
I will post my progress, and discuss my design...
(Please don't get angry if i do something horribly wrong...)

Posted: Thu Aug 18, 2011 12:20 am
by Nightmaretony
Nothing wrong with trying and sharing. Always adds to the fun.

One nice thing on the tinys, small and cheap but a nice power. Was always praying for a tiny architecture but being a 65C02.

The pic you know best, you may want too ask Darryl for the source for his keyboard encoder and write your own for the pic to interface. then share your knowledge and adventures as others may want to go the pic route as well....

Posted: Thu Aug 18, 2011 1:10 am
by Dajgoro
Daryl's keyboard source code is free for download at his web page.
Actually i used his scancode to ascii table in my interface(I hope he doesn't mind).
I already finished the keyboard interface(code), but i hat trouble with toes pic 628. Currently i have the pic16f877 hooked up to a lcd, and when i type something on the keyboard it shows on the lcd. But now i need a interface for the 6502 bus, and i need yet to make a mouse and i2c interface...
When i finish this, i will put all the code and schematic for download of course...

Posted: Thu Aug 18, 2011 2:48 pm
by Nightmaretony
Lee Davison has a great I2C interface on his projects page which is pretty adaptable. I think it can go inside a CPLD, no problem.

Posted: Fri Aug 19, 2011 2:46 am
by Dajgoro
Nightmaretony wrote:
Lee Davison has a great I2C interface on his projects page which is pretty adaptable. I think it can go inside a CPLD, no problem.
I don't have any CPLD, i only have microcontrollers. The Proton IDE(witch i use for the pic) has already implemented i2c and spi, and other functions, so it is not much trouble here...
I will post my first schematic in a few days, so we might start some discussion...

Posted: Sun Aug 21, 2011 4:03 pm
by Dajgoro
Here is my first try on this:
6502.org wrote:
Image no longer available: http://www.iteracija.com/mupload/pici.JPG
(Sorry for paint schematic, ill do a proper one when discussion finishes...)

The idea is, that writes happen only on clock phase 1, and when pic wants to write something, it sets the bistable, and wait for the phase 1. When writing is done, it sets 0 on the output port, so when the 6502 reads something, it sets the bistable, and it automatically clears the latch.

I see 2 problems with this design: 1. If more data needs to be read from the 6502 will require special subroutine that would wait for the data. 2. The design gets a bit complicated and slow...

I think it would be a better solution to have something like a 64 bit ram, and communicate through it. But then how do i clear something when it is read?
Any ideas?

Posted: Sun Aug 21, 2011 4:22 pm
by BigEd
Hi - try saving your picture as a gif or png instead of bmp.
Cheers
Ed
(Edit: picture is showing now, as a jpg.)

Posted: Sun Aug 21, 2011 7:41 pm
by GARTHWILSON
How about using a parallel slave port on a PIC. The hardware should be much simpler. Edit, 10/19/18: Microchip's ap. note AN579, about using the PSP, is at http://ww1.microchip.com/downloads/en/A ... 00579b.pdf .

Posted: Sun Aug 21, 2011 7:58 pm
by Dajgoro
Whit this i could send keyboard data, but still how do i then receive other data from mouse, i2c...

Posted: Sun Aug 21, 2011 8:00 pm
by GARTHWILSON
You would have to work out your protocol in software, but it can be done.

Posted: Sun Aug 21, 2011 9:23 pm
by OwenS
The PIC 877 (and successor 877A) have a builtin "parallel slave port" feature. While this is nowhere near as well featured as on more modern (PIC18, PIC24, dsPIC and PIC32), it should be feasible to implement a reasonably simple "command response" interface. For example, you might say that doing a write of $00 followed by a read would return the status register, which would contains bits such as "Character waiting" and "I2C operation complete". You might then do a write of $01 followed by a read to get the keyboard character. You just have to be careful that you never allow an interrupt to occur in the middle of one of these operations.

It requires an Intel-style bus interface, but you probably have one of these for your SRAM.

The complex bit (and one area where more modern PICs make things easier) is that you have to make sure you don't access the PIC faster than it can keep up, since there is no "in progress" output.

Posted: Mon Aug 22, 2011 1:00 am
by Dajgoro
OwenS wrote:
The PIC 877 (and successor 877A) have a builtin "parallel slave port" feature. While this is nowhere near as well featured as on more modern (PIC18, PIC24, dsPIC and PIC32), it should be feasible to implement a reasonably simple "command response" interface. For example, you might say that doing a write of $00 followed by a read would return the status register, which would contains bits such as "Character waiting" and "I2C operation complete". You might then do a write of $01 followed by a read to get the keyboard character. You just have to be careful that you never allow an interrupt to occur in the middle of one of these operations.

It requires an Intel-style bus interface, but you probably have one of these for your SRAM.

The complex bit (and one area where more modern PICs make things easier) is that you have to make sure you don't access the PIC faster than it can keep up, since there is no "in progress" output.
I thought about that, but then the problem is in the way keys are read, if i got it right, when a key is pressed, it appears, and when is read, it clears the register.
So if i use the method mentioned above, it would seem as a key is being pressed when i read something from the pic. I could make a bypass for the keyboard somehow... Or something?

Posted: Mon Aug 22, 2011 1:19 am
by GARTHWILSON
A handshake protocol implemented in software would make sure there are no overruns or repeated info. It would make sure the master knows when the slave is ready for more data (or instructions) or has new data to send, and what the data are.

Posted: Mon Aug 22, 2011 1:21 am
by Dajgoro
But if i use EhBasic, how do i implement such protocol in his get and put character system?

Posted: Mon Aug 22, 2011 1:24 am
by GARTHWILSON
I'm sure you would do that part in assembly language, probably based on interrupts so it would not even bother the BASIC system. The BASIC wouldn't even have to know you're doing it. Maybe Lee or someone who's familiar with EhBASIC will jump in here and confirm that that will work fine.