CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with serial communication with 16F877 (PCM)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zico
Guest







Problem with serial communication with 16F877 (PCM)
PostPosted: Thu May 15, 2003 3:14 am     Reply with quote

Hello Everybody.
I'n using now the PIC16F877, I used the same code that's functionning very good in the PIC16F84, but not in the PIC16F877.
NB: The 84 doesn't have any integrated SCI (USART), meanwhile the 877 has one, & there's nothing on the Serial Interface.
I mention here some Code:
#include <stdio.h>
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses XT,NOWDT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#int_TBE
TBE_isr() {}

#int_RDA
RDA_isr() {}

void main() {
char c;
enable_interrupts(INT_TBE);
enable_interrupts(INT_RDA);
enable_interrupts(global);
printf("\r\nPress a key: ");
c=getc();
printf("\r\n You've Pressed: \%c",c);
}


The unique difference is that here There's built-in support for ISR, but not in the 84.

SO, I don't see anything on TX nor on RX pins on the 877.
Should I live this ISR Interrupt Service Routines empty?
10x in advance.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514483
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

Re: Problem with serial communication with 16F877 (PCM)
PostPosted: Thu May 15, 2003 5:25 am     Reply with quote

I've experienced this stupid problem too. Try using two other pins for serial communication (i.e. C1 & C2).

Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514490
TSchultz



Joined: 08 Sep 2003
Posts: 66
Location: Toronto, Canada

View user's profile Send private message

RE: Problem with serial communication with 16F877 (PCM)
PostPosted: Thu May 15, 2003 7:03 am     Reply with quote

:=Hello Everybody.
:=I'n using now the PIC16F877, I used the same code that's functionning very good in the PIC16F84, but not in the PIC16F877.
:=NB: The 84 doesn't have any integrated SCI (USART), meanwhile the 877 has one, & there's nothing on the Serial Interface.
:=I mention here some Code:
:=#include <stdio.h>
:=#include <16F877.h>
:=#device adc=8
:=#use delay(clock=4000000)
:=#fuses XT,NOWDT
:=#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
:=
:=#int_TBE
:=TBE_isr() {}
:=
:=#int_RDA
:=RDA_isr() {}
:=
:=void main() {
:= char c;
:= enable_interrupts(INT_TBE);
:= enable_interrupts(INT_RDA);
:= enable_interrupts(global);
:= printf("\r\nPress a key: ");
:= c=getc();
:= printf("\r\n You've Pressed: \%c",c);
:=}
:=
:=
:=The unique difference is that here There's built-in support for ISR, but not in the 84.
:=
:=SO, I don't see anything on TX nor on RX pins on the 877.
:=Should I live this ISR Interrupt Service Routines empty?
:=10x in advance.

Since you are not using the interrupts, why not get rid of them and get thing working fisrt without them. Also your program will only run once, then will go to sleep.

Here is a modification to your code that should work, I did not have time to test it but you should get the idea.
<pre>
#include <16F877.h>
#use delay(clock=4000000)
#fuses XT,NOWDT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void main() {
char c;

printf("\r\nPress a key: ");
while(1){ //Let's do this forever
if( kbhit(){ //Is there a byte in the receive buffer?
c=getc(); //Get the byte
printf("\r\n You've Pressed: \%c",c);
printf("\r\nPress a new key: ");
}
}
}
</pre>

Hope this helps.
-Troy
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514492
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Problem with serial communication with 16F877 (PCM)
PostPosted: Thu May 15, 2003 9:59 am     Reply with quote

:=I've experienced this stupid problem too. Try using two other pins for serial communication (i.e. C1 & C2).
------------------------------------------------------
But then you give up the hardware USART, which is a very good
thing to have.

The problem is caused by CCS putting a hidden Sleep instruction
at the end of main(). You can't prevent the compiler from
putting in the Sleep instruction. It's always there.

So your program really looks like this:

main()
{
char c;

c = getc();
printf("\%c", c);

#asm
sleep // This is the hidden sleep instruction
#endasm
}

So why does it cause a problem with the hardware USART ?
It's because the USART transmitter can store two characters
internally. It can have one char in its input buffer and
another one in its output shift register.

So if you have 1 or 2 chars in the USART's buffers, and then
a Sleep instruction is executed, what happens ? The USART
is turned off and the characters are not transmitted.
This is why if you have a printf() statement near the end
of your program, you will often see the last two characters
cut off.

To solve this problem, the easiest way is to put a continous
loop at the end of the program, to prevent the Sleep instruction
from being executed. Example:

main()
{
char c;

c = getc();
printf("\%c", c);

while(1); // This line prevents the PIC from going to sleep
}

___________________________
This message was ported from CCS's old forum
Original Post ID: 144514495
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group