pic16f877a uart problem

Started by siva...@gmail.com November 11, 2008
Hai to all,

Iam trying to communicate from pic 16f877a to the pc's hyperterminal at 9600 baud rate. Iam posting my code for this.
Iam able to simulate with mplab but I could not implement with hardware. I checked for max232, rs232. They seem to be working perfect. But iam not getting the data through hyperterminal comm port.

My crystal oscillator is 8mhz.
/* uart serial communication at 9600 baud rate*/
/*spbrg=(8Mhz/(9600)/16)-1*/

#include
//unsigned char data[6]="fabmax";
unsigned char i;
/*void interrupt isr(void)
{
if(TXIF==1)
{

for(i=0;i<=5;i++)
TXREGa[i]; //transmitted data
}
if(RCIF==1)
{
for(i=0;i<=5;i++)
data[i]=RCREG; //received data
}
}*/
void main (void)
{
// unsigned char data[6]="fabmax";
unsigned char i;
TRISC6=0;
TRISC7=1;
SPBRG=0x33; // for 9600 baoud rate
// GIE=1;
// PEIE=1;
BRGH=1;
SPEN=1; //serial comm enabled
SYNC=0; //synchronous mode disabled
// TXIE=1;
// RCIE=1;
TXEN=1; //transmission enabled
// RCEN=1;
while(1)
{
TXREG='f';
TXREG='z';
}
}

Triple check your settings for the baud rate. I had trouble with that. The bit rate generator has two modes and the calculation is different for each mode.

Mike Harpe

--- On Tue, 11/11/08, s...@gmail.com wrote:

From: s...@gmail.com
Subject: [piclist] pic16f877a uart problem
To: p...
Date: Tuesday, November 11, 2008, 3:24 AM

Hai to all,

Iam trying to communicate from pic 16f877a to the pc's hyperterminal at
9600 baud rate. Iam posting my code for this.
Iam able to simulate with mplab but I could not implement with hardware. I
checked for max232, rs232. They seem to be working perfect. But iam not getting
the data through hyperterminal comm port.

My crystal oscillator is 8mhz.
/* uart serial communication at 9600 baud rate*/
/*spbrg=(8Mhz/(9600)/16)-1*/

#include
//unsigned char data[6]="fabmax";
unsigned char i;
/*void interrupt isr(void)
{
if(TXIF==1)
{

for(i=0;i<=5;i++)
TXREGa[i]; //transmitted data
}
if(RCIF==1)
{
for(i=0;i<=5;i++)
data[i]=RCREG; //received data
}
}*/
void main (void)
{
// unsigned char data[6]="fabmax";
unsigned char i;
TRISC6=0;
TRISC7=1;
SPBRG=0x33; // for 9600 baoud rate
// GIE=1;
// PEIE=1;
BRGH=1;
SPEN=1; //serial comm enabled
SYNC=0; //synchronous mode disabled
// TXIE=1;
// RCIE=1;
TXEN=1; //transmission enabled
// RCEN=1;
while(1)
{
TXREG='f';
TXREG='z';
}
}



to unsubscribe, go to http://www.yahoogroups.com and follow the
instructionsYahoo! Groups Links
Hello,

See my comments below...

