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

Send INT16 via RS-232?

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



Joined: 08 Feb 2012
Posts: 4

View user's profile Send private message Send e-mail MSN Messenger

Send INT16 via RS-232?
PostPosted: Wed Feb 08, 2012 6:31 am     Reply with quote

hi everybody,

i have a variable int16(2 bytes) but I can't send it very well its value via UART to my PC because i read a different value..what's wrong in my code?Can i send int16 with rs-232?? Please help me!

Code:
#include <18f4331.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(stream=MASTER,baud=57600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdlib.h>
#include <input.c>

int16 enc=64569 ;

void main()

putc(enc);

;
}
}


Thks everybody!!
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 6:54 am     Reply with quote

There are 2 easy ways to send int16 from a PIC to a PC. You have to understand that the usual PC and microcomputer UARTs are designed for 8 bit or byte sized data transfers.

Option 1: Have the PIC 'breakup' the int16 variable into 2 bytes then send them to the PC using 2 putc() functions. Check the onscreen help file or the examples folders for the details.

Option2: Cut PIC code to create a custom 16bit serial driver and a 16bit serial driver for the PC as well.

I don't know what PC application/program you're using, whether you can write drivers,etc. but I have used option 2 for clients in the past. Technically faster and safer.

99.999% of all PIC users will use option #1. It's faster to code and universal to all PC serial programs.
gabrix18



Joined: 08 Feb 2012
Posts: 4

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 08, 2012 7:59 am     Reply with quote

thank you very much for your reply! I' m using PCWHD (PIC compiler) and a matlab file to communicate with uart....

I understood everything you explained to me but I am not able to use the options.... can you help me with some example?

THKS!!!!!!

Very Happy
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Feb 08, 2012 8:02 am     Reply with quote

Numbers can be notated in many different forms. The PIC internally most often uses an electronic binary notation in which 8 bit registers notate numbers by voltages ( high and low) for each bit. The PIC registers can also notate other things such as the ascii code for an alpha character or the mantissa of a floating point number etc. Now this high low voltage binary notation can be transferred to the outside via the PIC UART. The UART will serialize the PIC register but you now must be aware of the order the serialized notation is in ( lsb first or msb first). You wish to transfer 2 bytes for int16 so again the order is important high byte first or low byte first. The PC must also agree with this ordering of the information in order for it to correctly notate the number received in its registers. This approach is about raw native notation.Putc() is used for this. Another approach can be to transfer the data from the PIC in hexadecimal ascii notation or just ascii decimal notation...here the convention ( for western languages) is universal with the most significant digit encoded in ascii always leftmost. Printf is used for this. The PC in turn will decode the ascii and notate the number in its registers.
gabrix18



Joined: 08 Feb 2012
Posts: 4

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 08, 2012 8:36 am     Reply with quote

ok, so if the value I need to send is for example A=40000...which is the right code to compile for reading in my pc the same value?

putc(A) does not work of course because A is declared INT16...
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 9:11 am     Reply with quote

You need to read about the CCS supplied function MAKE8().
Open a project, press F11, then find the make8() function in the list of shown on the leftside under 'all functions' ( or whatever it's called....)

YOU have to split your int16 variable into two int8s..

pseudocode to do this...

a0=make8(A,0);
a1=make8(A,1);


puseudocode to send to PC

putc(a0);
putc(a1);


It's up to YOU to figure out how to program the PC to accept the data and make it a 16bit value as we do not know what program you're using on the PC for serial reception(Delphi,VB, Quickbasic4.5,COBOL,FORTH)

YOU have to be sure the 'a0 and a1' are sent AND received in the correct order or your data will be wrong!
gabrix18



Joined: 08 Feb 2012
Posts: 4

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 08, 2012 9:23 am     Reply with quote

Ok I'm understanding...I use this matlab script...
Code:

try
    int=input('delta [s]:'); %set time of commuincation
    %1 create serial port object
    s = serial('COM3','BaudRate',57600);
    %connetct to the device
    fopen(s)
   
    i=0
    tic
    t=toc;
    delta=1;
    j=0;
    res=5/2^8;
   
    while (t<int)
        if (s.BytesAvailable~=0)
           
            delta=s.BytesAvailable;
        end
        d(1,j+1:j+delta)=fread(s,delta);
        j=j+delta;
       
        %data=res.*d;
        hold on
        plot(res.*d,'.');
        drawnow
        %clear d
        t=toc;

    end
    fclose(s)
    beep;
   
catch
    fclose(s)

end

What I have to do to program the PC to accept the data and make it a 16 bit??

THKS!!!!
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 10:09 am     Reply with quote

This is NOT the MATLAB forum ! It's supposed to be about using the CCS compiler with PICs.

You should be asking the MATLAB guys how to recieve two bytes and make them into a word....

This thread will probably be locked soon due to nonCompiler query.
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