UART – DSPIC30F2010

Photo of author

By Jackson Taylor

Home » Electronics Topics » dsPIC Microcontroller » UART – DSPIC30F2010

As an Amazon Associate we earn from qualifying purchases.

UART is one of the simplest ways to get text out of a microcontroller and into a PC terminal. This article shows how to send a string from a dsPIC30F2010 using the UART library in mikroBasic, explains what each line does, and covers the wiring and settings that most often cause problems.

UART TRANSMISSION

The video below shows the example running. The code follows right after it.

What UART does

UART stands for Universal Asynchronous Receiver/Transmitter. It sends data one bit at a time over two signal lines: TX (transmit) and RX (receive). There is no shared clock line, so both devices must agree on the same speed, called the baud rate, before they start talking.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

A common setup is 9600 baud, 8 data bits, no parity and 1 stop bit, often written as 9600 8N1. The example below uses this speed. If you want a broader look at the chip itself first, see the dsPIC30F2010 features page.

Example code: sending text over UART

This program is written for mikroBasic PRO for dsPIC. It starts the UART at 9600 baud, waits briefly, sends one string and then stays in an endless loop.

program UART_TX
dim rd as byte    'uart data variable
main:
UART1_Init(9600)               ' Initialize UART module at 9600 bps
Delay_ms(100)                  ' Wait for UART module to stabilize
UART1_Write_Text("haiii")
while TRUE
wend
end.

How the code works

  • dim rd as byte declares a variable for received data. It is not used in this transmit-only example, but you will need it when you read incoming bytes.
  • UART1_Init(9600) configures the UART module for 9600 baud. The library calculates the baud rate generator value from the oscillator frequency set in your project, so the clock setting in the project must match your real hardware.
  • Delay_ms(100) gives the module a short time to settle before the first byte is sent.
  • UART1_Write_Text(“haiii”) sends the characters of the string one after another.
  • while TRUE … wend keeps the program running so it does not fall off the end of main. Because the string is sent only once, you will see it once after reset.
See also
STM8S003K3 BLINKING IN ARDUINO

If you want the text to repeat, move the write call inside the loop and add a delay, for example half a second, so the terminal does not fill up too quickly.

Wiring the dsPIC30F2010 to a PC

The microcontroller pins work at logic levels, not at the voltage levels of an old PC serial port. You need something between them:

Connect the chip’s TX pin to the adapter’s RX pin, and the chip’s RX pin to the adapter’s TX pin. Always join the grounds as well. The UART pins on the dsPIC30F2010 share their function with other peripherals, so check the pin table in the datasheet to confirm which physical pins to use on your package.

Viewing the output

  1. Build the project and program the chip.
  2. Open a serial terminal on the PC. The mikroBasic IDE has a built-in USART terminal, and any other terminal program works as well.
  3. Select the correct COM port and set 9600 baud, 8 data bits, no parity, 1 stop bit.
  4. Reset the microcontroller. The text haiii should appear in the terminal.

Receiving data

To read what the PC sends, check whether a byte has arrived and then read it. This example echoes each received byte back to the terminal:

program UART_Echo
dim rd as byte
main:
UART1_Init(9600)
Delay_ms(100)
while TRUE
  if UART1_Data_Ready() = 1 then
    rd = UART1_Read()
    UART1_Write(rd)
  end if
wend
end.

Here the rd variable holds each incoming byte. Type a character in the terminal and the same character should come back.

Troubleshooting

  • Garbled characters: this is almost always a baud rate mismatch. Check that the terminal speed matches the code, and that the oscillator setting in the project matches the real crystal or internal oscillator.
  • Nothing appears: check that TX and RX are not swapped the wrong way, that the grounds are connected, and that you opened the right COM port.
  • Only a few characters show up: make sure the terminal was open before you reset the chip, since the string is sent only once.
  • Works on the bench but not on the final board: check the logic voltage of the adapter against the voltage of your circuit.
See also
REGULATORS

Related tutorials

If you are new to this chip, a good first step is to blink an LED to confirm your toolchain and clock settings work. When you have no hardware UART free, a software UART can be an alternative. For comparison with other chips, there is a PIC16F877A UART example and a guide to UART on the 8051.

FAQ

What baud rate should I use?

9600 is a safe starting point because nearly every terminal and adapter supports it. Higher rates work too, but the accuracy depends on your oscillator, so increase the speed only after the basic example works.

Why does the message only appear once?

The string is sent before the endless loop, so it runs once after each reset. Put the write call inside the loop if you want it repeated.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Can I connect the dsPIC30F2010 directly to a PC serial port?

No. RS-232 voltage levels differ from the microcontroller’s logic levels. Use a level converter or a USB-UART adapter that matches your board’s voltage.

Does UART1_Init set the parity and stop bits?

The basic UART1_Init call with only a baud rate uses the library’s default 8N1 format. If you need other settings, check the mikroBasic UART library help for the extended initialisation options.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works