Keypad for use with VIA

For discussing the 65xx hardware itself or electronics projects.
User avatar
Michael
Posts: 633
Joined: 13 Feb 2013
Location: Michigan, USA

Re: Keypad for use with VIA

Post by Michael »

Didn't mean to embarrass you, Mike.

Not sure why the diodes aren't working.

On the software side... I wonder if converting keypad row and column into an index might make it easier to maintain or modify a keymap array that mimics the layout of the actual keypad?

Code: Select all

 #define bitmask(bitnbr) (1 << (bitnbr))

 const char keymap[] = { '0','1','2','3',   // row 0, columns 0..3
                         '4','5','6','7',   // row 1, columns 0..3
                         '8','9','A','B',   // row 2, columns 0..3
                         'C','D','E','F',   // row 3, columns 0..3
                          0 };              // null (no switch pressed)

/********************************************
 *  Scan Keypad                             *
 ********************************************/

 char scan(void)                            // scan keypad matrix
 { char keyndx = 16;                        // null (no switch pressed)
   for(char col = 0; col < 4; col++)        // column 0..3
   { DDRD = bitmask(col);                   // set 'col' bit to output, all others inputs
     PORTD = ~(bitmask(col));               // set 'col' bit to '0', pull-up all other pins
     for(char row = 0; row < 4; row++)      // rows 0..3
     { if(PIND & bitmask(row + 4) == 0)     // if active low key press (row bit == '0')
         keyndx = row * 4 + col;            // save index, 0..15
     }                                      // next row
   }                                        // next col
   return(keymap[keyndx]);                  // return 0 or ASCII keycode
 }
User avatar
banedon
Posts: 742
Joined: 08 Sep 2013
Location: A missile silo somewhere under southern England

Re: Keypad for use with VIA

Post by banedon »

It's very odd. I've slowed down the speed of the AVR and also injected a 10ms and then 100ms delay between the row changes. If I do this then you can see the changes on the other side of the diode, but the AVR doesn't pick it up.
If I desolder the wire from the anode side of the diode and put it on the cathode it then all works for that row. Might be something to do with current pull as I measure a draw of 60-80uA on the row when it's working? Not sure.

As for your idea with regard to the key matrix array - I did think of this initially, but I don't get along well with C and so was having some real issues trying to implement certain things - arrays being one of them. Now that you've kindly provided me with an example, I'll give it another go :).

[edit] or maybe the prop delay introduced by the diodes..... I'll try sticking a delay in between the row going low and the column checks...
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Keypad for use with VIA

Post by BigEd »

Are you sure your inputs are set to be inputs and are not driving a value?
User avatar
banedon
Posts: 742
Joined: 08 Sep 2013
Location: A missile silo somewhere under southern England

Re: Keypad for use with VIA

Post by banedon »

BigEd wrote:
Are you sure your inputs are set to be inputs and are not driving a value?
Don't think so. I've double checked the wiring and this is my defintions for the ports/pins on the AVR:

Code: Select all

	// 0=input, 1=output
	
	// rows (PD pins 4-7), columns PD, pins 0-3)
	DDRD = 0b11110000;			// port D, set to 4-7 (output), 0-3 (input)
	PORTD = 0xff;				// port D, pins 4-7 output 1111, pull ups enabled for pins 0-3
	
	// VIA pins (lower nibble=PC, pins 3 to 0 (reversed), upper nibble=PB6 (bit4), PB2 (bit5), PB1 (bit6), PB0 (bit7)  
	DDRC = 0xff;				// port C, set to output
	PORTC = 0x0;				// port C, pull ups disabled
	DDRB = 0xff;				// port B, set to output
	PORTB = 0;					// port B, pull ups disabled
The "good" news is that I now get the short6 circuit issue when more than one key is pressed when they're on different rows :mrgreen:.
I'll put in the tristating for the rows.

[edit]

Here we go. This works fine and no need for those diodes. Still puzzled as to why they don't work though... :?

Code: Select all

