Overview
The UART is a simple serial communication interface exists in most of micro-controllers. Its protocol is very simple. A build-in UART module of a micro-controller offer a high speed, configurable and reliable data communication. This module doesn't exist in some low-end 8-bit micro-controller for example the PIC16F54 or the popular PIC16F84A. However the programmer can emulate this communication protocol in firmware such Assembly or even embedded C programming language (Software UART) due to its clarity. Some C compilers have their software UART allowing the user access to their build-in function libraries.
![]() |
| UART Received ASCII Data |
This communication interface is used to interact between micro-controller to microcontroller due to its two data wire - Transmit (TX) and Receive (RX). Some peripheral device such as GSM module, Wi-Fi modem (ESP8266), or HMI use UART.
MikroC for dsPIC UART Library
MikroC has built-in UART library that supports both hardware and software UART. Hardware UART implements the internal dedicated UART modules of any 16-bit PIC micro-controllers. Some 16-bit PIC micro-controllers have more than one UART interface, UART1, UART2, etc.
These are MikroC Hardware UART Library Routines:
Library Routines
- UARTx_Init
- UARTx_Init_Advanced
- UARTx_Data_Ready
- UARTx_Tx_Idle
- UARTx_Read
- UARTx_Read_Text
- UARTx_Write
- UARTx_Write_Text
- UART_Set_Active
Generic Routines
Now let test the UART1 communication interface of the dsPIC30F2010 using the MikroC UART example.
MikroC Source Code:
- char uart_rd;
- void main() {
- UART1_Init(9600); // Initialize UART module at 9600 bps
- Delay_ms(100); // Wait for UART module to stabilize
- // U1MODEbits.ALTIO = 1; // Un-comment this line to have Rx and Tx pins on their alternate
- // locations. This is used to free the pins for other module, namely the SPI.
- UART1_Write_Text("Start");
- UART1_Write(10);
- UART1_Write(13);
- while (1) { // Endless loop
- if (UART1_Data_Ready()) { // If data is received,
- uart_rd = UART1_Read(); // read the received data,
- UART1_Write(uart_rd); // and send data via UART
- }
- }
- }
In the Library Manger Window, Tick on UART. Then build the source code.
![]() |
| UART in Library Manager |
In MikroC IDE, go to Tools -> USART Terminal to open the interface.
![]() |
| USART Terminal |
Enter any characters and click Send, then the MCU will send back the received data. UART requires a precise micro-controller timing. So don't forget to set a correct clock source and frequency.
Let put a character LCD that displays received ASCII characters from PC Serial Port Terminal.
MikroC Source Code:
- // LCD module connections
- sbit LCD_RS at LATE0_bit;
- sbit LCD_EN at LATE1_bit;
- sbit LCD_D4 at LATE2_bit;
- sbit LCD_D5 at LATE3_bit;
- sbit LCD_D6 at LATE4_bit;
- sbit LCD_D7 at LATE5_bit;
- sbit LCD_RS_Direction at TRISE0_bit;
- sbit LCD_EN_Direction at TRISE1_bit;
- sbit LCD_D4_Direction at TRISE2_bit;
- sbit LCD_D5_Direction at TRISE3_bit;
- sbit LCD_D6_Direction at TRISE4_bit;
- sbit LCD_D7_Direction at TRISE5_bit;
- // End LCD module connections
- char txt1[] = "dsPIC30F2010";
- char txt2[] = "UART Example";
- char txt3[] = "Lcd4bit";
- char txt4[] = "example";
- char i=1;
- char uart_rd;
- void main(){
- ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
- Lcd_Init(); // Initialize LCD
- UART1_Init(9600); // Initialize UART module at 9600 bps
- Delay_ms(100); // Wait for UART module to stabilize
- UART1_Write_Text("Reply for dsPIC30F2010 MCU");
- UART1_Write(10);
- UART1_Write(13);
- Lcd_Cmd(_LCD_CLEAR); // Clear display
- Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
- Lcd_Out(1,3,txt1); // Write text in first row
- Lcd_Out(2,3,txt2); // Write text in second row
- Delay_ms(5000);
- Lcd_Cmd(_LCD_CLEAR); // Clear display
- Delay_ms(10);
- Lcd_Out(1, 1 ,"PC Terminal:");
- while(1) { // Endless loop
- if (UART1_Data_Ready()) { // If data is received,
- uart_rd = UART1_Read(); // read the received data,
- UART1_Write(uart_rd); // and send data via UART
- Lcd_Chr(2, i, uart_rd);
- i++;
- }
- if(i>15) {
- i = 1;
- Lcd_Cmd(_LCD_CLEAR);
- Delay_ms(10);
- Lcd_Out(1 ,1 ,"PC Terminal:");
- }
- }
- }
The LCD shows UART characters, and the MCU will clears it automatically whenever the UART receiving counts reach 15.
Schematic:
![]() |
| Schematic Diagram in Proteus |
dsPIC30F2010 Prototype Board Testing:
![]() |
| Serial Port Terminal |
![]() |
| Testing Program |
![]() |
| UART ASCII Data Receiving |
Using the Sprint Library
The mikroC PRO for dsPIC30/33 and PIC24 provides the standard ANSI C Sprintf function for easy data formatting.
Note : In addition to ANSI C standard, the Sprint Library also includes two
limited versions of the sprintf function (sprinti and
sprintl)
These functions take less ROM and RAM and may
be more convenient for use in some cases.
Functions
Since UART terminal accept only binary ASCII codes. Other data types such as array, integer or floating point numbers could not be directly transmitted over this communication protocol. So additional C function libraries is useful for converting non-ASCII characters into a readable ASCII characters.
MikroC Source Code:
- double ww = -1.2587538e+1;
- char buffer[15];
- void main(){
- UART1_Init(9600); // Initialize UART module at 4800 bps
- Delay_ms(10);
- UART1_Write_Text("Floating point number representation"); // Write message on UART
- sprintf(buffer, "%12e", ww); // Format ww and store it to buffer
- UART1_Write_Text("rne format:"); // Write message on UART
- UART1_Write_Text(buffer); // Write buffer on UART
- sprintf(buffer, "%12f", ww); // Format ww and store it to buffer
- UART1_Write_Text("rnf format:"); // Write message on UART
- UART1_Write_Text(buffer); // Write buffer on UART
- sprintf(buffer, "%12g", ww); // Format ww and store it to buffer
- UART1_Write_Text("rng format:"); // Write message on UART
- UART1_Write_Text(buffer); // Write buffer on UART
- }
We need to set a correct on board clock to 20MHz HS, and also include the UART and SPrint Library in Library Manager.
![]() |
| USART Terminal |








No comments:
Post a Comment