Serial data transmission from the dsPIC30F4011 UART

The following program is an example of serial data transmission from the dsPIC30F4011 UART. This program repeatedly reads 10-bit analog readings from AN0-AN7 and then prints the results to the serial output. The baud rate is set to 38,400. However, I am seeing occasional glitches in the incoming data. I’m using the internal RC oscillator to clock the PIC, so it’s probable that Fcy (and therefore the baud rate) is slightly off.

So far, I haven’t been able to get the dsPIC to receive serial data. I’ve spent a few frustrating hours trying to get it working, but without success. For the time being, transmission will have to do.

// This program is a UART transmission example for the dsPIC30F4011.
// To compile this, a heap size must be specified because
// printf is used. I set my heap size to 1024 in MPLAB
// as follows:
//	Project->Build Options...->Project->MPLAB LINK30->Heap size = 1024
// Apparently, a heap size of 0 might be ok for printf, but
// for scanf a bigger number may be required (e.g. 1024).
// Written by Ted Burke - last updated 4-12-2010

#include <libpic30.h>
#include <p30f4011.h>
#include <stdio.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

// Function prototypes
void configure_pins();
unsigned int read_analog_channel(int n);

int main()
	// Variables to store analog input voltages
	int v0, v1, v2, v3, v4, v5, v6, v7;

	// Set up which pins are which

		// Read AN0-AN7
		v0 = read_analog_channel(0);
		v1 = read_analog_channel(1);
		v2 = read_analog_channel(2);
		v3 = read_analog_channel(3);
		v4 = read_analog_channel(4);
		v5 = read_analog_channel(5);
		v6 = read_analog_channel(6);
		v7 = read_analog_channel(7);

		// Print the measured voltages on the serial o/p
		printf("AN0=%04d, AN1=%04d, AN2=%04d, AN3=%04d, ", v0, v1, v2, v3);
		printf("AN4=%04d, AN5=%04d, AN6=%04d, AN7=%04d\r\n", v4, v5, v6, v7);

	return 0;

void configure_pins()
	// Configure RD0 as a digital output
	LATD = 0;
	TRISD = 0b11111110;

	// Configure analog inputs
	TRISB = 0x01FF;      // Port B all inputs
	ADPCFG = 0xFF00;     // Lowest 8 PORTB pins are analog inputs
	ADCON1 = 0;          // Manually clear SAMP to end sampling, start conversion
	ADCON2 = 0;          // Voltage reference from AVDD and AVSS
	ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy
	ADCON1bits.ADON = 1; // Turn ADC ON

	// Set up UART
	// Default is 8 data bits, 1 stop bit, no parity bit
	U1BRG = 48;            // 38400 baud @ 30 MIPS
	U1MODEbits.UARTEN = 1; // Enable UART
	U1STAbits.UTXISEL = 1; // interrupt when TX buffer is empty
	U1STAbits.UTXEN = 1;   // Enable TX

// This function reads a single sample from the specified
// analog input. It should take less than 2.5us if the chip
// is running at about 30 MIPS.
unsigned int read_analog_channel(int channel)
	ADCHS = channel;          // Select the requested channel
	ADCON1bits.SAMP = 1;      // start sampling
	__delay32(30);            // 1us delay @ 30 MIPS
	ADCON1bits.SAMP = 0;      // start Converting
	while (!ADCON1bits.DONE); // Should take 12 * Tad = 1.2us
	return ADCBUF0;

I recorded the serial output of this program in the PICkit2 UART tool. Here’s a few lines of what it produced:

AN0=0116, AN1=0079, AN2=0000, AN3=0666, AN4=0466, AN5=0328, AN6=0230, AN7=0162
AN0=0116, AN1=0079, AN2=0000, AN3=0666, AN4=0472, AN5=0330, AN6=0232, AN7=0164
AN0=0114, AN1=0079, AN2=0000, AN3=0666, AN4=0472, AN5=0332, AN6=0234, AN7=0165
AN0=0116, AN1=0079, AN2=0000, AN3=0666, AN4=0472, AN5=0334, AN6=0236, AN7=0168
AN0=0115, AN1=0078, AN2=0000, AN3=0661, AN4=0469, AN5=0333, AN6=0233, AN7=0165
AN0=0117, AN1=0079, AN2=0000, AN3=0661, AN4=0469, AN5=0333, AN6=0237, AN7=0167
AN0=0119, AN1=0081, AN2=0000, AN3=0661, AN4=0469, AN5=0333, AN6=0237, AN7=0167
AN0=0121, AN1=0083, AN2=0000, AN3=0661, AN4=0469, AN5=0333, AN6=0240, AN7=0169
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s