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

Bit Stuffing & shifting

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







Bit Stuffing & shifting
PostPosted: Sat Jul 02, 2005 11:20 pm     Reply with quote

Hi all.

Perhaps some of you know how to do this.

Is it possible to have such a thing as a variable that is 14 bits wide only

and then copy say a 8 bit variable into the 14 bit wide variable.


The reason I ask is that I have to load a shift register with data.
Its a pll. One of the registers is a total of 14 bits wide. But I will
never use the full 14 bits. Typically I use only 7 bits in various combinations.

When loading the pll it expects to see the full 14 bits so the load sequence
is correct.

A code snip or insight would be helpful to me.


Thanks Paul.
Ttelmah
Guest







PostPosted: Sun Jul 03, 2005 2:29 am     Reply with quote

No, but yes!...
Basically, a variable inside the C, will always be a mutliple of 8 bits wide. However there is nothing at all to prevent you using just 14bits of a 16bit variable.
So, you can do something like this:
Code:

void output_14(int8 val) {
    int16 big_val;
    int8 count;
    big_val=(int16)val;
    //This 'casts' the 8bit value 'up to 16bits long
    for (count=0;count<14;count++) {
        if (bit_test(big_val,count)) {
            //Here the bit is high, so do what you want with it

        }
        else {
            //Here the bit is low

        }
    }
}

This goes through fourteeen bits, containing the eight bit value 'val' as the eight least significant bits, working LSB first (count the other way if you want MSB first), and you then need to output the data as required in the two branches.

Best Wishes
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