Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DsPIC Hello World Check

Status
Not open for further replies.

dknguyen

Well-Known Member
Most Helpful Member
Code:
/*******************************************************/
/*
/*	EE400 - Interface Assignment 1 - Hello World
/*  Group 27
/*  November 13, 2007
/*
/********************************************************/

#include <p30f4011.h>
#include <p30fxxxx.h>

/**********************/
/* CONFIGURATION BITS
/**********************/
_FOSC(CSW_FSCM_OFF & FRC);				//use internal fast RC oscillator
_FWDT(WDT_OFF);					//disable WDT
_FBORPOR(PBOR_OFF);					//disable brownout
_FGS(CODE_PROT_OFF);					//disable code protection

int main()
{
	/******************/
	/* CONFIGURE UART
	/******************/
	U2MODE = 0b1000000000000000;	//Enable UART2, 8-bit, no-parity, stop bit, everything else disabled
	//U2BRG = 0x002F;		//set baud rate to 9600bps at 7.37MHz (FRC oscillator)
	U2BRG = 47;		//set baud rate to 9600bps at 7.37MHz (FRC oscillator)

	/*********************/
	/* MAIN PROGRAM LOOP
	/*********************/
	char Message[] = " Hello World!";		//string to be sent over UART
	int n;

	do
	{
		for(n = 0; Message[n] != '!'; n++)
		{
   			while(U2STAbits.UTXBF==1);		//make sure TX buffer not full before loading in more data
   			U2TXREG=Message[n++];			//load next character into TX buffer and increment character counter
		}
	} while(1);								//repeat sending "Hello World" over UART forever

	return(0);
}

I got this simple little Hello World code for a dsPIC30F4011 that is supposed to be written in C which I've never done before so some of the definitions and defines in the header files seem a bit strange to me. It's also been 4 year since I've coded any C and I can't test it tonight, but it is due at the end of the lab tommorrow and it'd be nice if someone could see if they can't catch a glaring error in it.

THanks.
 
Last edited:
You MUST set the TRIS register to set the UARTTX pin as an output.

I don't recall the 30F's PLL scheme, but the 33F is more complex and must be set to get a desired freq. Since you've chosen serial comm, freq must be correct to get a coherent output.

FRC has a large variability, in fact your freq may be so inaccurate that you will not be able to read the UART output.

main() should not have a return value. It has nowhere to return to. True, it never gets hit since you've got a while() before it, but then you don't need anything here at all do you?

A string is implicity terminated with a Null (0x00) character. So look for the null, not '!'. Actually we prefer to use a ptr and make the loop like "while(*ptr){sendChar(*ptr); *ptr++;}"

The implementation is far more efficient than having an array index and repeatedly looking up an array item from the offset.
 
Oznog said:
You MUST set the TRIS register to set the UARTTX pin as an output.
Are you sure about this? THe manual says that once the enable bit in the UART register is set it overrides the corresponding TRIS register.

Oznog said:
I don't recall the 30F's PLL scheme, but the 33F is more complex and must be set to get a desired freq. Since you've chosen serial comm, freq must be correct to get a coherent output.

FRC has a large variability, in fact your freq may be so inaccurate that you will not be able to read the UART output.
Yeah, I was worried about this and was hoping it wouldn't be too bigt a deal since it is supposed to be asynchronous. THe problem is we were supposed to do a barebones breadboard of the uC and read out the message from the UART. We don't have a crystal so I had to use the internal RC oscillator (it's fixed...kind of at 7.37MHz).

Oznog said:
main() should not have a return value. It has nowhere to return to. True, it never gets hit since you've got a while() before it, but then you don't need anything here at all do you?
Yeah me and my partners were sitting there wondering if it was needed or not so we put it in.

Oznog said:
A string is implicity terminated with a Null (0x00) character. So look for the null, not '!'. Actually we prefer to use a ptr and make the loop like "while(*ptr){sendChar(*ptr); *ptr++;}"

The implementation is far more efficient than having an array index and repeatedly looking up an array item from the offset.
Ah yeah. I remember now. THanks.
 
I've learned a bit more about the header file and sat down and opened up my old C++ book and all the stuff about pointers and references (the stuff not involving objects and classes anyways) came back to me:

Code:
/*******************************************************/
/*
/*	EE400 - Interface Assignment 1 - Hello World
/*  Group 27
/*  November 13, 2007
/*
/********************************************************/

#include <p30f4011.h>
#include <p30fxxxx.h>

/**********************/
/* CONFIGURATION BITS
/**********************/
_FOSC(CSW_FSCM_OFF & FRC);					//use internal fast RC oscillator
_FWDT(WDT_OFF);								//disable WDT
_FBORPOR(PBOR_OFF);							//disable brownout
_FGS(CODE_PROT_OFF);						//disable code protection

void main()
{
	/******************/
	/* CONFIGURE UART
	/******************/
	U2BRG = 47;								//set baud rate to ~9600bps at 7.37MHz (FRC oscillator)	
	U2MODEbits.STSEL = 0;					//1 stop bit
	U2MODEbits.PDSEL = 0;					//8-bit, no parity
	U2MODEbits.UARTEN = 1;					//enable UART2
	U2STAbits.UTXEN = 1;					//enable UART2 transmitter

	/*********************/
	/* MAIN PROGRAM LOOP
	/*********************/
	char Text[] = "Hello World! ";		//text to be sent over UART
	char* Letter = Text;				//points to next letter to be sent over UART, intialize to first letter of 'Text'

	do
	{
		while(*Letter != '\0');			//continue to send letters until end of text is reached
		{
   			while(U2STAbits.UTXBF==1);		//wait until TX buffer has space before loading data
   			U2TXREG = *Letter++;			//load letter into TX buffer
   			Letter++;				//move onto next letter
		}
		Letter = Text;						//reset letter to be sent to first letter of text
	} while(1);								//repeat sending text over UART forever
}
 
Last edited:
Code:
U2TXREG = *Letter++;			//load letter into TX buffer
Letter++;				//move onto next letter
Wouldn't the output then be:
HloWrd &*^$^%$8?
Because you are incrementing the Letter pointer twice and will skip right over the NULL character at the end?
 
Right, don't increment the letter pointer twice!
U2TXREG = *letter++;

would be the preferred increment. U2TXREG = *letter; would assign U2TXREG without incrementing.

"Yeah, I was worried about this and was hoping it wouldn't be too bigt a deal since it is supposed to be asynchronous."

Actually if it were synchronous, it wouldn't matter because a clock tells you where the bit-times begin and end. This async serial IS a potential problem, it can tolerate SOME freq mismatch but not a whole lot. You may or may not see this but yes people have had this trouble running some serial protocols off of the FRC. You can always look at it with a scope and see if the osc is running at an acceptable freq. If not, use the OSCTUNE reg (well they have one on the 33F, not sure about the 30F) to tune the FRC osc, or just change the UART's BRG reg.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top