| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| KU5D 
 
 
 Joined: 10 Feb 2008
 Posts: 46
 Location: Asheville, North Carolina
 
 
			    
 
 | 
			
				| 2 bytes ASCII to int8(BCD) |  
				|  Posted: Sat Apr 12, 2008 8:04 am |   |  
				| 
 |  
				| 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
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 9:31 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 10:15 am |   |  
				| 
 |  
				| 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
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 11:22 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 11:34 am |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 11:34 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 11:41 am |   |  
				| 
 |  
				| 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
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 2:29 pm |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Apr 12, 2008 5:37 pm |   |  
				| 
 |  
				| 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...
 |  | 
	
		|  | 
	
		|  |