• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic24f problem

Status
Not open for further replies.

fromansr

New Member
Hey guys

I'm trying to interface a 4x4 keypad to the p24fj32ga002 micro but Im getting the wrong output for row 3 on my LCD. I commented out most of my code and tested the row outputs. With every combination of port B pins I try, I get at least 1 pin giving an output of logic low. Does this mean there is problem with my pic? ie. it cant give a logic high (3.3V) for 4 I/O pins of 1 port...

Here is my test code for the Keypad read function (with unused code commented out)....

#include<p24fj32ga002.h>

#define row1 PORTBbits.RB2
#define row2 PORTBbits.RB3
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB6

#define col1 PORTBbits.RB7
#define col2 PORTBbits.RB8
#define col3 PORTBbits.RB9
#define col4 PORTBbits.RB10

void DelayMs(unsigned int N);
void DelayUs(unsigned int N);

char KeyRead(void)
{

LATBbits.LATB2=0;
LATBbits.LATB3=0;
LATBbits.LATB5=0;
LATBbits.LATB6=0;

//row pins are outputs
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
TRISBbits.TRISB5 = 0;
TRISBbits.TRISB6 = 0;

//column pins are inputs
TRISBbits.TRISB7 = 1;
TRISBbits.TRISB8 = 1;
TRISBbits.TRISB9 = 1;
TRISBbits.TRISB10 = 1;

/*check if any keys are being pressed
row1,row2,row3,row4 = 0;
while((col1 || col2 || col3 || col4) == 0)
{ }*/

//set all rows to high
row1 = 1;
row2 = 1;
row3 = 1;
row4 = 1;

/*DelayMs(100);
while(1)
{
//scan keypad for user input
row1 = 0;
if (col1 == 0)
{ return('1'); }
else if (col2 == 0)
{ return('2'); }
else if (col3 == 0)
{ return('3'); }
else
{ row1 = 1; }

row2 = 0;
if (col1 == 0)
{ return('4'); }
else if (col2 == 0)
{ return('5'); }
else if (col3 == 0)
{ return('6'); }
else
{ row2 = 1; }

row3 = 0;
if (col1 == 0)
{ return('7'); }
else if (col2 == 0)
{ return('8'); }
else if (col3 == 0)
{ return('9'); }
else
{ row3 = 1; }

row4 = 0;
if (col2 == 0)
{ return('0'); }
else
{ row4 = 1; }
}*/

return ('N');

}

Can anyone with experience with these pics please help!!

THANKS
 

Noggin

Member
First thing I noticed is that your entire function is commented out, save for return N. Use [ code][/code] tags too, it makes things easier to read. Is it the same pins that are always giving you trouble? Did you disable the JTAG capabilities? Are you using I2C?

I've never done a row/scan before. If you were having column problems it would show up on every row, right? So what you're seeing suggests that JTAG has been disabled, columns are fine, and row selection is bad. Have you put "while (1) row3=1" and verified with a multimeter that all outputs and inputs are behaving as expected? Better yet, use an o-scope if you can.

Code:
#include<p24fj32ga002.h>

#define row1 PORTBbits.RB2
#define row2 PORTBbits.RB3
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB6

#define col1 PORTBbits.RB7
#define col2 PORTBbits.RB8
#define col3 PORTBbits.RB9
#define col4 PORTBbits.RB10

void DelayMs(unsigned int N);
void DelayUs(unsigned int N);

char KeyRead(void)
{

	LATBbits.LATB2=0;
	LATBbits.LATB3=0;
	LATBbits.LATB5=0;
	LATBbits.LATB6=0;

	//row pins are outputs
	TRISBbits.TRISB2 = 0;
	TRISBbits.TRISB3 = 0;
	TRISBbits.TRISB5 = 0;
	TRISBbits.TRISB6 = 0;

	//column pins are inputs
	TRISBbits.TRISB7 = 1;
	TRISBbits.TRISB8 = 1;
	TRISBbits.TRISB9 = 1;
	TRISBbits.TRISB10 = 1;

	/*check if any keys are being pressed
	row1,row2,row3,row4 = 0;
	while((col1 || col2 || col3 || col4) == 0)
	{ }*/

	//set all rows to high
	row1 = 1;
	row2 = 1;
	row3 = 1;
	row4 = 1;

	/*DelayMs(100);
	while(1)
	{
	//scan keypad for user input
		row1 = 0;
		if (col1 == 0)
			return('1');
		else if (col2 == 0)
			return('2');
		else if (col3 == 0)
			return('3');
		else
			row1 = 1;

		row2 = 0;
		if (col1 == 0)
			return('4');
		else if (col2 == 0)
			return('5');
		else if (col3 == 0)
			return('6');
		else
			row2 = 1;
	
		row3 = 0;
		if (col1 == 0)
			return('7');
		else if (col2 == 0)
			return('8');
		else if (col3 == 0)
			return('9');
		else
			row3 = 1;

		row4 = 0;
		if (col2 == 0)
			return('0');
		else
			row4 = 1;
	}*/

	return ('N');

}
 

fromansr

New Member
I tested all the row pins with a multimeter and 1 pin gave a low....

I then assigned LATB to the rows (instead of PORTB) and it seems to be working fine now! :)

Thanks for the help anyway....I thought LATB and PORTB were the same thing:confused:
 

Noggin

Member
LATB and PORTB are not the same thing. However, writing to PORTx will actually cause a write to LATx so it should work.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top