• AVR Freaks

dsPIC33EP512MU810 Digital Read Problem

Author
syntax3
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/01/02 09:23:44
  • Location: 0
  • Status: offline
2020/01/07 05:23:16 (permalink)
0

dsPIC33EP512MU810 Digital Read Problem

Hi everyone,
 
I am new to PIC world so apologies if I say something wrong.
 
I am using dsPIC33EP512MU810 to read RB8 pin and output the digital level read on pin to RB9.
 
You can find my code down below:
 
//----------------CONFIG BITS----------------//

// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)

// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Primary Oscillator (XT, HS, EC) with PLL)
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FOSC
#pragma config POSCMD = EC // Primary Oscillator Mode Select bits (EC (External Clock) Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch will not wait for the PLL lock signal.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)

// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)

// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)

//-------------------------------------------//

#define FCY 70000000UL //Defining Expected Clock frequency for __delay_ functions


#include <xc.h>
#include <libpic30.h> //Delay Function Library

void Initialization(void){
    //----CLOCK SETUP----//

    
    PLLFBDbits.PLLDIV = 54;
    CLKDIVbits.PLLPOST = 0;
    CLKDIVbits.PLLPRE = 0;
    
    //----PORT SETUP----//
    TRISBbits.TRISB9 = 0; //Output B9
    TRISBbits.TRISB8 = 1; //Input
    ANSELB = 0x0000; //Digital IO

 
    
}


int main(void) {
    
    Initialization();

    
    while(1){
            
        if (PORTBbits.RB8 == 1)
        {
            PORTBbits.RB9 = 1;
        }
        else
        {
            PORTBbits.RB9 = 0;

        }
                      
    }
    
    return 0;
}

 
The problem is after I upload my code I measure Vhigh at RB9 no matter what the RB8 pin is. Any help would be appreciated. Thank you. 
#1

11 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3310
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 05:45:29 (permalink)
    5 (1)
    Citing DarioG :)

    Always write to LATx (not to PORTx).


    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 05:46:27 (permalink)
    0
    Latch not port for output.
     
    LATBbits.RB9 = 1; //out = 1
    copied op:
     
     
    LATBbits.LATB2=0;
    post edited by Gort2015 - 2020/01/07 05:51:35

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 05:48:50 (permalink)
    1 (1)
    How are you controlling the RB8 pin?
     
    This is NOT causing your immediate problem, but
    PORTBbits.RB9 = 1;
    should be
    LATBbits.LATB9 = 1;


    Only use PORT registers for input, not output.
    Rather than reading an input first, you should  just try slowly toggling RB9 high and low to confirm that your PIC is actually running.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    syntax3
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2020/01/02 09:23:44
    • Location: 0
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:12:14 (permalink)
    0
    Thank you for your response!
     
    I changed "PORTBbits.RB9 = 1;" to "LATBbits.LATB9 = 1;". 
     
    I toggled  RB9 high and low using "__delay_ms(1000);" and it worked fine so system clock is configured right.
    I am giving 5V to the RB8 pin with a jumper. As I said output of RB9 stays constantly high even when I connect the pin to GND.
    #5
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:13:57 (permalink)
    0
    syntax3
    I am giving 5V to the RB8 pin with a jumper.



    and how are you driving it low?
    Simply disconnecting your jumper is NOT sufficient if you don't have a pulldown resistor!
     
    Edit: Oops, I misread the rest of your sentence. Maybe leave it connected to ground, and measure the voltage on the pin with a meter just to make sure it's working.
     
    What are you programming the PIC with? A PICkit, ICD or Real Ice?
    Do you know how to use it in debug mode?
     
     
    post edited by ric - 2020/01/07 06:17:28

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    du00000001
    Just Some Member
    • Total Posts : 3310
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:18:09 (permalink)
    0
    @ sytax3
    You changed PORTBbits.RB9 = 1;   to   LATBbits.LATB9 = 1;
    What about PORTBbits.RB9 = 0; ?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #7
    syntax3
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2020/01/02 09:23:44
    • Location: 0
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:29:23 (permalink)
    0
    yes I changed all "PORTBbits.RB9" to "LATBbits.LATB9".
     
    I measured the pin 0V while pin is connected to ground.
     
    I am using Pickit3. Yes I know how to use Debug mode. In debug mode it is constantly entering the if statement like its reading the RB8 high.
    #8
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:31:22 (permalink)
    0
    syntax3
    I am using Pickit3. Yes I know how to use Debug mode. In debug mode it is constantly entering the if statement like its reading the RB8 high.


    While you're in debug mode, you can examine the value of the PORTB register yourself in the Watch window.
     
    Please show the exact code you are running now.
    Only cut and paste, do not retype anything.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 06:34:29 (permalink)
    4 (1)
    Comment out RB8, toggle RB9 on/off connected to a 1K resistor and led.
     If that flashes then the input is the problem.
     
    But how have you got 5v connected to RB8?
    Is the pin 5v tolerant?
    post edited by Gort2015 - 2020/01/07 06:38:41

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    syntax3
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2020/01/02 09:23:44
    • Location: 0
    • Status: offline
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 22:06:03 (permalink)
    0
    The code that I am running is:

     
    //----------------CONFIG BITS----------------//
     
    // FGS
    #pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
    #pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
    #pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
     
    // FOSCSEL
    #pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Primary Oscillator (XT, HS, EC) with PLL)
    #pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
     
    // FOSC
    #pragma config POSCMD = EC // Primary Oscillator Mode Select bits (EC (External Clock) Mode)
    #pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
    #pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
    #pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
     
    // FWDT
    #pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
    #pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
    #pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch will not wait for the PLL lock signal.)
    #pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
    #pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
     
    // FPOR
    #pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
    #pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
    #pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
    #pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
     
    // FICD
    #pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
    #pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
    #pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
     
    // FAS
    #pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
    #pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
    #pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
     
    //-------------------------------------------//
     
    #define FCY 70000000UL //Defining Expected Clock frequency for __delay_ functions
     

    #include <xc.h>
    #include <libpic30.h> //Delay Function Library
     
     
     
    void Initialization(void){
    //----CLOCK SETUP----//

    PLLFBDbits.PLLDIV = 54;
    CLKDIVbits.PLLPOST = 0;
    CLKDIVbits.PLLPRE = 0;

    //----PORT SETUP----//
    TRISBbits.TRISB9 = 0; //Output B9
    TRISBbits.TRISB8 = 1; //Input
    ANSELB = 0x0000; //Digital IO
     



    }
     

    int main(void) {

    Initialization();
     

    while(1){


     
    if (PORTBbits.RB8 == 1)
    {
    LATBbits.LATB9 = 1;
    }
    else
    {
    LATBbits.LATB9 = 0;
     
    }
     

    }

    return 0;
    }
     

     
    I checked the registers PORTB has constant value of 0xFFFC. Do you think the problem is on hardware or software configuration bits?
    post edited by syntax3 - 2020/01/07 22:07:22
    #11
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP512MU810 Digital Read Problem 2020/01/07 22:44:09 (permalink)
    0
    syntax3
    I checked the registers PORTB has constant value of 0xFFFC. Do you think the problem is on hardware or software configuration bits?

    That indicates that ALL the PORTB bits are reading high, except for RB0 and RB1, which are PGED3 and PGEC3, so are being used by the debugger.
    What happens if you turn the built in pull down resistors on, by writing 0xFFFF to CNPDB ?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5