1

I'm very much new to Microcontroller programming.

I'm using

  • MplabX v3.26 as IDE
  • XC16 compiler
  • PICKit 3
  • p33EP256MU810 (dspic)

for programing

I have written very simple program to blink LED and send few characters over UART, please refer to following source code:

#include <p33Exxxx.h>
#include <p33EP256MU810.h>
#include <libpic30.h>
#include <uart.h>
#include <stdlib.h>
#include <stdio.h>
#include <pps.h>
#include <xc.h>
#include <stdint.h>

// Configuration settings
_FOSC(FNOSC_FRCPLL);
_FWDT(FWDTEN_OFF);

int main() 
{    
//make all pins digital  
PADCFG1 = 0xFFFF; 

//set direction
TRISCbits.TRISC2 = 0;

//Initialze UART1
iPPSOutput(OUT_PIN_PPS_RP68, OUT_FN_PPS_U1TX);

//close UART
CloseUART1();

//open UART
OpenUART1(  UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_NO_PAR_8BIT & UART_1STOPBIT,
            UART_TX_ENABLE & UART_INT_TX & UART_ADR_DETECT_DIS, 
            15);

while (1) 
{
    //turn on led 
    LATCbits.LATC2 = 1;
    __delay32(3750000);    // ~1 sec delay
    //turn off led
    LATCbits.LATC2 = 0;
    __delay32(3750000);    // ~1 sec delay


    //Transmit data
    while(BusyUART1());     //Wail till available
    WriteUART1(0x55);
    WriteUART1(0xaa);
    while(BusyUART1());     //Wail till all bytes sent
}

return 0;
}

LED blinking part works perfectly, but I'M receiving garbage characters on other end of UART where I'm using serial monitor tool (X-CTU) to monitor data.

My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong.

Thank you

Paul R
  • 208,748
  • 37
  • 389
  • 560
amg
  • 127
  • 1
  • 3
  • 12

2 Answers2

1
  1. Clear ANSELx register to set pins digital, instead of writing to PADCFG1
  2. Play with baud rate. Last parameter in procedure OpenUART1 specifies U1BRG value according to dsPic tool description page 147. You can also setup baud rate manually adding U1BRG = value; right after executing OpenUART1. What value pass to UxBRG depends on clock speed and desired baud rate, more details in manual for USART page 9. Notice, that if setup baud rate manually need also set or clear U1MODE.BRGH bit.
1

"My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong."

I don't know what is desired baudrate but:

  1. While making UxBRG calculation, please, take care to clock (oscillator) setup (since you are using _FOSCSEL(FNOSC_FRCPLL) with enabled PLL).

  2. Issue that you've described sounds like a wrong baudrate (including oscillator) calculation, or it could be that port setup is wrong (databits, parity, stopbits).

Hope this helps...

Darko Djuric
  • 754
  • 4
  • 10