3
\$\begingroup\$

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;   
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Check the Alternate Functions of Port B/C section in the datasheet. It shows that for Port C

TOSC2 – Port C, Bit 7 TOSC2, Timer Oscillator pin 2: When the AS2 bit in ASSR is set (one) to enable asynchronous clocking of Timer/Counter2, pin PC7 is disconnected from the port, and becomes the inverting output of the Oscillator amplifier. In this mode, a Crystal Oscillator is connected to this pin, and the pin can not be used as an I/O pin.

TOSC1 – Port C, Bit 6 TOSC1, Timer Oscillator pin 1: When the AS2 bit in ASSR is set (one) to enable asynchronous clocking of Timer/Counter2, pin PC6 is disconnected from the port, and becomes the input of the inverting Oscillator amplifier. In this mode, a Crystal Oscillator is connected to this pin, and the pin can not be used as an I/O pin

Which is likely why you can't output on those pins. There are similar sections for other ports, and a table that shows exactly which signals can override each pin (eg Overriding Signals for Alternate Functions in PB7..PB4).

Take a look through those entries, then if you're still having trouble post your full program including initialisation code.

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Ya I set ASSR bit to zero but too the problem is same. There is no any output on PC7 and PC6. \$\endgroup\$ Commented Oct 16, 2011 at 3:35
  • 1
    \$\begingroup\$ My main.c is pastebin.com/aDcfuKT0 and keyboard.c is pastebin.com/FzX1P0yx I kept PB7 scanning inside comment coz the code works great without PB7. But using PB7 seems the code to be working like hell. \$\endgroup\$ Commented Oct 16, 2011 at 3:38
  • \$\begingroup\$ I am not using PORTC pins but while testing, PC6 and PC7 doesn't show output. 3 pins of PORTC are used for LCD control lines.Other pins are required for external control. My LCD.c is pastebin.com/DfGd8hgb \$\endgroup\$ Commented Oct 16, 2011 at 3:51
  • \$\begingroup\$ where is "initialise()" defined? \$\endgroup\$ Commented Oct 16, 2011 at 3:51
  • \$\begingroup\$ And there I wrote a mistake on question, my keyboard is 8*8 matrix for 64 keys. \$\endgroup\$ Commented Oct 16, 2011 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.