View previous topic :: View next topic |
Author |
Message |
arga
Joined: 09 Sep 2003 Posts: 22
|
Shift Issue |
Posted: Thu Jan 08, 2004 9:00 pm |
|
|
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
|
|
Posted: Thu Jan 08, 2004 9:56 pm |
|
|
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
|
|
Posted: Thu Jan 08, 2004 10:22 pm |
|
|
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
|
|
Posted: Thu Jan 08, 2004 10:24 pm |
|
|
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
|
|
Posted: Fri Jan 09, 2004 8:02 am |
|
|
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 |
Posted: Fri Jan 09, 2004 12:30 pm |
|
|
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
|
|
|
|