| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| Delfinss 
 
 
 Joined: 05 Apr 2022
 Posts: 21
 
 
 
			    
 
 | 
			
				| Data types |  
				|  Posted: Mon Aug 29, 2022 5:10 am |   |  
				| 
 |  
				| Hello everyone ccs c version 5.093
 
 My variables are defined in the global field.
 I have two ADC values. When I printf by taking the difference of these, the results show -67 or 409044448 values on the screen. What could be the reason for this change?
 
  	  | Code: |  	  | float T;
 int32   avg_CO  ,avg_IN ;
 int32 resoult = 0;
 
 if ((T>= 20)  && (T <= 29.99)) {(int32)resoult = (avg_CO-avg_IN);}
 
 printf("Resoult= %ld\n\r  ",resoult);
 | 
 AVG_IN= 231  AVG_CO= 164  Resoult= -67
 AVG_IN= 231  AVG_CO= 164  Resoult= 409044448
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Aug 29, 2022 8:18 am |   |  
				| 
 |  
				| You have your int32 variables declared as _unsigned_ types. You need to use signed int32.
 You are printing an unsigned variable as a signed result. Also when
 you subtract, if the result is negative, the maths overflows. Outcome
 'disaster'....
 Because the variables are unsigned, unsigned maths is used.
 |  | 
	
		|  | 
	
		| Delfinss 
 
 
 Joined: 05 Apr 2022
 Posts: 21
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Aug 29, 2022 8:51 am |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | You have your int32 variables declared as _unsigned_ types. You need to use signed int32.
 You are printing an unsigned variable as a signed result. Also when
 you subtract, if the result is negative, the maths overflows. Outcome
 'disaster'....
 Because the variables are unsigned, unsigned maths is used.
 | 
 
 I'm asking with apologies.
 I am using variables as signed int32(int32).
 you mean int32 resoult=0; should I not write?
 |  | 
	
		|  | 
	
		| newguy 
 
 
 Joined: 24 Jun 2004
 Posts: 1924
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Aug 29, 2022 9:33 am |   |  
				| 
 |  
				| The issue is the CCS compiler (for all versions except for PCD [16 bit compiler]) treats all variables as unsigned by default. Unless you explicitly say "signed" in front of your int32 declaration, the compiler treats that variable as unsigned. 
 With PCD, the compiler treats all variables as signed by default.
 |  | 
	
		|  | 
	
		| Delfinss 
 
 
 Joined: 05 Apr 2022
 Posts: 21
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Aug 31, 2022 3:07 am |   |  
				| 
 |  
				|  	  | newguy wrote: |  	  | The issue is the CCS compiler (for all versions except for PCD [16 bit compiler]) treats all variables as unsigned by default. Unless you explicitly say "signed" in front of your int32 declaration, the compiler treats that variable as unsigned. 
 With PCD, the compiler treats all variables as signed by default.
 | 
 
 Thank you very much for the information. After making the relevant correction, I receive such a warning. Should I take it into account?
 
 >>> Warning 216 Line 489(2,3): Interrupts disabled during call to prevent re-entrancy: (@DIVS3232)
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Aug 31, 2022 5:50 am |   |  
				| 
 |  
				| It means you are using signed 32bit integer arithmetic somewhere in one of your interrupt handlers.
 Ask yourself the question 'do I really need to'?.
 Integer arithmetic is pretty fast, but division is the slowest.
 Generally it is good practice to minimise the complexity of everything
 done inside such handlers.
 Most operations inside handlers can be better done with smaller and
 faster operations.
 |  | 
	
		|  | 
	
		|  |