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.

PORT pin state not changin

Status
Not open for further replies.

drkidd22

Member
Hello,

I have a TouchScreen display setup with a simple button using microchip graphics library. When the button is pressed the LED is supposed to turn on when initially off and if the button is pressed again the LED should turn off and so on...The problem I'm having is that the port is not changing state when I press the button, only the text is changing correctly.

Maybe someone can give me a hand. It seems as if the TRISD register was stuck as an input and won't change over to output state. Bellow is the code for the main routine.

Code:
#include "main_BasicLCDsetup.h" 
#include "GDD_Screens.h"
#include "periphDev.h"

extern float voltage_x1000;

// Configuration bits
_CONFIG2(FNOSC_PRI & POSCMOD_XT & IESO_ON & FCKSM_CSDCMD & OSCIOFNC_OFF) // Primary XT OSC with PLL
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF & GCP_OFF & BKBUG_OFF & COE_OFF & ICS_PGx2 & WINDIS_OFF)   // JTAG off, watchdog timer off

int updateBTN1 = 1; //Goes low when BTN1 on display is pressed

int main(void)
{

TRISDbits.TRISD3 = 0;         //Set PORTD pin3 to output.
LATDbits.LATD3 = 1;		// When LOW LED = ON, Initialy OFF

char val[10];    
double voltAVG;
int i;
int ledstat = 0;    //Changes stat when LED is ON/OFF

GOL_MSG msg; // GOL message structure to interact with GOL

	EEPROMInit();   					// initialize EEPROM
    TouchInit();    					// initialize touch screen
	GOLInit(); 							// initialize graphics library &


if(GRAPHICS_LIBRARY_VERSION != EEPROMReadWord(EEPROM_VERSION)){
        TouchCalibration();
        TouchStoreCalibration();
        EEPROMWriteWord(GRAPHICS_LIBRARY_VERSION,EEPROM_VERSION);
    }

 TouchLoadCalibration();

/****************************************************************
Initialize the main screen created with Graphips Display Designer
this function resides in the GDD_Screens.c file which is automatically
generated by the designer.
*****************************************************************/
CreatemainScrn(); 

TRISDbits.TRISD3 = 0;  //Set the PORT D pin 3 to output
LATDbits.LATD3 = 1;		// Set PORT D pin 3 initially high.

while(1)
{

        if (GOLDraw()) {				// Draw GOL object
        	TouchGetMsg(&msg);   		// Get message from touch screen
        	GOLMsg(&msg);        		// Process message
        }

     if (!updateBTN1)
{
			if(!ledstat) 
      {

LATDbits.LATD3 = 0;  // Turn LED ON		
Bar( 25, 135, 55, 150 ); 
SetColor(RED);
		OutTextXY((55)>>1,(275)>>1, "ON"); //x starting point, y starting point
		updateBTN1 = 1;
		ledstat = 1;  //1 indicates LED is ON
	  }

     else
      {  
		LATDbits.LATD3 = 1;  //Turn LED OFF
Bar( 25, 135, 55, 150 ); 
SetColor(YELLOW);
//(x-position of lft top corner, y-position of lft top corner, x-position of rgt btm corner, y-position of rgt btm corner
		OutTextXY((55)>>1,(275)>>1, "OFF");
		updateBTN1 = 1;
		ledstat = 0;  //0 indicates LED is ON
      }
}

	}
}

WORD GOLMsgCallback(WORD objMsg, OBJ_HEADER* pObj, GOL_MSG* pMsg){
WORD objectID;
objectID = GetObjID(pObj);

if (objectID == mainScrn_BTN1) {
		if (objMsg == BTN_MSG_PRESSED) // check if button is pressed

{
			//Debounce the touch screen press
		  	__delay_ms(50);
				if (objMsg == BTN_MSG_PRESSED)
				__delay_ms(50);
	   			updateBTN1 = 0;  //Button has been pressed
}
				}					
return 1;
}	

WORD GOLDrawCallback(){
    return 1;
}
 
Last edited:
Code:
#include "main_BasicLCDsetup.h" 
#include "GDD_Screens.h"
#include "periphDev.h"

extern float voltage_x1000;

// Configuration bits
_CONFIG2(FNOSC_PRI & POSCMOD_XT & IESO_ON & FCKSM_CSDCMD & OSCIOFNC_OFF) // Primary XT OSC with PLL
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF & GCP_OFF & BKBUG_OFF & COE_OFF & ICS_PGx2 & WINDIS_OFF)   // JTAG off, watchdog timer off

int updateBTN1 = 1;			//Goes low when BTN1 on display is pressed

