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

RS232 for PIC 16F819

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



Joined: 31 Mar 2004
Posts: 23
Location: Switzerland

View user's profile Send private message

RS232 for PIC 16F819
PostPosted: Tue May 17, 2005 5:22 am     Reply with quote

hi

I'm looking for an code example for the RS232 Communication with a 16f819. This Pic hasn't an UART.

Can help me everybody?

Thanks

Pablo
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue May 17, 2005 8:10 am     Reply with quote

It should be pretty easy to use the software UART supplied by CCS. I used it all the time with the 16C5x series of parts. What do you want to do? Here is a trivial example clipped from some old code:

Code:
#include <16C58A.h>
#include <ctype.h>
#use delay(clock=20000000)
#fuses HS,WDT
#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7)

main() {
   char key;
   key = toupper(getch());
   putc(key);
} //main()

_________________
The search for better is endless. Instead simply find very good and get the job done.
neurus



Joined: 31 Mar 2004
Posts: 23
Location: Switzerland

View user's profile Send private message

PostPosted: Wed May 18, 2005 12:15 am     Reply with quote

hi SherpaDoug

thank you for your answer. I want to connect 2 Pic's through the RS232. Both Pic's doesn't have Hardware UART, therefore I'm looking for a subroutine to send and other to receive data. The first one is the master, the second is the slave. The slave measure the ambient temperature. The master gets the data from slave and shows it on the lcd.

Thanks

Pablo
Ttelmah
Guest







PostPosted: Wed May 18, 2005 4:51 am     Reply with quote

The key 'problem' with the software RS232, is that the receiving device, should be sitting waiting in 'getc', before the character transmission begins (or for very low baud rates, get 'into' getc soon after the character starts).
Hence if you use a very low baud rate, you can get away by using an interrupt input as your serial receive line, and when this line is seen to drop, immediately call getc in the interrupt handler. However unless the baud rate is low, relative to the processor clock (perhaps below about 300:1), this will not work.
The 'best' way if you have a spare pin on each chip, is to have the 'master' set this line when it wants to get the data. If the slave has this connected to an interrupt, or polls it in a relatively tight loop, then when it sees the line become set, it can send the data. The master meanwhile starts listening.
So (at a very crude level...):
Code:

//Master
//Assuming the pin is named 'I want to hear' on the master...
int8 ip[4];
int8 ctr;

output_high(I_WANT_TO_HEAR);
for (ctr=0;ctr<4;ctr++) {
    ip[ctr]=getc();
    //Must drop the pin as soon as transmission begins
    output_low(I_WANT_TO_HEAR);
}


//Slave
//Assuming the pin is named 'Send' on the slave...
in8 reading[4];
int8 ctr;

while(TRUE) {
   //Sit here taking the readings and looping
   if (input_pin(SEND)) {
       //Here the master has raised the pin
       for (ctr=0;ctr<4;ctr++)
           putc(reading[ctr]);
   }
   //Have code here to actually take the readings.
   //Ideally keep the loop times fairly short - don't sit waiting for the
   //ADC for example, but loop doing one job on each pass.
}

Then the main 'requests' data by raising the signal line. It then has to wait for however long the 'loop' time is in the slave (in the worst case, normally less time is needed, because the slave is already part of the way 'round' the loop). Then the slave sends four characters, and the master waits till these are all received (dropping the signal line as soon as one is seen). Ideally you would add timeouts, and error checking, but for a short link, this should not be needed.
Hopefully it gives you an 'idea' of one possible approach.

Best Wishes
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