|
|
View previous topic :: View next topic |
Author |
Message |
rvdbogae Guest
|
translate string number to unsigned long |
Posted: Wed Apr 24, 2002 2:22 am |
|
|
Hi All
I try to read an unsigned long via the RS232 into my pic.
I dont know how to do.
In the following code I read a sting (number) into my pic (16F877).
Is it possible to translate it in a unsigned long?
Are there other (more simple) possebilities?
////////////////code/////////////////////////////////////
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)
main() {
char string[50];
printf("give the number of pulses\r\n");
gets(string);
printf("\%S\n", string);
while(1);
//// now translate string to unsigned long
}
//////////////////////////////////////////////////////////////
thanks for help,
ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3976 |
|
|
Hans Wedemeyer Guest
|
Re: translate string number to unsigned long |
Posted: Wed Apr 24, 2002 7:08 am |
|
|
If it is an ASCII string the simply use the atoi(&string); function.
If you simply want to receive the bytes directly as binary values, then put them into a struct/union one by one and you do not have to convert anything.
Example:-
struct HighLow
{
char l;
char h;
};
union HL
{
long L;
struct HighLow hl;
};
union HL MyNumber;
MyNumber.hl.h = getchar(); // receive the high byte first
MyNumber.hl.l = getchar(); // receive the low byte next
Now you have the unsigned long value as MyNumber.L and no conversion needed.
unsigned long XYZ = MyNumber.L;
Then when you need to send it back out simply do this:-
putchar(MyNumber.hl.h);
putchar(MyNumber.hl.l);
:=////////////////code/////////////////////////////////////
:=
:=#fuses HS,NOWDT,NOPROTECT
:=
:=#use delay(clock=20000000)
:=#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=main() {
:=
:= char string[50];
:=
:= printf("give the number of pulses\r\n");
:= gets(string);
:= printf("\%S\n", string);
:= while(1);
:= //// now translate string to unsigned long
:=}
:=
:=//////////////////////////////////////////////////////////////
:=
:=thanks for help,
:= ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3981 |
|
|
Todd C. Micallef Guest
|
Re: translate string number to unsigned long |
Posted: Wed Apr 24, 2002 11:08 am |
|
|
Another way to convert is to use the get_long()function in the input.c file inside the drivers dir. The function defaults to returning a signed value though.
:=Hi All
:=
:=I try to read an unsigned long via the RS232 into my pic.
:=I dont know how to do.
:=
:=In the following code I read a sting (number) into my pic (16F877).
:=Is it possible to translate it in a unsigned long?
:=Are there other (more simple) possebilities?
:=
:=////////////////code/////////////////////////////////////
:=
:=#fuses HS,NOWDT,NOPROTECT
:=
:=#use delay(clock=20000000)
:=#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=main() {
:=
:= char string[50];
:=
:= printf("give the number of pulses\r\n");
:= gets(string);
:= printf("\%S\n", string);
:= while(1);
:= //// now translate string to unsigned long
:=}
:=
:=//////////////////////////////////////////////////////////////
:=
:=thanks for help,
:= ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3986 |
|
|
|
|
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
|