Need Help with PIC24FJ128GA010 Programming

Status
Not open for further replies.

Legris

Newbie level 6
Joined
Jun 1, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,443
This is my existing code :

PHP:
#include "p24FJ128GA010.h"
#include "system.h"
// Set up configuration bits
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRI )

// Set up user-defined variables

int main(void)
{
	// Set up PortA IOs as digital output
	AD1PCFG = 0xffff;			// Set all shared analog/digital I/O pins to digital
	TRISA = 0x0000;				// Set port A to output as LED driver

	BtnInit();			// Initialize for Button handling 

	while (1) 
	{

		BtnProcessEvents();		// Check Button
		if (_button_press.b4)   // Process the following if Button 4 is pressed and settled
		{	
			PORTAbits.RA0 ^= 1;			// toggle LED 'D3'
			while (_button_press.b4)    // Wait for Button 4 to release
			{	
//			while (BtnIsPressed(4)) {	// Wait for Button 4 to release
//				BtnProcessEvents();
			}
		}
		else (_button_press.b3)
		{
			PORTAbits.RA0 ^= 0;
		}

	}// End of while loop...

}// End of main()...


#include "system.h"
unsigned char _b4_cnt;
unsigned char _b3_cnt;	

BUTTON _button_press;

//store old tris values for button I/O pins

unsigned int b4tris;
unsigned int b3tris;

void BtnInit(void)
{

	TRISDbits.TRISD13=1;	// Set bit 13 of PORTD as input (to sense the logic level of push button 4)
							// Note: RD13 from PORT D is connected to Push Button S4 on the Explore16 board
	TRISDbits.TRISD6=1;		//  Set bit 6 of PORTD as input.
							// Note: RD6 from PORT D is connected to Push Button S3 on the Explorer 16 Board

	_b4_cnt = 128;			// Initialize debounce counter for button 4 to neutral value
	_b3_cnt = 128;
}

void BtnProcessEvents(void)
{

	if (!BUTTON4) {	// The following branch is when BUTTON4 is '0' (i.e. DEPRESSED)
		_b4_cnt++;		// Increment debounce counter
		if (_b4_cnt >= (128 + BUTTON_MAX_DEBOUCE)){	// The following is for BUTTON4 finally settles at '0'
			_b4_cnt = (128 + BUTTON_MAX_DEBOUCE);	// Stick to the maximum value
			_button_press.b4 = 1;		// Set flag for Button 4 to indicate it is depressed 
		}
	}
	else {			// The following branch is when BUTTON4 is '1' (i.e. RELEASED)
		_b3_cnt--;  	// Decrement debounce counter
		if (_b3_cnt <= (128 - BUTTON_MAX_DEBOUCE)){	// The following is for BUTTON4 finally settles at '1'
			_b3_cnt = (128 - BUTTON_MAX_DEBOUCE);	// Stick to the minimum value
			_button_press.b3 = 0;		// Set flag for Button 4 to indicate it is released
		}
	}
}


*HEADER FILES *

#define BUTTON4   PORTDbits.RD13
#define BUTTON3   PORTDbits.RD6

void BtnInit(void);

void BtnProcessEvents(void);

typedef struct tagButton 
{
	unsigned b4:1;
	unsigned b3:1;               
}BUTTON;
extern BUTTON _button_press;

#define	BUTTON_MAX_DEBOUCE 	20

extern unsigned char _b4_cnt;
extern unsigned char _b3_cnt;

#define BtnIsPressed(__btn)	_button_press.b##__btn

#define BtnIsNotPressed(__btn)	!_button_press.b##__btn

Can anyone help me how to get the S4 to turn on the LED and button S3 to turn off the LED.

Please help me guys !
Im really stuck !!

Appreaciate if you would help !
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…