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

RS232 receive

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



Joined: 31 Jan 2011
Posts: 4

View user's profile Send private message

RS232 receive
PostPosted: Mon Jan 31, 2011 6:09 pm     Reply with quote

Hi, I have a pair of temperature sensors which read through adc, now I like to send the data for rs232, but, how differentiate the reads in the rx ?
Here is the Tx code.
Greetings & tks...

Code:

#include <16f876A.h>
#device ADC=10

#FUSES NOWDT, XT, NOPUT, NOLVP
#use delay (clock=4000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main(){

int16 sen_01;
int16 sen_02;

setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);

while(true){
 

         set_adc_channel(0);
         delay_us(50);
         sen_01=read_adc();
         
         delay_us(50);
         
         set_adc_channel(1); 
         delay_us(50);
         sen_02=read_adc(); 

          putc(sen_01);
          putc(sen_02);

   }
}
SherpaDoug



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

View user's profile Send private message

PostPosted: Mon Jan 31, 2011 10:13 pm     Reply with quote

You are reading a 10 bit A/D, but your putc only sends 8 bits. To send 10 bits you need to send 2 bytes. If you are going to send 2 bytes (16 bits) you can use the high bit to signal if the reading is sensor #1 or sensor #2.
_________________
The search for better is endless. Instead simply find very good and get the job done.
achtung_m



Joined: 31 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 10:28 am     Reply with quote

ok , but if i sending with printf would take the division?
could you give an example?

Thanks & greetings.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 10:40 am     Reply with quote

Try it and learn while doing, it's the best teacher !
Since we do not have any idea what is receiving the data it is near impossible to create your code. Could be a simple PC with a canned terminal program or a propriatory 68k system that needs inverted, slewed 16 bit checksum!
You should develop your own protocol for your application, you'll get a lot of education!

Be aware that ANYTIME you use the RS232(....) fucntion, you MUST include the ERRORS option.
achtung_m



Joined: 31 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 12:46 pm     Reply with quote

Quote:

Try it and learn while doing, it's the best teacher !
Since we do not have any idea what is receiving the data it is near impossible to create your code. Could be a simple PC with a canned terminal program or a propriatory 68k system that needs inverted, slewed 16 bit checksum!
You should develop your own protocol for your application, you'll get a lot of education!


I agree, but the end of this post is to get a guide on how to do, not everything done.
I receive the data with other pic, but I don't know how to read.

greetings.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 2:14 pm     Reply with quote

There are lots of examples in the examples folder of PCM, including PIC to PIC communications...

I suggest you look at them and try them.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 3:37 pm     Reply with quote

Quote:
Now I like to send the data for rs232, but, how differentiate the reads in the rx ?

You can design an ASCII packet protocol to do this. For example,
here's the NMEA-0183 protocol:
http://www.tronico.fi/OH6NT/docs/NMEA0183.pdf
Look at this section:
Quote:

3. General Sentence Format

Notice how a packet starts with a unique "packet start" character that is
not ever used as a data byte byte. Also, it has unique "packet end"
characters. You can use other ASCII characters as the delimiters, such
as STX (0x02) and ETX (0x03). The data length can be fixed, or variable.
You can include a byte that tells the packet length. You can include a
checksum byte. These are optional.

To differentiate the data from the two ADC channels, you could create a
packet that has the data for both channels. Then have the bytes for the
1st channel be first in the packet. The 2nd channel bytes would be last
in the packet.

Once you have created your packet protocol, you can design the code
in the receiver PIC. Have it look for the unique "start of packet" byte.
Then sequentially read in the packet data bytes. Put them in an array.
Stop reading them when you receive the "end of packet" byte.
Then process the data in the array.

Convert the ASCII to a binary integer so you can use it in the receiver
PIC's program. It takes four ASCII Hex data bytes to represent a 16-bit
integer value. To convert this to an 'int16', you need to make those 4
bytes into a Hex string. To do this, put the two characters "0x" in the first
two bytes of the data array. Next you have the 4 ASCII Hex data bytes.
Then put a 0x00 byte at the end. This makes it into a string.

You can now use string handling routines in string.h or stdlib.h routines
to process it. The atol() routine in stdlib.h will convert a Hex string into
an 'int16' value. This is described in the CCS compiler manual.
achtung_m



Joined: 31 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 5:58 pm     Reply with quote

Hi PCM programmer, thoroughly read about this protocol, thank you very much for give me this data.

Greetings.
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