Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

PIC24FV16KM202 UART Setup

1 post / 0 new
Andrew's picture
Last seen: 4 months 1 week ago
Joined: 15/04/2019 - 07:06
PIC24FV16KM202 UART Setup

Can someone please help with me setting up UART on the PIC24FV16KM202 microcontroller. All I am trying to do is send the character 'A' through UART. I am using a Photon from particle for the receiving end. The photon is not receiving anything. I have tested the Photon with an Arduino and a different microcontroller and they both worked. I am just having trouble setting UART up with the PIC24FV16KM202. I have my code below, any help would be much appreciated.

//UART Code

// PIC24FV16KM202 Configuration Bit Settings

// 'C' source line config statements

// FBS
#pragma config BWRP = OFF               // Boot Segment Write Protect (Disabled)
#pragma config BSS = OFF                // Boot segment Protect (No boot program flash segment)

// FGS
#pragma config GWRP = OFF               // General Segment Write Protect (General segment may be written)
#pragma config GCP = OFF                // General Segment Code Protect (No Protection)

#pragma config FNOSC = FRCDIV           // Oscillator Select (8MHz FRC oscillator With Postscaler (FRCDIV))
#pragma config SOSCSRC = ANA            // SOSC Source Type (Analog Mode for use with crystal)
#pragma config LPRCSEL = HP             // LPRC Oscillator Power and Accuracy (High Power, High Accuracy Mode)
#pragma config IESO = ON                // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))

#pragma config POSCMOD = NONE           // Primary Oscillator Configuration bits (Primary oscillator disabled)
#pragma config OSCIOFNC = CLKO          // CLKO Enable Configuration bit (CLKO output signal enabled)
#pragma config POSCFREQ = HS            // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8MHz)
#pragma config SOSCSEL = SOSCHP         // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Both Clock Switching and Fail-safe Clock Monitor are disabled)

#pragma config WDTPS = PS32768          // Watchdog Timer Postscale Select bits (1:32768)
#pragma config FWPSA = PR128            // WDT Prescaler bit (WDT prescaler ratio of 1:128)
#pragma config FWDTEN = ON              // Watchdog Timer Enable bits (WDT enabled in hardware)
#pragma config WINDIS = OFF             // Windowed Watchdog Timer Disable bit (Standard WDT selected(windowed WDT disabled))

#pragma config BOREN = BOR3             // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware, SBOREN bit disabled)
//#pragma config RETCFG = OFF             //  (Retention regulator is not available)
#pragma config PWRTEN = ON              // Power-up Timer Enable bit (PWRT enabled)
#pragma config I2C1SEL = PRI            // Alternate I2C1 Pin Mapping bit (Use Default SCL1/SDA1 Pins For I2C1)
#pragma config BORV = V18               // Brown-out Reset Voltage bits (Brown-out Reset set to lowest voltage (1.8V))
#pragma config MCLRE = ON               // MCLR Pin Enable bit (RA5 input pin disabled, MCLR pin enabled)

#pragma config ICS = PGx1               // ICD Pin Placement Select bits (EMUC/EMUD share PGC1/PGD1)


#include "p24Fxxxx.h"
#include <xc.h>
#define _XTAL_FREQ 3200000000       //sets frequency
#include <libpic30.h>               // __delayXXX() functions macros defined here

#define FP 40000000
#define BAUDRATE 9600
#define BRGVAL ((FP/BAUDRATE)/16)-1

#define _ISR __attribute__((interrupt))
void initUART(){
    U2MODEbits.UARTEN = 0; // Disable the UART2
    U2MODE = 0; // 8 bits & no parity & 1 stop
    U2MODEbits.BRGH = 1; //high baud rate enable bit
    U2BRG = 25; //9600
    U2MODEbits.UEN = 0; // UARTx Enable bits
    IFS1bits.U2RXIF = 0; //Disable reception flag interrupt for UART2
    IFS1bits.U2TXIF = 0; //Disable transmission flag interrupt for UART2
    IEC1bits.U2RXIE = 1; //Enable interruptions for UART2 reception
    IEC1bits.U2TXIE = 1; //Enable interruptions for UART2 transmission
    U2MODEbits.UARTEN = 1; //enable UART2

int main()
    char A;
    CLKDIV = 0;
    ANSB = 0;//PORTB is digital
    TRISB = 0b0100110011011110;
        U2STAbits.UTXEN = 1; //enable UART2
        U2TXREG = A;
        while(U2STAbits.TRMT==0) // wait here till transmit complete
        U2STAbits.UTXEN = 0; //disable UART2
    }//end of while
 return 0;
}//end of main
void __attribute__ ((interrupt,no_auto_psv)) _U2RXInterrupt(void){
    IFS1bits.U2RXIF = 0;   //clear flag
    //U2TXREG = U2RXREG;   

Log in or register to post comments