Page 2 of 2

Re: Keypad for use with VIA

Posted: Wed Aug 26, 2015 5:37 pm
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
 }

Re: Keypad for use with VIA

Posted: Wed Aug 26, 2015 5:51 pm
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...

Re: Keypad for use with VIA

Posted: Wed Aug 26, 2015 6:08 pm
by BigEd
Are you sure your inputs are set to be inputs and are not driving a value?

Re: Keypad for use with VIA

Posted: Wed Aug 26, 2015 7:29 pm
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);
}

Re: Keypad for use with VIA

Posted: Fri Aug 28, 2015 5:55 pm
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 625 times

Re: Keypad for use with VIA

Posted: Tue Sep 01, 2015 5:54 am
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.

Re: Keypad for use with VIA

Posted: Tue Sep 01, 2015 12:29 pm
by banedon
Dammit I tried ebay, lol! Thanks Tor. I'll investigate when I get home this evening. :)

Re: Keypad for use with VIA

Posted: Wed Oct 07, 2015 9:39 pm
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

Re: Keypad for use with VIA

Posted: Wed Oct 07, 2015 9:53 pm
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?

Re: Keypad for use with VIA

Posted: Wed Oct 07, 2015 10:55 pm
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.