Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 15th November 2007, 01:22 AM   (permalink)
Default DsPIC Hello World Check

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 by dknguyen; 15th November 2007 at 01:26 AM.
dknguyen is offline  
Old 15th November 2007, 04:29 AM   (permalink)
Default

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.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline  
Old 15th November 2007, 04:50 AM   (permalink)
Default

Quote:
Originally Posted by Oznog
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.

Quote:
Originally Posted by Oznog
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).

Quote:
Originally Posted by Oznog
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.

Quote:
Originally Posted by Oznog
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.
dknguyen is offline  
Old 15th November 2007, 06:32 AM   (permalink)
Default

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 by dknguyen; 15th November 2007 at 06:50 AM.
dknguyen is offline  
Old 15th November 2007, 04:57 PM   (permalink)
Default

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?
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline  
Old 15th November 2007, 11:11 PM   (permalink)
Default

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.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
How to check whether it is ZERO or not? Suraj143 Micro Controllers 3 20th September 2007 05:23 AM
How to check a Live Chassis walters General Electronics Chat 5 17th August 2005 08:06 AM
Chip talk dreamproject Electronic Projects Design/Ideas/Reviews 8 2nd April 2005 08:24 PM
Motor control interface dreamproject Micro Controllers 0 31st March 2005 04:48 AM
Real world test measurements? SomeoneKnows General Electronics Chat 8 26th August 2003 07:38 PM



All times are GMT. The time now is 05:41 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker