| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| Doralice 
 
 
 Joined: 03 Feb 2011
 Posts: 5
 Location: Florence, Italy
 
 
			    
 
 | 
			
				| Variables bit access |  
				|  Posted: Wed Apr 20, 2011 7:14 am |   |  
				| 
 |  
				| Hi all! 
 How can I access directly a single bit of a variable, for example an int8 variable?
 
 I need it because I work with PIC12F683 and I have to use ADC and PWM with 10 bits. I'll store the result of the conversion and the duty cycle in two 16 bits variables.
 
 Otherway I'll do this:
 
 
  	  | Code: |  	  | unsigned int16 pippo,pluto;
 
 pippo=ADRESL;
 pippo=pippo>>6;
 pluto=ADRESH;
 pluto=pluto<<2;
 10_bit_result=pippo+pluto;
 
 | 
 
 and something similar with duty cycle...
 
 Doralice
 _________________
 Bugs will appear in one part of a working program when another 'unrelated' part is modified.
 
 Doralice
 |  | 
	
		|  | 
	
		| SherpaDoug 
 
 
 Joined: 07 Sep 2003
 Posts: 1640
 Location: Cape Cod Mass USA
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Apr 20, 2011 8:06 am |   |  
				| 
 |  
				| CCS has bit-set(), bit_clear(), and bit_test().  A more transportable method is to AND or OR with a bit mask. _________________
 The search for better is endless.  Instead simply find very good and get the job done.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Apr 20, 2011 8:37 am |   |  
				| 
 |  
				| Er. Why bother?. If you have selected #device ADC=10, the compiler does the ADC conversion for you. Just:
 
  	  | Code: |  	  | int16 adcval;
 
 adcval=read_adc();
 
 
 | 
 This puts the 10bit result, right justified into a 16 bit variable for you.
 Similarly, with the PWM:
 
  	  | Code: |  	  | int16 duty;
 
 duty =500
 set_pwm1_duty(duty);
 
 | 
 
 Writes the 10 bit value from the bottom 10 bits of the 16bit value 'duty', into the two registers for the PWM.
 
 The compiler does jobs like this for you, faster than you could code them
 
 You are 'making work'. The chip can be told to return the number right justified or left justified. What you show is the code needed to convert a left justified number (#device ADC=16) to a right justified one. But why not just set the chip to return the number right justified?....
 
 Best Wishes
 |  | 
	
		|  | 
	
		| Doralice 
 
 
 Joined: 03 Feb 2011
 Posts: 5
 Location: Florence, Italy
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Apr 20, 2011 8:54 am |   |  
				| 
 |  
				| Thank you both! 
 You've given me some useful advice.
 I didn't know such kinds of functions.
 
  _________________
 Bugs will appear in one part of a working program when another 'unrelated' part is modified.
 
 Doralice
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Apr 20, 2011 10:44 am |   |  
				| 
 |  
				| Pressing F11 while in a project will open up the onscreen help file. Jammed FULL of useful stuff to know! I leave it open all the time, to quickly reference when I ned to know something about a function or error messages. |  | 
	
		|  | 
	
		|  |