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 CCS Technical Support

PIC16F877 with Software USART

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







PIC16F877 with Software USART
PostPosted: Sat Feb 08, 2003 11:01 pm     Reply with quote

Hi all,

Just wondering how I would go about implementing a software USART in my PIC16F877. My hardware USART is currently being used, and I need some way of debugging my program via a PC RS-232 port. Which pins can be used for a software USART? What kind of code do I need to implement in order for the hardware and software USARTS to be operating together? Does it matter that the hardware USART is actually used as an I2C bus master, but the software USART needs to connect to a MAX232 chip and output standard serial data?

Any help would be appreciated,

Kind regards,

Andrew Ritchie.
andyritchie@iprimus.com.au
___________________________
This message was ported from CCS's old forum
Original Post ID: 11438
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: PIC16F877 with Software USART
PostPosted: Sun Feb 09, 2003 4:35 pm     Reply with quote

:=Hi all,
:=
:=Just wondering how I would go about implementing a software USART in my PIC16F877. My hardware USART is currently being used, and I need some way of debugging my program via a PC RS-232 port. Which pins can be used for a software USART? What kind of code do I need to implement in order for the hardware and software USARTS to be operating together? Does it matter that the hardware USART is actually used as an I2C bus master, but the software USART needs to connect to a MAX232 chip and output standard serial data?
:=
-----------------------------------------------------------
I'm not sure what you mean, when you say the "hardware USART
is actually used as an I2C bus master".

Are you talking about using hardware i2c ? That's on pins
C3 and C4 of the PIC. The hardware USART is on pins C6 and C7.
There's no conflict between them. They use different resources
in the PIC.

Anyway, here's an example program that demonstrates using
hardware and software USARTS. It's for version 3.xxx of
the compiler, because it uses "streams".

I've included two lines that demonstrate how to use the
kbhit() function with a stream. You don't really need that
in this example program, because the fgetc() statements will
also wait for an incoming char. But sometimes people ask
how to use kbhit() with streams, so I included it.
Code:

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=HW_USART)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, stream=SOFT_USART)

//=============================================
void main()
{
char i;
char c;

fprintf(HW_USART, "Press a key 5 times, to send data to the H/W usart\n\r");

for(i = 0; i < 5; i++)
   {
    while(!kbhit(HW_USART));   // Wait in loop for a keypress.
    c = fgetc(HW_USART);       // Then get the char.
    putc(c, HW_USART);         // Then echo it back to the PC.
   }

fprintf(HW_USART, "\n\rNow move the connector to the soft USART pins\n\r");

//---------
fprintf(SOFT_USART, "Press a key 5 times, to send data to the Soft usart\n\r");

for(i = 0; i < 5; i++)
   {
    while(!kbhit(SOFT_USART)); // Wait in a loop for a keypress
    c = fgetc(SOFT_USART);     // Then get the char.
    putc(c, SOFT_USART);       // Then echo it back to the PC.
   }

fprintf(SOFT_USART, "\n\rNow you've tested both types of USARTS\n\r");

while(1);
}


___________________________
This message was ported from CCS's old forum
Original Post ID: 11458

Edit: Put it in a Code Block for easier reading.


Last edited by PCM programmer on Mon Oct 13, 2008 5:01 pm; edited 1 time in total
Andrew Ritchie
Guest







Re: PIC16F877 with Software USART
PostPosted: Sun Feb 09, 2003 5:30 pm     Reply with quote

<i>"I'm not sure what you mean, when you say the "hardware USART is actually used as an I2C bus master". Are you talking about using hardware i2c ? That's on pins C3 and C4 of the PIC. The hardware USART is on pins C6 and C7. There's no conflict between them. They use different resources in the PIC."</i>

Sorry, I should've been clearer. Basically, my project requires SPI, I2C and RS-232 interfaces. Some C code I have downloaded of a similar project uses C3 & C4 for the SPI port, and C6 & C7 for the I2C port. I know the I2C is supposed to go on C3 & C4, but I presume in the example I have they use C6 & C7 because C3 & C4 are already booked up.

Thank you very much for the example - it's great. I presume I could replace the hardware USART "#use rs232..." line with "#use i2c..." to get the results I want.

Kind regards

Andrew Ritchie
___________________________
This message was ported from CCS's old forum
Original Post ID: 11461
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: PIC16F877 with Software USART
PostPosted: Sun Feb 09, 2003 5:38 pm     Reply with quote

:=<i>"I'm not sure what you mean, when you say the "hardware USART is actually used as an I2C bus master". Are you talking about using hardware i2c ? That's on pins C3 and C4 of the PIC. The hardware USART is on pins C6 and C7. There's no conflict between them. They use different resources in the PIC."</i>
:=
:=Sorry, I should've been clearer. Basically, my project requires SPI, I2C and RS-232 interfaces. Some C code I have downloaded of a similar project uses C3 & C4 for the SPI port, and C6 & C7 for the I2C port. I know the I2C is supposed to go on C3 & C4, but I presume in the example I have they use C6 & C7 because C3 & C4 are already booked up.
:=
:=Thank you very much for the example - it's great. I presume I could replace the hardware USART "#use rs232..." line with "#use i2c..." to get the results I want.
------------------------------------------------------------
If they're using pins C6 and C7 for the i2c port, then they're
using software i2c. You can use any pins for that. Use
some other pins besides C6 and C7, because the hardware USART
is very useful.

If you use Port A for software i2c, make sure that you use
the setup_adc_ports() function to configure the pins as digital
i/o, and not analog. That's the only thing to watch out for.

___________________________
This message was ported from CCS's old forum
Original Post ID: 11462
Andrew Ritchie
Guest







Re: PIC16F877 with Software USART
PostPosted: Mon Feb 10, 2003 12:59 am     Reply with quote

So is it possible to use a hardware USART on C6 & C7, a software I2C port on lets say B1 & B2, a hardware SPI on C3 & C4 and a software USART on B3 & B4, all in the one program?!?

This would be amazing...
___________________________
This message was ported from CCS's old forum
Original Post ID: 11466
Pete Smith
Guest







Re: PIC16F877 with Software USART
PostPosted: Mon Feb 10, 2003 3:11 am     Reply with quote

:=So is it possible to use a hardware USART on C6 & C7, a software I2C port on lets say B1 & B2, a hardware SPI on C3 & C4 and a software USART on B3 & B4, all in the one program?!?

Yep! It should be perfectly possible!

I've certainly had 2 serial ports (1 hardware, 1 software) and a hardware I2C port on one device. I've not used SPI recently, but there's no reason why it wouldn't work.

FWIW, my codespace was pretty full, I just couldn't get the streamed IO to work, ie using 2 #use RS232s with the STREAM field, and then using printf with the stream field.

On its own, in a small program, they worked fine, but as the codespace increased, they got more & more unreliable, until they all stopped working!

Pete.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11468
R.J.Hamlett
Guest







Re: PIC16F877 with Software USART
PostPosted: Mon Feb 10, 2003 3:14 am     Reply with quote

:=So is it possible to use a hardware USART on C6 & C7, a software I2C port on lets say B1 & B2, a hardware SPI on C3 & C4 and a software USART on B3 & B4, all in the one program?!?
:=
:=This would be amazing...
Yes. This is fine. :-)

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 11469
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