Hello,
I have a program up and running and displaying a button on the touch screen, but I'm having some issues trying to get turn the LED on/off with the button. Basically one press should turn the led on and the second press turn it off assuming it's initialy off. Below is the code, I tried doing different configurations, but I think I might be having some denouncing issues as the led sometimes blinks and turns on and stays on.
Any help will be appreciated. I think it would be the same idea if a mechanical switch was being used, but I have not been able to accomplish that either.
I have a program up and running and displaying a button on the touch screen, but I'm having some issues trying to get turn the LED on/off with the button. Basically one press should turn the led on and the second press turn it off assuming it's initialy off. Below is the code, I tried doing different configurations, but I think I might be having some denouncing issues as the led sometimes blinks and turns on and stays on.
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
WORD updateBTN1 = 0;
int main(void){
int BTN1st = 0;
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();
char val[10];
double voltAVG;
int i;
GOL_MSG msg; // GOL message structure to interact with GOL
/****************************************************************
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();
//initADC (ADCIN);
while(1)
{
if (GOLDraw()) { // Draw GOL object
TouchGetMsg(&msg); // Get message from touch screen
GOLMsg(&msg); // Process message
}
[COLOR="red"]if (updateBTN1 && !BTN1st) //This is where I'm having problems
{
__delay_ms(20);
TRISDbits.TRISD3 = 0;
LATDbits.LATD3 = 1;
BTN1st = 1;
}
else if (updateBTN1 && BTN1st)
{
__delay_ms(20);
TRISDbits.TRISD3 = 0;
LATDbits.LATD3 = 0;
}
}
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
{
updateBTN1 = 1;
}
}
return 1;
}
[/COLOR]
WORD GOLDrawCallback(){
return 1;
}
Any help will be appreciated. I think it would be the same idea if a mechanical switch was being used, but I have not been able to accomplish that either.