Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Programming pic33FJ128MC804 to transmit message through Xbee module

Not open for further replies.


May 6, 2023
Reaction score
Trophy points
Activity points
I am trying to program my MCU such that it transmits a message through an xbee module connected to the MCU and the message is received to the XCTU command console. I do not see any message in the XCTU console. I have written the code and properly configured both the xbee modules(I think). The code is following:
#include <p33FJ128MC804.h>
// DSPIC33FJ128MC804 Configuration Bit Settings

// 'C' source line config statements

// FBS
#pragma config BWRP = WRPROTECT_OFF   // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH      // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM       // Boot Segment RAM Protection (No Boot RAM)

// FSS
#pragma config SWRP = WRPROTECT_OFF   // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH      // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM       // Secure Segment Data RAM Protection (No Secure RAM)

// FGS
#pragma config GWRP = OFF        // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF        // General Segment Code Protection (User program memory is not code-protected)

#pragma config FNOSC = PRI       // Oscillator Mode (Primary Oscillator (XT, HS, EC))
#pragma config IESO = ON        // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)

#pragma config POSCMD = XT       // Primary Oscillator Source (XT Oscillator Mode)
#pragma config OSCIOFNC = OFF      // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON       // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSDCMD      // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)

#pragma config WDTPOST = PS32768    // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128      // WDT Prescaler (1:128)
#pragma config WINDIS = OFF       // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = ON       // Watchdog Timer Enable (Watchdog timer always enabled)

#pragma config FPWRT = PWR128      // POR Timer Value (128ms)
#pragma config ALTI2C = OFF       // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON        // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity)
#pragma config HPOL = ON        // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON       // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset)

#pragma config ICS = PGD1        // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF       // JTAG Port Enable (JTAG is Disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define BRGVAL ((FCY/BAUDRATE)/16)-1
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
//#include <uart.h>
#include <xc.h>
  char uart1_buffer[100];
  int uart_index = 0;
int i;
int main(void)
    __builtin_write_OSCCONL(OSCCON & 0xbf);
//    RPOR10bits.RP20R = 3; // UART1 TX @ RP20(RC4)
    __builtin_write_OSCCONL(OSCCON | 0x40);
//    __delay32(1000000)
//    TRISCbits.TRISC4 = 0; // UART1 TX, RP20 is device RC4
//    UART1Init();
//    UART1TransmitString("aaAa\r\n");
    char message[5] = {'a', 'a', 'A', 'a', 'A'};
//    __delay_ms(500);
    return 0;
void initU1(void)
    U1MODEbits.STSEL = 0;
    U1MODEbits.PDSEL = 0;
    U1MODEbits.ABAUD = 0;
    U1MODEbits.BRGH = 0;
    U1STAbits.UTXISEL0 = 0;
    U1STAbits.UTXISEL1 = 0;
    IEC0bits.U1TXIE = 1;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 1;
    U1TXREG = 'a';

void __attribute__((__interrupt__))_U1TXInterrupt(void)
  IFS0bits.U1TXIF = 0;
  U1TXREG = 'a';
Am I missing any step in configuring anything or is my code wrong? I want to know why the xbee module connected to the laptop doesn't show any message in the XCTU console command.

I suspect this is related to a question in the Microchip forum -
Over there, the general advice was to avoid using interrupts at this stage (although what you have shown her is very close to what is provided in the UART FRM section for your MCU).
That aside, what version of the XC8 compiler are you using? The reason I ask is that you should not be including the "p33FJ128MC804.h" wile. The inclusion of the "xc.h" file later on is the correct one.
Also, as suggested in the other forum, why not connect your MCU directly to a PC and not via the XBee3? A direct connection will let you know if the MCU is working correctly and not have a question about the XBee3.
Your code has commented out the PPS line to connect the Tx signal to a GPIO pin - you need that.
You have the #pragma IOL1WAY set to 'ON' which means that the line where you lock the PPS registers means that you cannot unlock them again without a reset. At this stage I suggest that you set IOL1WAY to 'OFF' and don't try to unlock/lock the PPS registers - they are unlocked by default.
I can't see where you define FCY and BAUDRATE as you calculate BRGVAL. These are vital in getting the UAT baud rate setting correct. Also make sure that they care actually calculating the correct value - the integer divisions you are using can cause issues if you are not careful. Also be careful of the default 'int' size: normally you need to specify the FCY value as 'unsigned long'.
In a similar vein, you are using an external XT crystal - what frequency is it? That will also help you calculate the correct FCY value.
Frankly, to get things going, I recommend that you hand calculate the value to go into U1BRG. Once that is working, you can start to 'generalise' the calculation.
Finally, for the #pragma settings, remember that you don't need to set any that default the way you want them. For example you may want the PWM settings later on but not now.
I've just seen that you have the watchdog set to on - DON'T. Keep it off.
Not open for further replies.

Part and Inventory Search

Welcome to