![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| 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);
} THanks. Last edited by dknguyen; 15th November 2007 at 01:26 AM. | |
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) | ||||
| Quote:
Quote:
Quote:
Quote:
| |||||
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) |
| Code: U2TXREG = *Letter++; //load letter into TX buffer Letter++; //move onto next letter 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. --- | |
| |
| | (permalink) |
| 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. | |
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| 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 |