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.

PIC18F252 USART problem

Status
Not open for further replies.

mikesmixes777

New Member
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
 
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)
 
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?
 
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();
	
}
 
Code:
void write_usart(void)     
{ 
    TXREG='A';      // copy 'A' into the buffer
    while(PIR1bits.TXIF==0); // wait until all bits are transmitted
}
 
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 said:
How would I send a complete word "Hello" to the usart and then stop the usart from sending? Could I use an array?
Yes.

mikesmixes777 said:
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).
 
hi, i am currently using PIC16f886, pickit2, MPlab for a project. i editted the codes i obtained from the sample codes taken from the HiTechC folders.

can i know where will the "A" be displayed at? how do we actually do that?

pls help me as this project is really crucial to me and am rather clueless/confused of my next step. solving this current problem of mine is an intermediate step to go to the next step - configuring AT commands for the p16f886 and rs232 and BT module
 
Last edited:
Firstly dont hijack an other persons thread.It doesnt give you any more replies then you would have gotten in one of your own.If you send an "a" to a computer through a serial port it will be displayed on the top left of the screen.If you want to position it then use carriage return spaces etc.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top