Get Adobe Flash player
FacebookTwitterGoogle+
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

Thomas Alva Edison (1847-1931) was one of the most well known inventors of all time with 1093 patents. During the whole of his life, Edison received only three months of formal schooling, and was dismissed from school as being retarded.
 

Help us stay online:

small donate

dspic uartThis is basic example on how to send and receive data over dsPIC30F UART peripheral. It is easy to understand and if you have not worked with UART on microcontroller before, it is good starting point. In article UART - usage of registers we have explained details of register usage.

We will show how to send charachters, decimal values and strings and how to recive one charachter via UART. You can download whole project for MPLABX and try it at your own solution.

Our hardware platform is descibed in article Microchip dsPIC30f4013 microcontroller and Telit GM862 GSM modem – hardware implementation. Since Telit GM862 GSM modem is obsolete, it is perfect board for educational purposes :) . Aslo we use our FTDI boards described in article USB to Serial Adapter Using FTDI chip. You can see how that looks on picture.

dsPIC UART

As you can notice, for baudrate of UART it is nessecary to change instruction clock rate (FCY) and value of U1BRG register via UBRG1_VALUE and UBRG2_VALUE macros. For more information about dsPIC30F4013 universal asynchronous receiver transmitter module (UART), you can read dsPIC30F4013 datasheet.

Here are complete codes:

uart.c

#include<p30fxxxx.h>
void InitUART1(void);
void WriteUART1(unsigned int data);
void WriteUART1dec2string(unsigned int data);
void WriteStringUART1(const char * s);//for strings
void InitUART2(void);
void WriteUART2(unsigned int data);
void WriteUART2dec2string(unsigned int data);
void WriteStringUART2(const char * s);

uart.c

#include<p30fxxxx.h>
#include <math.h>
#include "uart.h"
#define FCY (unsigned long)7372800   //add your operating frequency
#define UART1_BAUD 115200
#define UART2_BAUD 9600
#define UBRG1_VALUE (FCY/UART1_BAUD)/16 - 1
#define UBRG2_VALUE (FCY/UART2_BAUD)/16 - 1
void InitUART1(void)
{
    U1BRG=UBRG1_VALUE; ///baud rate 115200
    U1MODEbits.ALTIO=0;
    IEC0bits.U1RXIE=1;
    U1STA&=0xfffc;
    U1MODEbits.UARTEN=1;
    U1STAbits.UTXEN=1;
}
void WriteUART1(unsigned int data)
{
    while (U1STAbits.TRMT==0);
    if(U1MODEbits.PDSEL == 3)
        U1TXREG = data;
    else
        U1TXREG = data & 0xFF;
}
void WriteUART1dec2string(unsigned int data)
{
	unsigned char temp;
	temp=data/1000;
	WriteUART1(temp+'0');
	data=data-temp*1000;
	temp=data/100;
	WriteUART1(temp+'0');
	data=data-temp*100;
	temp=data/10;
	WriteUART1(temp+'0');
	data=data-temp*10;
	WriteUART1(data+'0');
}
void WriteStringUART1(const char * s)
{
	while(*s)
            WriteUART1(*s++);
}
/************************************ UART2 *******************************/
void InitUART2(void)
{
	U2BRG = UBRG2_VALUE; // baud rate 9600
	U2STA &= 0xfffc;
	IEC1bits.U2RXIE = 1; // enable RX2 interrupt
	U2MODEbits.UARTEN = 1; // UARTEN: UART Enable bit
	U2STAbits.UTXEN = 1; // transmition ON
}
void WriteUART2 (unsigned int data)
{
	U2TXREG = data;
	while(!U2STAbits.TRMT){}
}
void WriteUART2dec2string(unsigned int data)
{
	unsigned char temp;
	temp=data/1000;
	WriteUART2(temp+'0');
	data=data-temp*1000;
	temp=data/100;
	WriteUART2(temp+'0');
	data=data-temp*100;
	temp=data/10;
	WriteUART2(temp+'0');
	data=data-temp*10;
	WriteUART2(data+'0');
}
void WriteStringUART2(const char * s)
{
	while(*s)
			WriteUART2(*s++);
} 

main.c

#include<p30fxxxx.h>
#include <string.h>
#include <delay.h>
#include <libpic30.h>
#include "uart.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //7.37 MHz Internal RC oscillator, 4x PLL enabled
_FWDT(WDT_OFF);                 //Watchdog disabled
#define DELAY_RANDOM 3000000
int tempRX, tempRX2;
char buf[10];
//-----------------------------------------------------------------
void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
    IFS0bits.U1RXIF = 0;
    tempRX = U1RXREG;
}
void __attribute__((__interrupt__)) _U2RXInterrupt(void) {
    IFS1bits.U2RXIF = 0;
    tempRX2 = U2RXREG;
}
int main(void)
{
    InitUART1();
    InitUART2();
    while (1)
    {
        if ((tempRX > 97) && (tempRX < 122))
        {
            WriteStringUART1("Received letter between small 'a' (97) and 'z' (122), exactly:  ");
            WriteUART1(tempRX);
            tempRX = 13; //Enter ;)
            WriteUART1(tempRX);
        }
        WriteUART1(69); // letter E as Electronics-Base
        __delay32(DELAY_RANDOM); //random - not important
        WriteUART1(13); //13 is Enter
        __delay32(DELAY_RANDOM);
        WriteStringUART1("Electronics-Base on UART1");
        __delay32(DELAY_RANDOM);
        WriteUART1(13); //13 is Enter
        __delay32(DELAY_RANDOM);
        WriteUART1dec2string(1234);
        __delay32(DELAY_RANDOM);
        WriteUART1(13); //13 is Enter
        __delay32(DELAY_RANDOM);
        sprintf(buf, "%d", 1234); //or you can do this way also
        WriteStringUART1(buf);
        __delay32(DELAY_RANDOM);
        WriteUART1(13); //13 is Enter
        __delay32(DELAY_RANDOM);
        WriteUART2(90); // letter Z
        __delay32(DELAY_RANDOM);
        WriteUART2(13); //13 is Enter
        __delay32(DELAY_RANDOM);
        WriteStringUART2("Electronics-Base on UART2");
        __delay32(DELAY_RANDOM);
        WriteUART2(13); //13 is Enter
    }//while
    return 0;
}//main

On following vide you can see results of code execution on two diferent Terminal software.