pic16f877a uart c code problm

Started by sivaramakrishna 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<pic.h> 
//unsigned char data[6]="fabmax"; 
unsigned char i; 
/*void interrupt isr(void) 
{ 
if(TXIF==1) 
{ 

for(i=0;i<=5;i++) 
TXREGÚta[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'; 
} 
} 
Can you communicate with other serial devices with hyperterminal?

Are you getting anything out of the max232 on your board?

Is your serial cable wired the way you think it is?



In article <raidnUsvbpGT2oTUnZ2dnUVZ_sednZ2d@giganews.com>, 
siva.gram@gmail.com says...
> 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. > > > while(1) > { > TXREG='f'; > TXREG='z'; > }
There is no test here to make sure the data has left before you shove the next value in. Look at the data sheet for the transmit complete flag. The start of the code looks like you were playing with the UART interrupts at one point or another, but that's not going to help you with the above code.
On Nov 11, 8:49�am, "sivaramakrishna" <siva.g...@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<pic.h> > //unsigned char data[6]="fabmax"; > unsigned char i; > /*void interrupt isr(void) > { > if(TXIF==1) > { > > for(i=0;i<=5;i++) > TXREG=data[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'; > > > > } > }- Hide quoted text - > > - Show quoted text -
You need an interupt driven, buffered, handshaking system to get performance. Email me for some code, it's not for a pic but it's easily modified, more for instruction as to what to do and how to do it.
> Iam trying ...
> /*void interrupt isr(void)
/snap/
> }*/
Seems you commented out interrupt routines (ISRs); why did you do that? To make a joke? -- StoneThrower www.dgmicrosys.com
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal at
> 9600 baud rate. Iam posting my code for this.
Try using TeraTerm instead of HyperTerminal: its much more forgiving (see numerous past posts on the subject). In particular HyperTerminal has a tendency to require the modem control signals even when you tell it to ignore them.
> 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.
Have you checked that you are seeing the correct voltages on the PIC's TX line and the MAX232 TX? You can do this by wobbling the port not just sending a character. That way you can be sure that the hardware path is correct before worrying about serial communication. Have you checked that the lead you are using to the PC is correct?
> > My crystal oscillator is 8mhz.
> if(TXIF==1) > { > > for(i=0;i<=5;i++)
The PIC's serial port is only single buffered: you can only put a single character into it each time TXIF is set.
> TXREGÚta[i]; //transmitted data > } > if(RCIF==1) > { > for(i=0;i<=5;i++) > data[i]=RCREG; //received data
Ditto for receive.
> } > }*/ > 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'; > } > }
As others have pointed out you need to wait for TXIF to become set. The basic port configuration looks correct so I'd look at your hardware. You might want to set TX9 and RX9 to zero to ensure that you are dealing with 8 bit data. Andrew
> Iam trying to communicate from pic 16f877a to the pc's hyperterminal at
> 9600 baud rate. Iam posting my code for this.
Try using TeraTerm instead of HyperTerminal: its much more forgiving (see numerous past posts on the subject). In particular HyperTerminal has a tendency to require the modem control signals even when you tell it to ignore them.
> 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.
Have you checked that you are seeing the correct voltages on the PIC's TX line and the MAX232 TX? You can do this by wobbling the port not just sending a character. That way you can be sure that the hardware path is correct before worrying about serial communication. Have you checked that the lead you are using to the PC is correct?
> > My crystal oscillator is 8mhz.
> if(TXIF==1) > { > > for(i=0;i<=5;i++)
The PIC's serial port is only single buffered: you can only put a single character into it each time TXIF is set.
> TXREGÚta[i]; //transmitted data > } > if(RCIF==1) > { > for(i=0;i<=5;i++) > data[i]=RCREG; //received data
Ditto for receive.
> } > }*/ > 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'; > } > }
As others have pointed out you need to wait for TXIF to become set. The basic port configuration looks correct so I'd look at your hardware. You might want to set TX9 and RX9 to zero to ensure that you are dealing with 8 bit data. Andrew
> Iam trying ...
> /*void interrupt isr(void)
/snap/
> }*/
Seems you commented out interrupt routines (ISRs); why did you do that? To make a joke? -- StoneThrower www.dgmicrosys.com
On Nov 11, 8:49�am, "sivaramakrishna" <siva.g...@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<pic.h> > //unsigned char data[6]="fabmax"; > unsigned char i; > /*void interrupt isr(void) > { > if(TXIF==1) > { > > for(i=0;i<=5;i++) > TXREG=data[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'; > > > > } > }- Hide quoted text - > > - Show quoted text -
You need an interupt driven, buffered, handshaking system to get performance. Email me for some code, it's not for a pic but it's easily modified, more for instruction as to what to do and how to do it.
In article <raidnUsvbpGT2oTUnZ2dnUVZ_sednZ2d@giganews.com>, 
siva.gram@gmail.com says...
> 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. > > > while(1) > { > TXREG='f'; > TXREG='z'; > }
There is no test here to make sure the data has left before you shove the next value in. Look at the data sheet for the transmit complete flag. The start of the code looks like you were playing with the UART interrupts at one point or another, but that's not going to help you with the above code.
Can you communicate with other serial devices with hyperterminal?

Are you getting anything out of the max232 on your board?

Is your serial cable wired the way you think it is?



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<pic.h> 
//unsigned char data[6]="fabmax"; 
unsigned char i; 
/*void interrupt isr(void) 
{ 
if(TXIF==1) 
{ 

for(i=0;i<=5;i++) 
TXREGÚta[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'; 
} 
}