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

Sending integer to pic

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



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

Sending integer to pic
PostPosted: Sat Jan 08, 2011 6:20 pm     Reply with quote

I'm using PIC16f877 and CSC C compiler.
Although I tried much and spent some hours, I couldn't manage to send integer values from serial port to pic. What would you suggest?
[I can send characters such as 'a','b','c' etc..]

Thanks
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Jan 08, 2011 6:26 pm     Reply with quote

Remember, that 'a', 'b' are all integers, we just also happen to translate them as ASCII text.

What kinds of integer values are you trying to send? 8bits? 16bits? 32bits?

If you're only sending 8bits, you can just send 0-255 as a single PUTC();
If you want > 8bits, you need to send them as strings and then use ATOI() to convert the ASCII back to an Integer.


-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Sat Jan 08, 2011 6:38 pm     Reply with quote

Thanks bkamen,
I'm trying to send integers bigger than 256, such as 500, 1000, 2000.
I tried something with atoi() function but failed. I obtained negative numbers.
Can you show a piece of code for this purpose?
By the way, when I send something (until now I could only managed sending characters) sending a second info is not recognized. I use getc() in an infinite while loop, should I add something, for instance an extra delay?
SherpaDoug



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

View user's profile Send private message

PostPosted: Sat Jan 08, 2011 7:37 pm     Reply with quote

How are you sending a number like 1000? Are you sending an ASCII digit '1' followed by three ASCII digit '0's? Or are you sending a binary 00000011 followed by binary 11101000? Or are you sending something else?
_________________
The search for better is endless. Instead simply find very good and get the job done.
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Sat Jan 08, 2011 7:39 pm     Reply with quote

You know CCS C has a serial port monitor tool. I use it, so I'm not sure in which way it send that data. As character string?
SherpaDoug



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

View user's profile Send private message

PostPosted: Sat Jan 08, 2011 8:28 pm     Reply with quote

If you are using the serial monitor to send 1000 then you are sending the ASCII character '1' followed by three copies of the ASCII character '0', a total of four bytes plus maybe a Carriage Return byte.

First trim the input and make it into a null terminated string. Then use ATOI() on the string to get a long unsigned int.
_________________
The search for better is endless. Instead simply find very good and get the job done.
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 5:01 am     Reply with quote

I tried sending multiple characters , "456" , and using integer value of every single character I tried to sum for the incoming number,which is 456.But there was an instability.Sometimes I send 456 and see nothing.Sometimes I send 3253 and see some results.[result of printf command running on pic side through csc serial monitor]
Do I miss something basic?Maybe terminator character[although Im not aware of it much] or buffer size?

Thanks for any guidance
temtronic



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

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 6:27 am     Reply with quote

It'd help if you showed us your PIC code.
My first guess is that you do not have the 'errors' option in the use RS232 function.
Second, sounds like you need to use a 'ring buffer' to cature the data from the sending unit(PC, terminal,etc.)
Third do you have the proper interface( MAX232 chip or equal?).
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 7:18 am     Reply with quote

You said, single characters are received correctly. If so, the RS232 interface should be O.K.

ASCII coded integer numbers, as send by Hyperterminal or the port monitor tool are multi character strings.
So you have to receive strings and define a terminating character, that ends the number string. Carriage return
would be a common standard. For a first test, you can use the gets() function, although it's somewhat limited.
You can display the received string (e.g. echo it to the terminal) after receiving it completely, and also the value
decoded by atoi() or sscanf(). You have to think about a suitable numeric type to represent the number.
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 11:54 am     Reply with quote

temtronic wrote:
It'd help if you showed us your PIC code.
My first guess is that you do not have the 'errors' option in the use RS232 function.
Second, sounds like you need to use a 'ring buffer' to cature the data from the sending unit(PC, terminal,etc.)
Third do you have the proper interface( MAX232 chip or equal?).


Hi temtronic;
I changed pic code many times since I couldnt do what should I do.
Yes I didnt have errors option,should I add it at the end of #fuses?
Yes I have max232 in the circuit,as I said I could send and receieve single character.This is one of the code attempts I tried just for basics:
Code:
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)
#org 0x1F00,0x1FFF {}
#opt 9


void main()
{
int i=0;
char ch;
while(i<10)
{

printf("%d Hello world\n\r",i);
ch=getc();
printf("The character you wrote is : %c\n\r",ch);
delay_ms(400);
i++;
}

}


I may show other codes as well.By the way I dont know what "ring buffer" is.

Thanks for your help
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 12:06 pm     Reply with quote

Same thing as a "Circular Buffer"...


See: http://en.wikipedia.org/wiki/Circular_buffer
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 12:08 pm     Reply with quote

FvM wrote:
You said, single characters are received correctly. If so, the RS232 interface should be O.K.

ASCII coded integer numbers, as send by Hyperterminal or the port monitor tool are multi character strings.
So you have to receive strings and define a terminating character, that ends the number string. Carriage return
would be a common standard. For a first test, you can use the gets() function, although it's somewhat limited.
You can display the received string (e.g. echo it to the terminal) after receiving it completely, and also the value
decoded by atoi() or sscanf(). You have to think about a suitable numeric type to represent the number.


Hi FvM;

Yes I could transfer single character.
Ok, I think I'm not clear about ending character.When I send a character from matlab, I add NOTHING special to its end, although I set the connection so that conn. Terminator='CR'. I just set the baudrate and send a single character.
You said gets() is limited. Is it size limitation or something else? It would be easier for me to use a single standard function.

And finally let me ask one more thing:
I set the connection at MATLAB side same as pic (baud=19200). I send 500 times 'a' character in a loop with a pause(0.5) in it.
On the pic side I used code as below. I see LED gets on and off 2 times, not 500 times. After second on-off no reaction from LED on my circuit.

Code:
void main()
{
char ch;

while(1)
{

ch=getc();
if(ch=='a')
output_high(PIN_D3); //LED on circuit gets on
delay_ms(500);        //stay on for 500 ms
output_low(PIN_D3); // LED on circuit gets off
delay_ms(50);          //wait for 50 ms for next character reading
}
}


Now, what would you suggest?
Thanks again
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