I recommend you to use at least 330 ohms resistors on
all the pins used for the keyboard. Otherwise you
could unintentionally damage the ports, for instance
you could inadvertedly put all the pins as ouputs and
make a short-circuit with the switches.
I would scan the keyboard this way:
//PORTA.0:3 OUTPUTS
//PORTA.4:7 INPUTS
unsigned char i;
unsigned char result[4];
DDRA = 0x0F;//set direction (input/output)
PORTA = 0xFF;//activate pull-ups
for(i = 0; i < 4; i++)
{
PORTA = ~(0x01 << i);//put 0s in PORTA.0:3
result[i] = PINA;
}
If you want to save RAM memory (3 bytes) you can do
the following:
//PORTA.0:3 OUTPUTS
//PORTA.4:7 INPUTS
unsigned char result[2];
DDRA = 0x0F;//set direction (input/output)
PORTA = 0xFF;//activate pull-ups
PORTA = ~(0x01 );//put 0 in PORTA.0
result[0] = PINA | 0x0F;
PORTA = ~(0x01 << 1);//put 0 in PORTA.1
result[0] &= ((PINA >> 4) | 0xF0);
PORTA = ~(0x01 << 2);//put 0 in PORTA.2
result[1] = PINA | 0x0F;
PORTA = ~(0x01 << 3);//put 0 in PORTA.3
result[1] &= ((PINA >> 4) | 0xF0);
Then you have to check the bits in 'result' variable,
a 0 means a pressed switch.
This code is just an idea, I haven't tested it yet.
Regards
Jose
--- Eric <erichards@clear.net.nz> escribió:
>
> Hi what a failure the project is.
>
> I was planning to put a 4*4 keyboard on portA of a
> ATmega32, sending F0 to the data direction register
> &
> the data port.
>
> this is the "test" plan, to put 1's on all the rows,
> the loop will look for a 1 on the columns, when it
> finds a 1 the beeper will beep, then I will continue
> to write the code so to put information on the LCD
> from the keyboard after strobbing the rows one by
> one.
>
>
> I got it to work alright on the studio program, it
> loops round looking for a 1 on the columns, but it
> fails when I download it to the micro,
> I have got the port lines going direct to the
> keyboard, should I put say 10K resistors on the
> input
> ports? or use some other hardware ideas?
>
>
>
> From Eric
>
>
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
> AVR-Chat-unsubscribe@yahoogroups.com
>
>
>
>
>
>
___________________________________________________________
250MB gratis, Antivirus y Antispam
Correo Yahoo!, el mejor correo web del mundo
http://correo.yahoo.com.arMessage
Re: [AVR-Chat] The keyboard
2005-02-18 by Jose Fuentes
Attachments
- No local attachments were found for this message.