int main(void)
	{
	TRISDbits.TRISD3 = 0;	//Set PORTD pin3 to output.
	LATDbits.LATD3 = 1;		// When LOW LED = ON, Initialy OFF

	char val[10];    
	double voltAVG;
	int i;
	int ledstat = 0;    	//Changes stat when LED is ON/OFF

	GOL_MSG msg; 			// GOL message structure to interact with GOL

	EEPROMInit();			// initialize EEPROM
	TouchInit();			// initialize touch screen
	GOLInit();				// initialize graphics library &
	
	if(GRAPHICS_LIBRARY_VERSION != EEPROMReadWord(EEPROM_VERSION))
		{
		TouchCalibration();
		TouchStoreCalibration();
		EEPROMWriteWord(GRAPHICS_LIBRARY_VERSION,EEPROM_VERSION);
		}

	 TouchLoadCalibration();

	/****************************************************************
	Initialize the main screen created with Graphips Display Designer
	this function resides in the GDD_Screens.c file which is automatically
	generated by the designer.
	*****************************************************************/
	CreatemainScrn(); 

	TRISDbits.TRISD3 = 0;		//Set the PORT D pin 3 to output
	LATDbits.LATD3 = 1;			// Set PORT D pin 3 initially high.

	while(1)
		{

        if (GOLDraw()) 
        	{					// Draw GOL object
        	TouchGetMsg(&msg);	// Get message from touch screen
        	GOLMsg(&msg);   	// Process message
        	}

		 if (!updateBTN1)
			{
			if(!ledstat) 
				{
				LATDbits.LATD3 = 0;  				// Turn LED ON		
				Bar( 25, 135, 55, 150 ); 
				SetColor(RED);
				OutTextXY((55)>>1,(275)>>1, "ON");	//x starting point, y starting point
				updateBTN1 = 1;
				ledstat = 1;  						//1 indicates LED is ON
				}
		 	else
				{  
				LATDbits.LATD3 = 1;  				//Turn LED OFF
				Bar( 25, 135, 55, 150 ); 
				SetColor(YELLOW);
				
				//(x-position of lft top corner, y-position of lft top corner, x-position of rgt btm corner, y-position of rgt btm corner
				
				OutTextXY((55)>>1,(275)>>1, "OFF");
				updateBTN1 = 1;
				ledstat = 0;  						//0 indicates LED is ON
				}
			}

		}
	}

WORD GOLMsgCallback(WORD objMsg, OBJ_HEADER* pObj, GOL_MSG* pMsg)
	{
	WORD objectID;
	objectID = GetObjID(pObj);

	if (objectID == mainScrn_BTN1) 
		{
		if (objMsg == BTN_MSG_PRESSED) // check if button is pressed
			{
			//Debounce the touch screen press
			__delay_ms(50);
			if (objMsg == BTN_MSG_PRESSED)
					__delay_ms(50);
			updateBTN1 = 0;  //Button has been pressed
			}
		}					
		return 1;
	}	


WORD GOLDrawCallback()
	{
	return 1;
	}

Sorry about the edit but I make the tabs line up so we can read it...

All I can suggest at this point is to make sure the PSP is disabled ( parallel port mode, its controlled in the TRISE register)

Ian

I haven't seen this code before.. but " if (!updateBTN1 )".. I take it this is changed on an interrupt somewhere?
 
Last edited:
!updateBTN1 is changed when the button on the display is pressed, is in the GOLMsgCallback(); funcction at the end
 
I think my problem might be that the pin should be an input instead of an output. RD3 is the cathode of the LED, thus will be sinking the current. If you guys have any clue please let me know.
 
No! you are correct.. it needs to be an output (you give the LED a ground) I am going through the data sheet it has something to do with the JTAG configuration.
Sorry about the earlier post.. I assumed pic18 for some reason.

Are you using the pic24 dev board? if so, half the LED's are connected to the JTAG and this needs to be disabled.

Ian

Scratch that sorry that was port A... still looking!! the pin is also the PMPBE so my first though may have been right try disabling the PM in the PMCON register
 
Last edited:
Yes, Most of I did it following microchip's instructions and added other stuff like the touch screen driver and SS1928 driver from another place. Everything is working ok so far, but for this issue that i've encountered. Attached is a zip of the entire project.
 

Attachments

  • BasicLCDsetup.zip
    886.8 KB · Views: 107
There four setting on these chips
All I/O ports have four registers directly associated with the operation of the port, where ‘x’ is a
letter that denotes the particular I/O port:
• TRISx: PORTx Data Direction Control register
• PORTx: I/O Port register
• LATx: PORTx Data Latch register
• ODCx: PORTx Open-Drain Control register
I would say it because its open drain is whats happening

You may want to read this because the data sheet doesn't tell you all you need to set
the ports up. https://www.electro-tech-online.com/custompdfs/2011/05/39711b.pdf

12.2.4 ODC Registers
Each I/O pin can be individually configured for either normal digital output or open-drain output.
This is controlled by the PORTx Open-Drain Control register, ODCx, associated with each I/O
pin. If the ODC bit for an I/O pin is ‘1’, then the pin acts as an open-drain output. If the ODC bit
for an I/O pin is ‘0’, then the pin is configured for a normal digital output (ODC bit is valid only for
output pins). After a Reset, the status of all the bits of the ODCx register is set to ‘0’.
The open-drain feature allows a load to be connected to a voltage higher/lower than VDD on any
desired digital only pins by using external pull-up resistors. The maximum open-drain voltage
allowed is the same as the maximum VIH specification and the minimum is VSS. The ODCx register
setting takes effect in all the I/O modes, allowing the output to behave as an open-drain even
if a peripheral is controlling the pin. Although the user could achieve the same effect by manipulating
the corresponding LAT and TRIS bits, this procedure will not allow the peripheral to operate
in Open-Drain mode (except for the default operation of the I2C™ pins). Since I2C pins are
already open-drain pins, the ODCx settings do not affect the I2C pins. Also, the ODCx settings
do not affect the JTAG output characteristics as the JTAG scan cells are inserted between the
ODCx logic and the I/O.
 
Last edited by a moderator:
I'm still stuck trying to get this going. I noticed that if I modify this lines.
Code:
// if (GOLDraw()) {				// Draw GOL object
     	TouchGetMsg(&msg);   		// Get message from touch screen
      	GOLMsg(&msg);        		// Process message
//     }
which comments out the GOLDraw function then the LED is initially off as it shoud be, but since the GOLDraw function is not being called then there is nothing displayed on the display.
 
Status
Not open for further replies.
Back
Top