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 18th December 2007, 01:53 PM   (permalink)
Default PIC18F252 USART problem

Hi,
I am trying to do a small program to get the USART to work. I want to write the letter "A" out on the RS232 port. I have downloaded a sample hex file and loaded into the PIC to test the RS-232 comms to my pc. All is working except my version of code. I am getting funny asterix symbols out the comms port.

Have I left something out of the code. I have set up the registers as per the datasheet and I am only transmitting at the moment.

Code:
#include <p18cxxx.h>

void init_usart(void)		// Initialise the USART
{
	TXSTA=0x24;		// Asynch,8bits,tx enb
	RCSTA=0x90;					
	SPBRG=0x12;		// BaudRate=9600 @ 20Mhz as Table
	RCSTAbits.SPEN=1;	// Enable Serial Port
} 

void write_usart(void)		//Write to the USART
{ 
	while(TXSTAbits.TRMT==1)
	TXREG='A';
}

void main (void)

{

	TRISC=0x80;		//Set RC7=Input, RC0:6 as Output

  	init_usart();		// Initialise the USART
	write_usart();
	
}
Thanks
Mike
mikesmixes777 is offline  
Old 18th December 2007, 02:14 PM   (permalink)
Default

Code:
        SPBRG = 0x12;


Are you sure about that value? Shouldn't it be 0x20?
eng1 is offline  
Old 18th December 2007, 02:23 PM   (permalink)
Default

My bad....
I forgot to take out the 0x

The correct one is
Code:
SPBRG=12;		// BaudRate=9600 @ 20Mhz as Table16-5
The datasheet table says that for 9600 @ 20Mhz with +0.16error SPBRG=12 (decimal)
mikesmixes777 is offline  
Old 18th December 2007, 02:34 PM   (permalink)
Default

Oh, the BRGH bit is set, I missed that, sorry! We must refer to table 16-5, right. The correct value is 129 (0x81) for 9600 baud, isn't it?
eng1 is offline  
Old 18th December 2007, 02:53 PM   (permalink)
Default

ok...i need glasses.... I was looking at the 96Kbps and not 9.6Kbps. (thanks for that!!)

I have changed the SPBRG value to 129 however now i am getting nothing on the RS232, not even the symbols.

im not to sure about my "while" loop... I'm new to C.

Code:
#include <p18cxxx.h>

void init_usart(void)		// Initialise the USART
{
	TXSTA=0b00100100;	// Asynch,8bits,tx enb
	RCSTA=0b10010000;					
	SPBRG=129;		// BaudRate=9600 @ 20Mhz as Table
	RCSTAbits.SPEN=1;	// Enable Serial Port
} 

void write_usart(void)		//Write to the USART
{ 
	while(TXSTAbits.TRMT==1)
	TXREG='A';
}

void main (void)

{

	TRISC=0x80;		//Set RC7=Input, RC0:6 as Output

  	init_usart();		// Initialise the USART
	write_usart();
	
}
mikesmixes777 is offline  
Old 18th December 2007, 02:59 PM   (permalink)
Default

Code:
void write_usart(void)     
{ 
    TXREG='A';      // copy 'A' into the buffer
    while(PIR1bits.TXIF==0); // wait until all bits are transmitted
}
eng1 is offline  
Old 18th December 2007, 03:10 PM   (permalink)
Default

Aha!!! Thats it... its working now. I'm getting an infinite amount of A's on the screen.

How would I send a complete word "Hello" to the usart and then stop the usart from sending? Could I use an array?

THanks for your help..!!!
mikesmixes777 is offline  
Old 18th December 2007, 03:18 PM   (permalink)
Default

Quote:
Originally Posted by mikesmixes777
How would I send a complete word "Hello" to the usart and then stop the usart from sending? Could I use an array?
Yes.

Quote:
Originally Posted by mikesmixes777
Aha!!! Thats it... its working now. I'm getting an infinite amount of A's on the screen.
Place a while(1); at the end of the program (inside the main routine).
eng1 is offline  
Old 18th December 2007, 03:27 PM   (permalink)
Default

Thanks. I shall look into the array's....
mikesmixes777 is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Drive 33 Servos With One PIC + USART wschroeder Micro Controllers 45 25th October 2008 10:10 AM
RF receive using PIC USART col_implant Electronic Projects Design/Ideas/Reviews 11 15th November 2007 05:06 PM
JDM programmer problem krvavizmaj Micro Controllers 3 2nd October 2007 09:36 AM
URGENT!! 16F877 & USART & Interrupt & Recieve Problem DaMieN Micro Controllers 0 18th September 2007 11:53 AM
RF tranceiver problem darvaish General Electronics Chat 2 3rd April 2005 08:44 AM



All times are GMT. The time now is 02:35 AM.


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

eXTReMe Tracker