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 CCS Technical Support

Shift Issue

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



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Shift Issue
PostPosted: Thu Jan 08, 2004 9:00 pm     Reply with quote

Consider the following:


aFinal[0] = aTemp[0] ;
aFinal[0] = aBuff[2]<<aBuff[1] ;
aTemp[0] = aFinal[0] ;
PORTD = aFinal[0] ;


The array aBuff[2] holds the bit value, the aBuff[1] holds the amount of shift.

PORTD is used to write data to some device.

Assume that atemp[0] = 0b10000000 at the start.

If aBuff[2] = 3 and aBuff[1] = 1, then PORTD = 0b100001000 because aBuff[1] is being [censored] by aBuff[2] times.
My hardware responds correctly.

However, if aBuff[2] = 3 but aBuff[1] = 0, it doesn't toggle bit 3 back to zero.

Is there something wrong with the way i'm writing it or does shifting only works if the bit being shifted is 1?

I'm using compiler ver 3.173

Please, anyone who can shed light on this one. Like all of us here we have deadlines to meet.

Thanks.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 08, 2004 9:56 pm     Reply with quote

if aBuff[2] = 3 and aBuff[1] = 1 then portd shoud equal 6 or 0b00000110.

if the shifting amount is 0, then the result is what the input was. You didn't change anything. I think maybe you have the 2 vars mixed up. Tell us what you are trying to do and we can help.
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

PostPosted: Thu Jan 08, 2004 10:22 pm     Reply with quote

Basically, what I'm trying to do is change bit values. aTemp[0] is used as a temporary storage. Each bit of PORTD is used to address a lamp via a transparent latch.

I want to be able to toggle a lamp independently. Hence, if, say lamp7 and lamp3 are already ON, and I want to turn off lamp3, aTemp[0] is called back and only the bit pertaining to lamp3 is changed. The position of which bit to change is known through aBuff[2].

I just couldn't focus anymore with too much on my hands right now. I really appreciate your response.
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

PostPosted: Thu Jan 08, 2004 10:24 pm     Reply with quote

One other thing. There was a typo error. The second line s/b OR'ed.

aFinal[0] = aTemp[0] ;
aFinal[0] |= aBuff[2]<<aBuff[1] ;
aTemp[0] = aFinal[0] ;
PORTD = aFinal[0] ;
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Jan 09, 2004 8:02 am     Reply with quote

CCS has some functions bit_set, bit_clear, and bit_test. Take a look at these. They will allow you to set or clear a bit in a variable. If you want to toggle a bit then you can do something like this:

Code:

if (bit_test(var,bit_to_toggle))
  bit_clear(var,bit_to_toggle);
else
  bit_set(var,bit_to_toggle);
chas
Guest







bit toggle
PostPosted: Fri Jan 09, 2004 12:30 pm     Reply with quote

Or you could declare the bit:

#byte port_d = 8
#bit IOBit0 = port_d.0

then to toggle that bit:

IOBit0 = !IOBit0;
burnsy



Joined: 18 Oct 2003
Posts: 35
Location: Brisbane, Australia

View user's profile Send private message Send e-mail Visit poster's website

easy bit manipulation
PostPosted: Sun Jan 11, 2004 5:29 am     Reply with quote

Try this link for an EASY way to twiddle bits on Port D

http://www.ccsinfo.com/forum/viewtopic.php?t=17881&highlight=
_________________
This is the last code change until its ready....
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