unsigned char scanKbd(void)
{
	// ensure that 
	//PORTD = 0xff;
	unsigned char scanresult;
	
	scanresult = 0;

	for (int row=4;row<=7;row++)
	{
		KYBD_bit_set(DDRD, KYBD_bit(row));					// set row as output 
		KYBD_bit_clear(PORTD, KYBD_bit(row));
		for (int col=0;col<=3;col++)
		{
			if (KYBD_bit_get(PIND, KYBD_bit(col))==0)
			{
				KYBD_bit_set(scanresult, KYBD_bit(col));
				KYBD_bit_set(scanresult, KYBD_bit(row));
			}
		}
		KYBD_bit_set(PORTD, KYBD_bit(row));
		KYBD_bit_clear(DDRD, KYBD_bit(row));					// set row as input (disable it)
	}
	return(scanresult);
}
User avatar
banedon
Posts: 742
Joined: 08 Sep 2013
Location: A missile silo somewhere under southern England

Re: Keypad for use with VIA

Post by banedon »

Just received some of these in the post. No wonder they are so cheap. The awesome tactile feel reminds me of the original ZX80... :roll: :wink: .

As discussed previously; I wish I could find a decent keypad which use buttons and are 5x5 or 6x6... :(
cheapkeypad.jpg
cheapkeypad.jpg (15.59 KiB) Viewed 624 times
Tor
Posts: 597
Joined: 10 Apr 2011
Location: Norway/Japan

Re: Keypad for use with VIA

Post by Tor »

banedon wrote:
As discussed previously; I wish I could find a decent keypad which use buttons and are 5x5 or 6x6... :(
There's this great site where you can find all kinds of info.. 6502.org! :) Specifically, this thread: viewtopic.php?f=4&t=3235

If you enter 'white keyboard metal panel machine pushbutton' in Ebay's search you should see a bunch of them. No 6x6 though. I bought a couple of those after seeing chessdoger's post and his design pages. They feel really nice and solid.
User avatar
banedon
Posts: 742
Joined: 08 Sep 2013
Location: A missile silo somewhere under southern England

Re: Keypad for use with VIA

Post by banedon »

Dammit I tried ebay, lol! Thanks Tor. I'll investigate when I get home this evening. :)
Christoffer
Posts: 7
Joined: 07 Oct 2015

Re: Keypad for use with VIA

Post by Christoffer »

This is pretty much exactly what I've been looking for!
- Is it possible to program the atmega328 by programming the .hex file (/debug folder) to it with a universal device programmer (Eproms, mpu's, etc)? - Or would you have to compile a proper .hex dump with Atmel studio?

--Christoffer
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Keypad for use with VIA

Post by GARTHWILSON »

Quote:
One thing: I can't seem to use the diodes. The rapid pulsing of the rows isn't translated throught the 1N4848 diodes - I get a very shallow square wave ripple on the other side. Anyone got any suggestions on a different type? If not, I'll try and slow down the scanning to see if that helps (and yes, I have the diodes the correct way round :p).
1N4148 diodes are plenty fast, but they drop a lot more voltage than Schottky diodes do; so depending on the configuration, it may barely be pulling down to a valid logic-0. Put the diodes on the outputs to keep them from getting shorted together and fighting if two keys are pressed that would otherwise short outputs together. Don't put them on the inputs. What's the value of your pull-up resistors?
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?
User avatar
banedon
Posts: 742
Joined: 08 Sep 2013
Location: A missile silo somewhere under southern England

Re: Keypad for use with VIA

Post by banedon »

Christoffer wrote:
This is pretty much exactly what I've been looking for!
- Is it possible to program the atmega328 by programming the .hex file (/debug folder) to it with a universal device programmer (Eproms, mpu's, etc)? - Or would you have to compile a proper .hex dump with Atmel studio?

--Christoffer
You should be able to use that hex file. If you have any problems I'm sure that I can dig out my code and recompile it for you.
Let me know.
Post Reply