--- In p..., siva.gram@... wrote:
>
> Hai to all,
>
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal
at 9600 baud rate. Iam posting my code for this.
> Iam able to simulate with mplab but I could not implement with
hardware. I checked for max232, rs232. They seem to be working
perfect. But iam not getting the data through hyperterminal comm port.
>
> My crystal oscillator is 8mhz.
> /* uart serial communication at 9600 baud rate*/
> /*spbrg=(8Mhz/(9600)/16)-1*/
>
> #include
> //unsigned char data[6]="fabmax";
> unsigned char i;
> /*void interrupt isr(void)
> {
> if(TXIF==1)
> {
>
> for(i=0;i<=5;i++)
> TXREGa[i]; //transmitted data
> }
> if(RCIF==1)
> {
> for(i=0;i<=5;i++)
> data[i]=RCREG; //received data
> }
> }*/
> void main (void)
> {
> // unsigned char data[6]="fabmax";
> unsigned char i;
> TRISC6=0;
> TRISC7=1;
> SPBRG=0x33; // for 9600 baoud rate
> // GIE=1;
> // PEIE=1;
> BRGH=1;
> SPEN=1; //serial comm enabled
> SYNC=0; //synchronous mode disabled
> // TXIE=1;
> // RCIE=1;
> TXEN=1; //transmission enabled
> // RCEN=1;
> while(1)
> {
> TXREG='f';
> TXREG='z';
Above: You are putting data into the TXREG but there is no delay
before you put another character in. You'll need to check the TRMT
bit in the TXSTA register. IE.

while(!TRMT); //Do nothing while waiting for the TSR is still full

Your interrupt routine seems to be spot on to account for that. At
8MHz clock and 9600 Baud it takes approximately 1875 instruction
cycles to transmit one byte (8 data bits + 1 stop bit).
> }
> }
>

Good luck! I've had more than a few problems dealing with RS232, all
because of a strange mistake here and there. Also, double check your
connections to the PC's serial port. I've messed that up before as well.
make sure you turn off handshaking in Hyperterminal.
Jim

----- Original Message ----
From: demolitron
To: p...
Sent: Tuesday, November 11, 2008 11:06:14 AM
Subject: [piclist] Re: pic16f877a uart problem

Hello,

See my comments below...

--- In p..., siva.gram@... wrote:
>
> Hai to all,
>
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal
at 9600 baud rate. Iam posting my code for this.
> Iam able to simulate with mplab but I could not implement with
hardware. I checked for max232, rs232. They seem to be working
perfect. But iam not getting the data through hyperterminal comm port.
>
> My crystal oscillator is 8mhz.
> /* uart serial communication at 9600 baud rate*/
> /*spbrg=(8Mhz/(9600)/16)-1*/
>
> #include
> //unsigned char data[6]="fabmax";
> unsigned char i;
> /*void interrupt isr(void)
> {
> if(TXIF==1)
> {
>
> for(i=0;i<=5;i++)
> TXREGa[i]; //transmitted data
> }
> if(RCIF==1)
> {
> for(i=0;i<=5;i++)
> data[i]=RCREG; //received data
> }
> }*/
> void main (void)
> {
> // unsigned char data[6]="fabmax";
> unsigned char i;
> TRISC6=0;
> TRISC7=1;
> SPBRG=0x33; // for 9600 baoud rate
> // GIE=1;
> // PEIE=1;
> BRGH=1;
> SPEN=1; //serial comm enabled
> SYNC=0; //synchronous mode disabled
> // TXIE=1;
> // RCIE=1;
> TXEN=1; //transmission enabled
> // RCEN=1;
> while(1)
> {
> TXREG='f';
> TXREG='z';
Above: You are putting data into the TXREG but there is no delay
before you put another character in. You'll need to check the TRMT
bit in the TXSTA register. IE.

while(!TRMT); //Do nothing while waiting for the TSR is still full

Your interrupt routine seems to be spot on to account for that. At
8MHz clock and 9600 Baud it takes approximately 1875 instruction
cycles to transmit one byte (8 data bits + 1 stop bit).
> }
> }
>

Good luck! I've had more than a few problems dealing with RS232, all
because of a strange mistake here and there. Also, double check your
connections to the PC's serial port. I've messed that up before as well.
thanks for all,
I was able to transmit and also receive my data to the pc... The problem was with my board as well as with some code changes here and there.

Thanks,
siva

