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

2 bytes ASCII to int8(BCD)

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



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

2 bytes ASCII to int8(BCD)
PostPosted: Sat Apr 12, 2008 8:04 am     Reply with quote

Greetings all,

I have an application where I gather data from a GPS receiver and need to send it to another device that is expecting the time and date in BCD. For example, when I grab the UTC minutes from the NMEA string as "47", this is actually two bytes ASCII. I need to ship this out the serial port as an int8 with a value of 47. Not 0x2F.
_________________
Confidence is the feeling you have right before you fully understand the situation...
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Apr 12, 2008 9:31 am     Reply with quote

When we look at numbers we always see a notation of the real thing.
"47" ascii is decimal 47 as is "0x2F". Now 8 bit binary notation is especially useful since it can be directly manipulated by the PIC. So 47decimal in binary when stored in a byte and reference by int8 is very convenient. To assign this use n=47 and to output this as a byte use printf("%c",n) .The char output will have the binary pattern of the decimal number 47 ( a single ascii byte with the decimal value 47 will be output)
Now when "4" followed by "7" is input you need to see it as a conversion from one notation to another.
"4" is ascii 52 in decimal so subtract 48 (aka ascii "0") "7" is ascii decimal 55
so again subtract 48. So algorithmically you have the ascii decimal value of ("4"-"0")*10+("7"-"0")=47. This is what the built in atoi routine does.
Now BCD ( binary code decimal) is another notation. Here 4 bits is used to notate a decimal digit. So 47 in BCD is 0100 0111 in binary notation but this will be interpreted by the PIC as pure binary notation or "0x47" in hex notation. Again see this as a conversion of notation issue. Algorithmically it is ("4"-"0")*16+("7"-"0")=71
KU5D



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 10:15 am     Reply with quote

Many thanx,

Here's what I ended up doing...tell me if I'm wrong:

Code:

//----------- UTC time stuff--- --------------------------------------
UTC_Hours = 0x10 * (GPS_String[6]-0x30) + (GPS_String[7]-0x30);       
UTC_Mins = 0x01 * (GPS_String[8]-0x30) + (GPS_String[9]-0x30);
UTC_Secs_Int = 0x10 * (GPS_String[10]-0x30) + (GPS_String[11]-0x30);

_________________
Confidence is the feeling you have right before you fully understand the situation...
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Apr 12, 2008 11:22 am     Reply with quote

Take a second look at
Code:
UTC_Mins = 0x01 * (GPS_String[8]-0x30) + (GPS_String[9]-0x30);

It may be a typo and not what you intend.
KU5D



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 11:34 am     Reply with quote

Actually it's working with the multiply by 16. Apparently, when his end receives a value of 47 minutes it's looking for a bcd or hex number rather than decimal. I was sending him 47, i.e. 0x2F. This of course didn't work because he's looking for 0x47. So, multiplying the tens digit by 16 and adding the ones makes it a hex number.
_________________
Confidence is the feeling you have right before you fully understand the situation...


Last edited by KU5D on Sat Apr 12, 2008 11:35 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 11:34 am     Reply with quote

The function you want is called "atoi". It's in stdlib.h. It requires a
string as input (i.e., it must have a 0x00 string terminator byte at the
end of the sequence of ascii characters).
Quote:
c:\program files\picc\drivers\stdlib.h

For more information, check the CCS manual and search the forum
archives for atoi.
KU5D



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 11:41 am     Reply with quote

Yes, I'm familiar with the atoi function. However, I'm not printing this out a port by itself...I'm putting this in a transmit buffer with lots of other stuff and sending it over radio using an MSK modem. I couldn't have the terminator in there. In any case it is now working using the above approach. Thanx for taking the time guys, I appreciate it.
_________________
Confidence is the feeling you have right before you fully understand the situation...
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Apr 12, 2008 2:29 pm     Reply with quote

Take a third look at
Code:

UTC_Mins = 0x01 * (GPS_String[8]-0x30) + (GPS_String[9]-0x30);

0x01 is multiply by 1 for multiply by 16 use 0x10
KU5D



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 5:37 pm     Reply with quote

Ah...call it a rectal / cranial inversion moment...Typo indeed.

Many thanx
_________________
Confidence is the feeling you have right before you fully understand the situation...
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