• AVR Freaks

Hot!PIC24F UART not working

Author
jdegeete
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/07/15 05:24:16
  • Location: 0
  • Status: offline
2019/07/15 14:25:35 (permalink)
0

PIC24F UART not working

I am trying to send data from a PIC24FJ1024GB610 using an UART connection and capture the transmitted data on the pc side. Without any luck. Nothing shows up on the pc side. I am puzzled by this and have now come to the point of requiring some help. Since I am completely new to this, I assume I am missing something incredibly obvious.
 
Some context:
PIC24FJ1024GB610
XC16
Explorer 16/32
 
I have connected one - the correct one - of the micro-usb connectors of the explorer board to my pc. On the pc side I have a serial tool running connected to the correct usb with
Baud rate=9600
bits=8
Parity=none
Stop Bits=1
 
My code:
// FOSCSEL
#pragma config FNOSC = FRCPLL
#pragma config PLLMODE = PLL96DIV8
#pragma config IESO = OFF
// FOSC
#pragma config POSCMD = NONE
 
#include <xc.h>
#define FCY 4000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
unsigned int i;
int main(void) {

IFS0bits.U1TXIF = 0; //clear transmit interrupt flag
IEC0bits.U1TXIE = 1; //UART transmitter interrupt enabled
IPC3bits.U1TXIP = 4; //interrupt priority level for transmit

U1BRG = BRGVAL;

U1MODE = 0; //8-bit data, no parity, 1 stop bit

U1STA = 0;
U1STAbits.UTXISEL0 = 0;
U1STAbits.UTXISEL1 = 0;

U1MODEbits.UARTEN = 1; //enable the module
U1STAbits.UTXEN = 1; //enable transmission

/* wait at least 104 usec (1/9600) before sending first char */
for(i = 0; i < 4160; i++) {
Nop();
}

U1TXREG = 'a'; // Transmit one character

while(1) { }

}
 
void __attribute__((__interrupt__)) _U1TXInterrupt(void) {
IFS0bits.U1TXIF = 0; // clear TX interrupt flag
U1TXREG = 'a'; // Transmit one character
}

 
The code is a slightly altered version of the example found in the family reference manual in the section on UART.
I have already verified that the code executes the interrupt.
 
Not sure this has anything to do with the UART problem but this is just for some additional context. If I have my FNOSC, PLLMODE and POSCMD set 'correctly' - which admittedly is a point of worry - I have FOSC=8000000 (FCY=4000000). The thing that worries me about this part is that when I look into p24FJ1024GB610.h it states 'PLLMODE_PLL96DIV8 96 MHz PLL. (32 MHz input)'. But when running a contrived program that blinks my LEDS with an interval of 2 seconds the delay calculated assuming FCY=4000000 seems to be correct. Code that I used to test this:
 
#define FOS 8000000
#define PRESCALER 256
#define IPERS FOS / 2 / PRESCALER
int main(void) {

TRISA = 0;

TMR1 = 0;
T1CON = 0b1000000000110000;

while (1) {

//swith on
LATA = 0x00ff;

TMR1 = 0;
while(TMR1 < IPERS * 2) {}

//switch off
LATA = 0;

TMR1 = 0;
while(TMR1 < IPERS * 2) {}

}

return(0);
}

 
Hope somebody can put me in the right direction.
#1