Hai to all,
>
>Iam trying to communicate from pic 16f877a to the pc's hyperterminal at 9600 baud rate. Iam posting my code for this.
>Iam able to simulate with mplab but I could not implement with hardware. I checked for max232, rs232. They seem to be working perfect. But iam not getting the data through hyperterminal comm port.
>
>My crystal oscillator is 8mhz.
>/* uart serial communication at 9600 baud rate*/
>/*spbrg=(8Mhz/(9600)/16)-1*/
>
>#include
>//unsigned char data[6]="fabmax";
>unsigned char i;
>/*void interrupt isr(void)
>{
>if(TXIF==1)
>{
>
>for(i=0;i

My guess is that you are resetting the USART by constantly writing to
TXREG. The uC scan is going to be much faster than the USART shift
register. Your while loop should watch for TXIF to go high and then
load the next character that you want to send. TXIF will work
regardless of whether or not you have interrupts enabled.

Phil

On Nov 11, 2008, at 2:24 AM, s...@gmail.com wrote:

> Hai to all,
>
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal
> at 9600 baud rate. Iam posting my code for this.
> Iam able to simulate with mplab but I could not implement with
> hardware. I checked for max232, rs232. They seem to be working
> perfect. But iam not getting the data through hyperterminal comm port.
>
> My crystal oscillator is 8mhz.
> /* uart serial communication at 9600 baud rate*/
> /*spbrg=(8Mhz/(9600)/16)-1*/
>
> #include
> //unsigned char data[6]="fabmax";
> unsigned char i;
> /*void interrupt isr(void)
> {
> if(TXIF==1)
> {
>
> for(i=0;i<=5;i++)
> TXREGa[i]; //transmitted data
> }
> if(RCIF==1)
> {
> for(i=0;i<=5;i++)
> data[i]=RCREG; //received data
> }
> }*/
> void main (void)
> {
> // unsigned char data[6]="fabmax";
> unsigned char i;
> TRISC6=0;
> TRISC7=1;
> SPBRG=0x33; // for 9600 baoud rate
> // GIE=1;
> // PEIE=1;
> BRGH=1;
> SPEN=1; //serial comm enabled
> SYNC=0; //synchronous mode disabled
> // TXIE=1;
> // RCIE=1;
> TXEN=1; //transmission enabled
> // RCEN=1;
> while(1)
> {
> TXREG='f';
> TXREG='z';
> }
> }
>
>
>
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
One thing I noticed is you aren't waiting for the character to be sent
before sending the next one. You need to create a loop something like
this so that the character has a chance to be sent.
while (TRMT == 0)
;
TXREG='f';
while (TRMT == 0)
;
TXREG='z';

The while loop will stop the processor till the character is sent or
as long as the Transmit Shift Register is full (equal to zero). And,
as has already been mentioned triple check your baud rate.

kmansfie

--- In p..., siva.gram@... wrote:
>
> Hai to all,
>
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal
at 9600 baud rate. Iam posting my code for this.
> Iam able to simulate with mplab but I could not implement with
hardware. I checked for max232, rs232. They seem to be working
perfect. But iam not getting the data through hyperterminal comm port.
>
> My crystal oscillator is 8mhz.
> /* uart serial communication at 9600 baud rate*/
> /*spbrg=(8Mhz/(9600)/16)-1*/
>
> #include
> //unsigned char data[6]="fabmax";
> unsigned char i;
> /*void interrupt isr(void)
> {
> if(TXIF==1)
> {
>
> for(i=0;i<=5;i++)
> TXREGa[i]; //transmitted data
> }
> if(RCIF==1)
> {
> for(i=0;i<=5;i++)
> data[i]=RCREG; //received data
> }
> }*/
> void main (void)
> {
> // unsigned char data[6]="fabmax";
> unsigned char i;
> TRISC6=0;
> TRISC7=1;
> SPBRG=0x33; // for 9600 baoud rate
> // GIE=1;
> // PEIE=1;
> BRGH=1;
> SPEN=1; //serial comm enabled
> SYNC=0; //synchronous mode disabled
> // TXIE=1;
> // RCIE=1;
> TXEN=1; //transmission enabled
> // RCEN=1;
> while(1)
> {
> TXREG='f';
> TXREG='z';
> }
> }
>