| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| AlastairM 
 
 
 Joined: 28 Apr 2008
 Posts: 33
 
 
 
			    
 
 | 
			
				| dsPIC30 printf 32-bit ints |  
				|  Posted: Wed Jul 04, 2012 4:16 pm |   |  
				| 
 |  
				| Hi, I'm using a dsPIC30F4011, MPLAB 8.85, CCS 4.134
 I am having some strange printf results. Could someone explain if it's me or a bug?
 
  	  | Code: |  	  | int16    buf[3]; int32   PackedData;
 
 #use rs232(UART2,baud=19200,parity=N,bits=8,stream=DEBUG,errors)
 
 buf[0]=0x169;   //ADCBUF0;
 buf[1]=0x2AA;   //ADCBUF1;
 buf[2]=0x355;   //ADCBUF2;
 PackedData=(((int32)buf[2])<<22)|(((int32)buf[1])<<12)|(((int32)buf[0])<<2);
 fprintf(DEBUG,"%4LX,%4LX,%4LX = %8LX\r\n\r\n",   buf[2],buf[1],buf[0],PackedData);
 | 
 output from MPLAB SIM
 
  	  | Code: |  	  | Update      Address      Symbol Name      Value       Hex                    Binary 
 80E     PackedData        0xD56AA5A4 0xD56AA5A4  11010101 01101010 10100101 10100100
 808     buf
 808     [0]                   0x0169     0x0169                    00000001 01101001
 80A     [1]                   0x02AA     0x02AA                    00000010 10101010
 80C     [2]                   0x0355     0x0355                    00000011 01010101
 | 
 I'm Happy with that!
 but output from serial
 
  	  | Code: |  	  | 0355,02AA,0169 =  FFFFFFFFD56AA5A4 
 | 
 what are all those extra 'F's?
 If I change it to
 
  	  | Code: |  	  | buf[0]=0x69; buf[1]=0xAA;
 buf[2]=0x55;
 | 
 then I get on the serial:
 
 
  	  | Code: |  	  | 0055,00AA,0069 =  154AA1A4 
 | 
 (as an aside the print field width for hex numbers seems to be ignored)
 Thanks
 Al
 |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Jul 05, 2012 2:53 pm |   |  
				| 
 |  
				| I don't have that compiler but just to make an educated guess, I think that because PCD has a "long long int" data type (64 bits) that it may be
 sign-extending the value.   Try casting the variable in the fprintf statement
 to an int32.  See if that fixes the problem.
 
 In other words, CCS doesn't have separate printf format strings for all
 the various sizes beyond a byte.  In PCM and PCH, it has "%LX" which
 is used to display both 16-bit and 32-bit values.   There is no "%LLX".
 So in PCD, they add an even larger type, 64-bits, to the same format
 string.   That's why I think casting the variable (inside the fprintf
 statement) may fix the problem.  At least it's something to try.
 |  |  
		|  |  
		| AlastairM 
 
 
 Joined: 28 Apr 2008
 Posts: 33
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Jul 05, 2012 5:03 pm |   |  
				| 
 |  
				|  	  | Quote: |  	  | Try casting the variable | 
 That didn't work - I've admitted defeat, I assume there's a bug that thinks a 32-bit unsigned int with the MSB set is a 64-bit signed int.
 Here's my gruesome workaround!
 
  	  | Code: |  	  | fprintf(DEBUG,"%4LX,%4LX,%4LX ...  %4LX%4LX\r\n\r\n",   buf[2],buf[1],buf[0],(int16)(PackedData>>16), (int16)PackedData); | 
 which gives the correct output on the serial:
 
  	  | Code: |  	  | 0355,02AA,0169 ...  D56AA5A4 
 | 
 Thanks for your help
 Al
 |  |  
		|  |  
		| AlastairM 
 
 
 Joined: 28 Apr 2008
 Posts: 33
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jul 10, 2012 2:30 pm |   |  
				| 
 |  
				| I've had an email from CCS Support: 
 That was quick! Don't forget to send your bug reports to them... 	  | Quote: |  	  | The problem you reported has been fixed and will be in the next compiler release. | 
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |