Hot!LAT won't function as Expected in IF-ELSE Statement

Author
czablow
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/04/14 15:54:50
  • Location: 0
  • Status: offline
2018/09/14 10:59:08 (permalink)
0

LAT won't function as Expected in IF-ELSE Statement

Hello All,
 
I am just attempting to turn and LED on and off by pressing a button. Below is my code.
 
/*
* File: 
* Author: 
*
* Created on August 28, 2018, 12:07 PM
*
* Setting a bit : number |= 1UL << n;
* Clearing a bit : number &= ~(1UL << n);
* Toggling a bit : number ^= 1UL << n;
* Checking a bit : bit = (number >> n) & 1U;
* Changing the nth bit to x: number ^= (-x ^ number) & (1UL << n);
*
*/

/*Load Libraries
*/
#include <xc.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/attribs.h>
#include <string.h>
#include <math.h>
/*Configuration of Device
*/
//#pragma config FWDTEN = 0, POSCMOD = 0x3
//#pragma config FSOSCEN = 0, FNOSC = 0x0
//DEVCFG0
#pragma CP = 0, BWP = 0, ICESEL = 1, DEBUG = 0x3
//DEVCFG1
#pragma config FWDTEN = 0, FCKSM = 0x3, FPBDIV = 0x3, OSCIOFNC = 1
#pragma config POSCMOD = 0x3, IESO = 1, FSOSCEN = 0, FNOSC = 0x0
//DEVCFG2 - NOTHING NEEDED
void oscillatorInitalize(void);
void main(void)
{
oscillatorInitalize();

//WHITE LED
TRISEbits.TRISE2 = 0;
ODCEbits.ODCE2 = 1;

//USER INPUT BUTTON
TRISEbits.TRISE6 = 1;
AD1PCFGbits.PCFG6 = 1;

TRISDbits.TRISD1 = 0;

while(1)
{
if(!PORTEbits.RE6) //USER BUTTON DEPRESSED - ACTIVE LOW
{

LATDbits.LATD1 = 1;
}
else //USER BUTTON PRESSED
{
LATDbits.LATD1 = 0;
}


}

//END
return;
}
void oscillatorInitalize()
{
/*
* Oscillator initialization
*
* PBCLK is SYSCLK divided by 8 (default)
*
*
*/
OSCCONbits.COSC = 0x0;


}
 
 
/////
 
The issue I am having is that the LED is OFF when the button is pressed and ON when the button is depressed. Although my code is supposed to perform the opposite. Now I have been trying to troubleshoot, but nothing seems to be working. The code should turn the LED ON when the button is pressed and OFF when the button is depressed. The funny thing is, if I set the LATDbits.LATD1 outside of the IF-ELSE statement, the code performs as expected. Am I seriously confused or am I doing something wrong? Thanks!
 
PIC32MX795F512L
USB 32-Bit Whacker - PIC32MX795 Development Board
 
post edited by czablow - 2018/09/14 19:30:37
#1

9 Replies Related Threads

    malaugh
    Super Member
    • Total Posts : 342
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 11:16:23 (permalink)
    5 (1)
    What does you hardware look like?  Since ports can sink more current than source, a lot of engineers connect the LED between Vdd and the I/O port, so a logic zero will turn on the LED.
    #2
    czablow
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2017/04/14 15:54:50
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 11:58:41 (permalink)
    0
    My hardware is the USB 32-Bit Whacker - PIC32MX795 Development Board. The output to PORTD1 is a low ohm resistor (not sure the value, under 10k) connected in series with an LED that's connected to common ground.
    #3
    andersm
    Super Member
    • Total Posts : 2466
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 12:25:50 (permalink)
    0
    If you mean this board, then the schematics show that just as malaugh explained, the LEDs are connected so that they are off when the pin is high, and on when it is low.
    #4
    qhb
    Superb Member
    • Total Posts : 7176
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 15:13:39 (permalink)
    0
    If it's this schematic http://www.schmalzhaus.com/UBW32/EE/v26/UBW32_MX795_v26_sch.pdf
    then the on board LEDs are connected to PORTE, so these LEDs on PORTD must be something added by the OP.
    they are using RE6 for the button, which IS the on board "USER" button, and correctly treating it as active low.

    Worst forum problems are now fixed, but the damn firewall is still there.
    #5
    czablow
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2017/04/14 15:54:50
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 19:24:38 (permalink)
    0
    The issue isn't about the on-board LED's, in my attached image, you can see that I have a wire leading out of PORTD1 in series with a resistor and a green LED. The issue I am having is that the green LED is OFF when I press the button and ON when I depress the button. The strangest part is that the LED will turn ON or OFF respective to the LAT value assigned when the statement is placed outside the conditional statements. I am only having the issue of opposite LAT to PORT values when the statement is inside the conditional statements.
     
     
    post edited by czablow - 2018/09/14 19:31:36
    #6
    czablow
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2017/04/14 15:54:50
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 19:26:09 (permalink)
    0
    Here's the image from the previous post

    Attached Image(s)

    #7
    czablow
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2017/04/14 15:54:50
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 19:35:18 (permalink)
    0
    void main(void)
    {
    oscillatorInitalize();


    //USER INPUT BUTTON
    TRISEbits.TRISE6 = 1;
    AD1PCFGbits.PCFG6 = 1;

    TRISDbits.TRISD1 = 0;

    while(1)
    {
    if(!PORTEbits.RE6) //USER BUTTON DEPRESSED - ACTIVE LOW
    {
    //THIS IS THE PROBLEM --- LED SOMEHOW TURNS OFF
    LATDbits.LATD1 = 1;
    }
    else //USER BUTTON PRESSED
    {
    //THIS IS THE PROBLEM --- LED SOMEHOW TURNS ON
    LATDbits.LATD1 = 0;
    }

    //LATDbits.LATD1 = 0; -- THIS WORKS
    //LATDbits.LATD1 = 0; -- THIS WORKS AS EXPECTED
    }

    //END
    return;
    }
    #8
    czablow
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2017/04/14 15:54:50
    • Location: 0
    • Status: offline
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/14 19:45:50 (permalink)
    0
    I got it to work now. Not sure how I didn't understand it before, but maybe I was a bit too tired last night. Thanks for your time guys. Sorry about that!
    #9
    qhb
    Superb Member
    • Total Posts : 7176
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: LAT won't function as Expected in IF-ELSE Statement 2018/09/15 00:38:33 (permalink)
    0
    What was the problem?
     

    Worst forum problems are now fixed, but the damn firewall is still there.
    #10
    Jump to:
    © 2018 APG vNext Commercial Version 4.5