USART in PIC

Thread Starter

sakishrist

Joined Dec 4, 2009
26
Hello,

I am trying to send a simple "Hello World!" message to a terminal running on a PC through the com port of that PC. I still have a problem.

The code I have is:
Rich (BB code):
#include <p18f4620.h>
//#pragma config WDT = OFF
#pragma config MCLRE = OFF
#include <usart.h>
#include <delays.h>
#pragma config PWRT = OFF, WDT = OFF, LVP = OFF
 

void main()
{

unsigned int i;
TRISC = 0x00;
TRISB =0x00;
OSCCONbits.IRCF0 =1;
OSCCONbits.IRCF1 =1;
OSCCONbits.IRCF2 =1;
OSCCONbits.SCS1 =1;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,103);
 
while(1)
{
PORTB=0x00;
for(i=0;i<4000;i++);

PORTB=0xff;
for(i=0;i<4000;i++);

putrsUSART( (const far rom char *)"Hello World!" );
Delay10KTCYx(200);


}

}
I set spbrg to 103 because I used this:
Rich (BB code):
With USART_BRGH_LOW:
baud rate= FOSC / (64 * (spbrg + 1))
I have connected pin 25 to 2 of the COM port and 5 of the COM port to gnd. The problem is that I'm getting some strange characters on the terminal and not the message. What could be the problem here? Could it be the baudrate? (The thing is that when I run it in simulation everything seems to be fine)

Thanks
 

AlexR

Joined Jan 16, 2008
732
Hello,

Could it be the baudrate? (The thing is that when I run it in simulation everything seems to be fine)

Thanks
It could well be but since you don't post a circuit or tell us your clock frequency, your PC baud rate, or for that matter what compiler you are using, we can only guess at the cause of your problem.
 

Thread Starter

sakishrist

Joined Dec 4, 2009
26
Sorry. I have attached the circuit. I am using the internal osc at 8 Mhz. I set the pc baudrate at 1200 (although I have tried others as well) and I use MPLAB IDE with C18.

Thanks
 

AlexR

Joined Jan 16, 2008
732
Ok, we can see it now.

The on chip USART is really designed to drive a RS232 level converter/RS232 dirver chip such as a Max232 to produce the correct RS232 signal levels.

The RS232 standard defines a mark condition as -12Volt and a space as +12Volt.

Your chip output gives a high logic level for a mark and a low for a space.

The PC is pretty tolerant of low level signals and will probably work down to logic levels but if you are going to feed logic level signals into your PC you have to invert the signal. The simplest way would be to use an inverter gate such as 74LS04 or similar. Of course the best way would be to use the proper driver chip such as the Max232.
 

THE_RB

Joined Feb 11, 2008
5,438
You should add a 220 to 470 ohm resistor between the PIC and the DE9 serial plug. That is all you need to drive a PC serial port.

Then you have to stop using the PIC USART and manually generate (bit bang) the serial. This is because the PIC USART does not have an invert option.

There is some working C code here to manually generate the serial and send direct to PC with nothing needed besides a 470 ohm resistor;
http://www.romanblack.com/bitbangserial.htm
 

Thread Starter

sakishrist

Joined Dec 4, 2009
26
I managed to invert the signal but the problem now is with the formula for finding the spbrg. As far as I can see my chip is working at 8Mhz so I used 415 for 1200 baudrate but when I compared the two signals (PC and PIC) with an oscilloscope I found out that 415 works for 3231 baudrate. So is there something wrong with the formula?

I might use the manual method.

Thanks (especially for the link :) )
 
Top