Keypad problem

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have problems with keypad. This is how it's connected.

code:

#include 
#include 
#include 


int main (void)
{
   	DDRA = 0xFF;
	DDRB = 0xFF;
	DDRC = 0xFF;
	DDRD = 0xFF;

	PORTA = 0x00;
	PORTB = 0x00;
	PORTC = 0x00;
	PORTD = 0x00;

		unsigned int cport = 0;
		unsigned char one = 0x06;
		unsigned char onep = 0x86;
		unsigned char two = 0x5B;
		unsigned char twop = 0xDB;
		unsigned char three = 0x4F;
		unsigned char threep = 0xCF;
		unsigned char four = 0x66;
		unsigned char fourp = 0xE6;
		unsigned char five = 0x6D;
		unsigned char fivep = 0xED;
		unsigned char six = 0x7D;
		unsigned char sixp = 0xFD;
		unsigned char seven = 0x07;
		unsigned char sevenp = 0x87;
		unsigned char eight = 0x7F;
		unsigned char eightp = 0xFF;
		unsigned char nine = 0x6F;
		unsigned char ninep = 0xEF;
		unsigned char zero = 0x3F;
		unsigned char zerop = 0xBF;

   while(1)
	{

	PORTA= 0x02;	
	cport = PINC;

	if(cport == 0x01)
		{
		PORTB = two;
		PORTD = 0x01;
		}
	}


}

Ports B and D drives led segment display.

But when I push button 2, nothing happens. No number two on display. Display works and I now how to drive it so reason cannot be display.

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You read PINC but DDRC=0xFF which means the port is setup as an output?

Also which AVR is this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And which chip are you using? You are using pins shared by JTAG, is it turned off? Otherwise those pins are not usable.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry I didn't mentioned that I have atmega 16.
I changed DDRC = 0xFF; to DDRC = 0x00;
JTAG is disabled.

When I connect at the STK500 PORTA to PORTC and PORTB and PORTD to segment display it works. PORTC reads fine what PORTA gives.

But on my PCB it doesn't recognise when I push keypad buttons.

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
But when I push button 2, nothing happens.

No wonder.
You compare the whole port, which have 4 pins floating.
You need to mask only the used pins.

Also you should look on other key matrix examples.
No resistors needed at all.
The outputs are driven low, if the output direction register was set.
So no fighting of different level may occur, if you press more than one key.
And the inputs are with internal pullup.
So they are high, if no key was pressed.

Then to detect the key, you need two passes.
First the rows are driven low and the colums are read in.
Then the columns are driven low and the rows are read in.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I have code below. The problem is that numbers 1, 2, 3 blinks continuously. It seems that if(1<<PINC0 == 1) is always true. But why?

   while(1)
	{

	sbi(PORTA, 0);
	if(1<<PINC0 == 1)
		{
		PORTB = one;
		PORTD = D1;
		_delay_ms(1000);
		}
	cbi(PORTA, 0);

	
	sbi(PORTA, 1);
	if(1<<PINC0 == 1)
		{
		PORTB = two;
		PORTD = D1;
		_delay_ms(1000);
		}
	cbi(PORTA, 1);

	
	sbi(PORTA, 2);
	if(1<<PINC0 == 1)
		{
		PORTB = three;
		PORTD = D1;
		_delay_ms(1000);
		}
	cbi(PORTA, 2);
	
	}

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

if(1<<PINC0 == 1)

That makes little/no sense. If you are trying to read the state of bit 0 of PINC with that line it should read:

if (PINC & (1<<PINC0)) ...

As it stands, because PINC0 is defined as 0 the line you wrote says:

if (1<<0 == 1)

well 1<<0 is 1 so the line says:

if (1 == 1)

I can tell you now that is always true so there effectively is no conditional here.

I'd strongly suggest you read the "bit manipulation 101" thread in Tutorial Forum.

EDIT: new keyboard but now the 0 key is playing up :-(

Last Edited: Thu. Mar 3, 2011 - 11:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is PINC0 defined as, I expect it's 0
so (1<<0) is always 1.
You need something like:

if(PINC &(1<<PINC0))
        {
         blah, blah
        } 

[Edit] Must learn to type faster...[/Edit]

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, I understood my microcontroller book wrong. Thanks.
This is going to right direction.
Now only problem is that when I push buttons, nothing happens :)

Look at my picture in the first post. Am I totally wrong if I think that when setting first bit in PORTA to 1 and push button number 1 it will make contact between PA0 and PC0 and will place PC0 to 1?

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It might not with the 330R resistors in place - have you removed them, as a previous post suggests, yet?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not yet because I'm far away from soldering iron.

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MakeeK wrote:
Not yet because I'm far away from soldering iron.
Might one suggest that you develop prototype circuits on a breadboard before you reach for the soldering iron? :lol:

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I started to make this like this:
http://www.8051projects.net/keyp...

and code (Programming AVR in C for ADC0804):
http://www.8051projects.net/keyp...

When I compile code AVR studio says "../../../../../avr-libc-1.4.6/crt1/gcrt1.S:51: undefined reference to `main'"

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
unsigned char key_code[4][4]={{'C','D','E','F'},
{'B','3','6','9'},
{'A','2','5','8'},
{'0','1','4','7'}};
if ((PINC&0x01)==0x00) i=0;
if ((PINC&0x02)==0x00) i=1;
if ((PINC&0x04)==0x00) i=2;
if ((PINC&0x08)==0x00) i=3;

DDRC =0x0F;

PORTC=0x00;

DDRB =0x00;

PORTB=0x0F; 
pause(10);

j=4;

if ((PINB&0x01)==0x00) j=0;
if ((PINB&0x02)==0x00) j=1; 
if ((PINB&0x04)==0x00) j=2;
if ((PINB&0x08)==0x00) j=3;

sample code of matrix keyboard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Personally don't get why a programmer would use ANY keypad config. that would use half of 2 ports like that ( makes things more complicated than they need to be ). OP you ought to take a look at AVR240 with it's theory and working code. It'll at least help you in thinking about 4x4 keypad code, generally.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1