11 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 09:26:24 (permalink)
    0
    Explorer 16 and "serial via USB" ???
     
    Provided that DS50001589B (for DM240001) is still valid, the only UART connection available is via the RS-232 connector. Might be you have to set some jumpers in addition...

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11413
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 09:38:46 (permalink)
    0
    DS50001589B is the Explorer 16 manual, not the Explorer 16/32 manual.  The latter has on-board USB-serial.
    #3
    rodims
    Super Member
    • Total Posts : 1527
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 09:41:56 (permalink)
    0
    I would assume it's DM240001-2, which has a MCP2221A on-board
    At OP: please confirm.
     
    http://ww1.microchip.com/downloads/en/DeviceDoc/Explorer_16_32_Schematics_R6_3.pdf
     
    USB-Micro J40.
     
    As du00000001 wrote, first check your jumper settings.
    Search all occurances of e.g. J37    (and J38) 
    https://microchipdeveloper.com/boards:explorer1632
     
    In order to send/receive UART RX/TX data bytes to/from the PIM microcontroller, jumpers J37 and J38 should both be capped.
    #4
    du00000001
    Just Some Member
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 09:52:11 (permalink)
    0
    Sorry about the Explorer-16 (which could already accept some early PIC32s) error.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    jdegeete
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/15 05:24:16
    • Location: 0
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 17:33:21 (permalink)
    0
    The part number of my Explorer 16/32 is indeed DM240001-2.


    The usb cable connected to my pc is in the micro-usb J40.


    I can confirm that both J37 and J38 are capped.
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3628
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 19:31:22 (permalink)
    0
    Almost certainly the oscillator is not set up correctly and so the actual baud rate will be out.
    You do not set the PLLMODE config bits which means that they will default to the setting where the PLL is not used (if I've read the data sheet correctly - I only glanced at it really!). Therefore I'm really not sure what happens when you tell the rest of the oscillator configuration to use the FRCPLL. It may be that the oscillator does not work at all!
    Personally, I'd start with just the FRC which you know is 8MHz and go from there to get either the 'flash a LED' or the UART working. After that you can bring in the PLL.
    Note that setting the FCY pre-processor variable does NOT configure anything. Of course you can use it yourself (as you do) but it is only used in the 'delay' functions to let them know the instruction cycle speed ( and remember that it is Fosc/2).
    Susan
    #7
    jdegeete
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/15 05:24:16
    • Location: 0
    • Status: offline
    Re: PIC24F UART not working 2019/07/16 20:32:50 (permalink)
    0
    I have changed the oscillator settings according to the feedback. I was aware that the FCY 'setting' does not configure anything. 
     
    Unfortunately, the change does not solve my UART problem.
     
    My current settings:

    // ================================
    // FOSCSEL
    // ================================
    #pragma config FNOSC = FRC
    #pragma config PLLMODE = DISABLED
    #pragma config IESO = OFF
    // ================================

    // ================================
    // FOSC
    // ================================
    #pragma config POSCMD = NONE
    // ================================

    // ================================
    // FPOR
    // ================================
    #pragma config BOREN = ON
    #pragma config LPCFG = ON
    #pragma config DNVPEN = ENABLE
    // ================================

    // ================================
    // FICD
    // ================================
    #pragma config ICS = PGD2
    #pragma config JTAGEN = OFF
    #pragma config BTSWP = OFF

    #8
    du00000001
    Just Some Member
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC24F UART not working 2019/07/17 01:12:30 (permalink)
    +1 (1)
    Time for some measurements replacing the wild guessing:
    take a scope, set your software to continuously transmit e.g. the character 0x55 and start measuring what's coming out of the controller's TX pin.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #9
    du00000001
    Just Some Member
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC24F UART not working 2019/07/17 01:25:06 (permalink)
    +1 (1)
    Risked an eye on your initial post:
    did you omit something or did you simply skip the act of setting up the I/O / PPS for the UART. This may not be part of he UART FRM but is necessary anyway...

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #10
    jdegeete
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/15 05:24:16
    • Location: 0
    • Status: offline
    Re: PIC24F UART not working 2019/07/17 10:14:49 (permalink)
    0
    That was it! 
     
    These two lines did the trick
    RPINR18bits.U1RXR = 10;
    RPOR8bits.RP17R = 3;

     
    In case somebody else struggles with this, my code:
    // ================================
    // FOSCSEL
    // ================================
    #pragma config FNOSC = FRC
    #pragma config PLLMODE = DISABLED
    #pragma config IESO = OFF
    // ================================
    // ================================
    // FOSC
    // ================================
    #pragma config POSCMD = NONE
    // ================================
    // ================================
    // FPOR
    // ================================
    #pragma config BOREN = ON
    #pragma config LPCFG = ON
    #pragma config DNVPEN = ENABLE
    // ================================
    // ================================
    // FICD
    // ================================
    #pragma config ICS = PGD2
    #pragma config JTAGEN = OFF
    #pragma config BTSWP = OFF

     
    #define FCY 4000000
    #define BAUDRATE 9600
    #define BRGVAL ((FCY/BAUDRATE)/16)-1
    unsigned int i;
    int main(void) {

    RPINR18bits.U1RXR = 10;
    RPOR8bits.RP17R = 3;

    IFS0bits.U1TXIF = 0; //clear transmit interrupt flag
    IEC0bits.U1TXIE = 1; //UART transmitter interrupt enabled
    IPC3bits.U1TXIP = 4; //interrupt priority level for transmit

    U1BRG = 25; //Baud rate set to 9600

    U1MODE = 0; //8-bit data, no parity, 1 stop bit

    U1STA = 0;
    U1STAbits.UTXISEL0 = 0;
    U1STAbits.UTXISEL1 = 0; //interrupt generated transmit is complete

    U1MODEbits.UARTEN = 1; //enable the module
    U1STAbits.UTXEN = 1; //enable transmission

    /* wait at least 104 usec (1/9600) before sending first char */
    for(i = 0; i < 4160; i++) {
    Nop();
    }

    U1TXREG = 'a'; // Transmit one character

    while(1) { }

    }
     
    void __attribute__((__interrupt__)) _U1TXInterrupt(void) {
    IFS0bits.U1TXIF = 0; // clear TX interrupt flag
    U1TXREG = 'a'; // Transmit one character
    }

     
    Thanks for all the help!
    #11
    du00000001
    Just Some Member
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC24F UART not working 2019/07/17 10:25:24 (permalink)
    0
    Hard to believe  wink
     
    Or - as a colleague repeatedly stated (coarse translation):
    Jens Berger
    As soon as you're doing it right, it's working.


    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5