I am making 16*16 keypad(on PORTB and PORTD) with ATMega32 using proteus 6.9 and AVRstudio5.
My problem is PB7 and PD7 won't work when pull ups on those pins are enabled. Here PD7 is output, so no problem here. But PB7 must be pull up enabled for matrix. so this part of code wont work as expected:
if (!(keyportpin1&(1<<col8)))
{
while (!(keyportpin1&(1<<col8))){}
key+=7;
return key;
}
Next problem:PC6 and PC7 also don't give output in any case. I tried changing SFR and too got no solution.
The full code I am using is:
/*
* keyboard.c
*
* Created: 10/15/2011 10:54:24 AM
* Author: CMANI
*/
#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#define keyportpin1 PINB
#define keyportpin2 PIND
#define keyport1 PORTB
#define keyport2 PORTD
#define keyportdirection1 DDRB
#define keyportdirection2 DDRD
//#define bit_clear(bit) (!(keyportpin&(1<<bit)))
#define col1 PB0
#define col2 PB1
#define col3 PB2
#define col4 PB3
#define col5 PB4
#define col6 PB5
#define col7 PB6
#define col8 PB7
int keyboard(void)
{
keyportdirection1=0x00;
keyportdirection2=0xFF;
keyport1=0xFF;
int key=1;
for(int i=0;i<8;i++)
{
keyport2=0xFF;
keyport2&=~(0x01<<i);
if (!(keyportpin1&(1<<col1)))
{
while (!(keyportpin1&(1<<col1)))
{}
return key;
}
if (!(keyportpin1&(1<<col2)))
{
while (!(keyportpin1&(1<<col2)))
{}
key+=1;
return key;
}
if (!(keyportpin1&(1<<col3)))
{
while (!(keyportpin1&(1<<col3)))
{}
key+=2;
return key;
}
if (!(keyportpin1&(1<<col4)))
{
while (!(keyportpin1&(1<<col4)))
{}
key+=3;
return key;
}
if (!(keyportpin1&(1<<col5)))
{
while (!(keyportpin1&(1<<col5)))
{}
key+=4;
return key;
}
if (!(keyportpin1&(1<<col6)))
{
while (!(keyportpin1&(1<<col6)))
{}
key+=5;
return key;
}
if (!(keyportpin1&(1<<col7)))
{
while (!(keyportpin1&(1<<col7)))
{}
key+=6;
return key;
}
if (!(keyportpin1&(1<<col8)))
{
while (!(keyportpin1&(1<<col8)))
{}
key+=7;
return key;
}
key+=8;
}
return 0;
}