Continue to Site

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.

  • 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.

18F4550 - PORTD inputs are shaky, even with internal pull up enabled

Status
Not open for further replies.

toodles

New Member
Below is the entire code Im using; dont let the size fool you, there is very little code. The problem I'm having is in this part:
unsigned char button_status;
Code:
while(1){
    if (!(button_status == PORTD)){  /* if the buttons pressed have changed... */
	Con_Neo_A = Stick_Jab; 		/* update the output pins to match */
	/* update rest of pins here... */
	button_status = PORTD;		/* and save the status to compare again */
    }
    /* update output pins to match input directions */
    }

Every iteration, the button_status does not equal PORTD, even though the is nothing connected; the wires are floating. Im watching it through 'Animate' using an Inchworm. I set the entire port to input and enabled the internal pull-up ( the mInitButtons() macro). Is there something I'm missing that would cause the pins to change inside the MCU? The 'button_status' variable keeps the same value it was last assigned just fine, but PORTD keeps reading different random values; mostly all 1', but with one or two random 0's.

Any help appreciated, thanks for reading.

Code:
/**/

/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>

/** V A R I A B L E S ********************************************************/
#pragma udata

/** P R I V A T E  P R O T O T Y P E S ***************************************/

/** V E C T O R  R E M A P P I N G *******************************************/

/** C O N F I G  D E C L A R A T I O N S *************************************/
#pragma config WDT = OFF, WDTPS = 16384, MCLRE=ON, STVREN=ON, LVP=OFF
#pragma config PLLDIV=5, CPUDIV=OSC1_PLL2, USBDIV=2, FOSC=HSPLL_HS, IESO=OFF 
#pragma config PWRT=OFF, BOR=OFF, BORV=2, EBTRB=OFF, WRTB=OFF, FCMEN=OFF
#pragma config VREGEN=ON, LPT1OSC=OFF, PBADEN=OFF, CCP2MX=ON, XINST=OFF, DEBUG=OFF

/** D E C L A R A T I O N S **************************************************/
#pragma code


#define Stick_Jab			PORTDbits.RD0
#define Stick_Strong		PORTDbits.RD1
#define Stick_Fierce		PORTDbits.RD2
#define Stick_Short			PORTDbits.RD3
#define Stick_Forward		PORTDbits.RD4
#define Stick_Roundhouse	PORTDbits.RD5
#define Stick_Select		PORTDbits.RD6
#define Stick_Start			PORTDbits.RD7
#define Stick_Up			PORTCbits.RC2
#define Stick_Down			PORTAbits.RA4
#define Stick_Left			PORTCbits.RC1
#define Stick_Right			PORTCbits.RC0

/* 	Following is to cement the pins and their output pin # on the D-Sub 15 
	defines to come later
GND					1
RE2					2
RE1					3
RE0					4
RA5 (Serial Select)	5
RA3					6
RA2					7
VCC					8
RB4					9
RB3					10
RB2					11
Serial In (RB0)		12
D- & TX (RC6)		13
D+ & RX/SDO(RC7)	14
Serial Clock (RB1)	15
*/

#define Con_Neo_A		LATCbits.LATC6
#define Con_Neo_B		LATAbits.LATA5
#define Con_Neo_C		LATBbits.LATB0
#define Con_Neo_D		LATEbits.LATE0
#define Con_Neo_E		LATBbits.LATB3
#define Con_Neo_F		LATEbits.LATE2
#define Con_Neo_Start	LATBbits.LATB2
#define Con_Neo_Select	LATEbits.LATE1
#define Con_Neo_Up		LATBbits.LATB1
#define Con_Neo_Down	LATAbits.LATA2
#define Con_Neo_Left	LATCbits.LATC7
#define Con_Neo_Right	LATAbits.LATA3


#define mInitAllLEDs()      TRISAbits.TRISA0=0; TRISAbits.TRISA0=0;
#define mLED_1              LATAbits.LATA0
#define mLED_2              LATAbits.LATA1

#define mLED_1_On()         mLED_1 = 1;
#define mLED_2_On()         mLED_2 = 1;

#define mLED_1_Off()        mLED_1 = 0;
#define mLED_2_Off()        mLED_2 = 0;

#define mLED_1_Toggle()     mLED_1 = !mLED_1;
#define mLED_2_Toggle()     mLED_2 = !mLED_2;

/** S W I T C H *****************************************************/

/* set all 8 input buttons (all of port D) as input, and enable 
	internal pull up resistor                                   */
#define mInitButtons()		TRISD = 0xFF;PORTEbits.RDPU=1;

/* set all four directions as input. No pull up resistor, so pull up 
	needed on the board.										*/
#define mInitStick()		TRISCbits.TRISC0=1;TRISCbits.TRISC1=1;\
							TRISCbits.TRISC2=1;TRISAbits.TRISA4=1;
							
/* Sets all of the pins going to the console as input. Once we 
	identify the console connected to, then we'll set the pins as 
	needed. */
#define mInitConsolePins()	TRISAbits.TRISA2=1;TRISAbits.TRISA3=1; \
							TRISEbits.TRISE0=1;TRISEbits.TRISE1=1; \
							TRISEbits.TRISE2=1;TRISBbits.TRISB2=1; \
							TRISBbits.TRISB3=1;TRISBbits.TRISB4=1; \
							TRISCbits.TRISC6=1;TRISCbits.TRISC7=1; \
							TRISAbits.TRISA5=1;TRISBbits.TRISB1=1; \
							TRISBbits.TRISB0=1;TRISCbits.TRISC4=1; \
							TRISCbits.TRISC5=1;
							
void neogeo(void){
		unsigned char button_status;
	
	
	/* If we're here, we've determined we're using the neo output. */
	/* set the appropriate pins to outputs by clearing the TRIS bit */
	TRISA &= 0b11010011; /* pins A2, A3, A5 */
	TRISB &= 0b11000000; /* B0-B5 */
	TRISC &= 0b00111111; /* C6 and C7 */
	TRISE &= 0b11111000; /* E0-E2 */
	
	
	while(1){
		if (!(button_status == PORTD)){  /* if the buttons pressed have changed... */
			Con_Neo_A = Stick_Jab; 		/* update the output pins to match */
			Con_Neo_B = Stick_Short;
			Con_Neo_C = Stick_Strong;
			Con_Neo_D = Stick_Forward;
			Con_Neo_E = Stick_Fierce;
			Con_Neo_F = Stick_Roundhouse;
			Con_Neo_Select = Stick_Select;
			Con_Neo_Start = Stick_Start;
			button_status = PORTD;		/* and save the status to compare again */
		}
		Con_Neo_Up = Stick_Up;
		Con_Neo_Down = Stick_Down;
		Con_Neo_Left = Stick_Left;
		Con_Neo_Right = Stick_Right;
	}	
	
	
}

void main(void)
{
    ADCON1 |= 0x0F;                 // Default all pins to digital
    mInitButtons();
    mInitStick();
    mInitAllLEDs();
    mLED_1_On();
    while(1)
    {
    	neogeo();   
    }//end while
}//end main
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top