CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Variables bit access

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Doralice



Joined: 03 Feb 2011
Posts: 5
Location: Florence, Italy

View user's profile Send private message

Variables bit access
PostPosted: Wed Apr 20, 2011 7:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 20, 2011 8:06 am     Reply with quote

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: 19255

View user's profile Send private message

PostPosted: Wed Apr 20, 2011 8:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 20, 2011 8:54 am     Reply with quote

Thank you both!

You've given me some useful advice.
I didn't know such kinds of functions.
Smile
_________________
Bugs will appear in one part of a working program when another 'unrelated' part is modified.

Doralice
temtronic



Joined: 01 Jul 2010
Posts: 9133
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 20, 2011 10:44 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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