  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			John Morley Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| problem with shift_left..... | 
			 
			
				 Posted: Mon Jun 23, 2003 11:19 am     | 
				     | 
			 
			
				
  | 
			 
			
				Hi all,
 
 
Compiler version: 3.162
 
 
I am using shift_left to clock 8 bits of data out of a peripheral device, MSB first. The problem is that when I get the 8th bit, the variable that the data is going into rolls over to a negative number. I seem to be doing it exactly the way numerous examples in the manual do it, but mine has this undesired behaviour. BTW, the data variable is define as: 
 
 
int data;
 
 
I show the routine I am having a problem with first, and then the output of a printf to show what's going on. As you can see from the output, all seems to be well until the 8th bit....
 
 
 
 for(ctr = 1 ; ctr <= 8 ; ++ctr)
 
	{
 
		output_high(SCLK); 		// Clock to high
 
		delay_us(20);
 
		shift_left(&data,1,input(SDATA));
 
		output_low(SCLK); 		// Clock to low
 
		printf("ctr, bit, data: : \%i \%i \%i\n\r", ctr,input(SDATA), data);
 
	}
 
		output_low(SDATA);		// Acknowledge the 1st byte  to be driven by the SHT1x
 
		output_high(SCLK);
 
		delay_us(20);
 
		output_low(SCLK);
 
 
 
Output:
 
 
ctr, bit, data: : 1 1 1
 
ctr, bit, data: : 2 1 3
 
ctr, bit, data: : 3 1 7
 
ctr, bit, data: : 4 0 14
 
ctr, bit, data: : 5 0 28
 
ctr, bit, data: : 6 0 56
 
ctr, bit, data: : 7 0 112
 
ctr, bit, data: : 8 1 -32
 
 
Thanks for looking!
 
 
John
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 144515473 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Neutone
 
 
  Joined: 08 Sep 2003 Posts: 839 Location: Houston 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re: problem with shift_left..... | 
			 
			
				 Posted: Mon Jun 23, 2003 11:25 am     | 
				     | 
			 
			
				
  | 
			 
			
				:=Hi all,
 
:=
 
:=Compiler version: 3.162
 
:=
 
:=I am using shift_left to clock 8 bits of data out of a peripheral device, MSB first. The problem is that when I get the 8th bit, the variable that the data is going into rolls over to a negative number. I seem to be doing it exactly the way numerous examples in the manual do it, but mine has this undesired behaviour. BTW, the data variable is define as: 
 
:=
 
:=int data;
 
:=
 
:=I show the routine I am having a problem with first, and then the output of a printf to show what's going on. As you can see from the output, all seems to be well until the 8th bit....
 
:=
 
:=
 
:= for(ctr = 1 ; ctr <= 8 ; ++ctr)
 
:=	{
 
:=		output_high(SCLK); 		// Clock to high
 
:=		delay_us(20);
 
:=		shift_left(&data,1,input(SDATA));
 
:=		output_low(SCLK); 		// Clock to low
 
:=		printf("ctr, bit, data: : \%i \%i \%i\n\r", ctr,input(SDATA), data);
 
:=	}
 
:=		output_low(SDATA);		// Acknowledge the 1st byte  to be driven by the SHT1x
 
:=		output_high(SCLK);
 
:=		delay_us(20);
 
:=		output_low(SCLK);
 
:=
 
:=
 
:=Output:
 
:=
 
:=ctr, bit, data: : 1 1 1
 
:=ctr, bit, data: : 2 1 3
 
:=ctr, bit, data: : 3 1 7
 
:=ctr, bit, data: : 4 0 14
 
:=ctr, bit, data: : 5 0 28
 
:=ctr, bit, data: : 6 0 56
 
:=ctr, bit, data: : 7 0 112
 
:=ctr, bit, data: : 8 1 -32
 
:=
 
:=Thanks for looking!
 
:=
 
:=John
 
 
So the printf function is showing a signed interger instead of an unsigned interger? Verify the printf function call is formatted correctly. If you were shifting in a signed byte it worked fine. 
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 144515475 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Sherpa Doug Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re: problem with shift_left..... | 
			 
			
				 Posted: Mon Jun 23, 2003 12:24 pm     | 
				     | 
			 
			
				
  | 
			 
			
				It looks like a printf problem.  Try printing the result in hex instead of decimal to see what the actual bits are.
 
 
:=Hi all,
 
:=
 
:=Compiler version: 3.162
 
:=
 
:=I am using shift_left to clock 8 bits of data out of a peripheral device, MSB first. The problem is that when I get the 8th bit, the variable that the data is going into rolls over to a negative number. I seem to be doing it exactly the way numerous examples in the manual do it, but mine has this undesired behaviour. BTW, the data variable is define as: 
 
:=
 
:=int data;
 
:=
 
:=I show the routine I am having a problem with first, and then the output of a printf to show what's going on. As you can see from the output, all seems to be well until the 8th bit....
 
:=
 
:=
 
:= for(ctr = 1 ; ctr <= 8 ; ++ctr)
 
:=	{
 
:=		output_high(SCLK); 		// Clock to high
 
:=		delay_us(20);
 
:=		shift_left(&data,1,input(SDATA));
 
:=		output_low(SCLK); 		// Clock to low
 
:=		printf("ctr, bit, data: : \%i \%i \%i\n\r", ctr,input(SDATA), data);
 
:=	}
 
:=		output_low(SDATA);		// Acknowledge the 1st byte  to be driven by the SHT1x
 
:=		output_high(SCLK);
 
:=		delay_us(20);
 
:=		output_low(SCLK);
 
:=
 
:=
 
:=Output:
 
:=
 
:=ctr, bit, data: : 1 1 1
 
:=ctr, bit, data: : 2 1 3
 
:=ctr, bit, data: : 3 1 7
 
:=ctr, bit, data: : 4 0 14
 
:=ctr, bit, data: : 5 0 28
 
:=ctr, bit, data: : 6 0 56
 
:=ctr, bit, data: : 7 0 112
 
:=ctr, bit, data: : 8 1 -32
 
:=
 
:=Thanks for looking!
 
:=
 
:=John
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 144515478 | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  
		 |