# How can we change the state of 3 adjescent bits of a port

#### varunme

Joined Sep 29, 2011
60
How can we change the state of 3 adjescent bits of a port according to the input from another port

eg :

input port : Portc= 10010000
Outputport: Portd= 11111100

the three bits including that is high from the input also on

#### RiJoRI

Joined Aug 15, 2007
536
Read PortC then write it to PortD.

Insofar as you have not even mentioned which micro controller you are interested in, it is impossible to give a more detailed answer.

--Rich

#### t06afre

Joined May 11, 2009
5,934
You can test if the bit are set. Then do a logical or with the corresponding bit(s) you want to set high. The write the result to port d. If you want to be sure bit 6 and 5 is set and the rest unchanged you do a logical or with 0b01100000 (given 8 bit data )

#### atferrari

Joined Jan 6, 2004
4,122
Is the fact of being adjacent adding an unknown difficulty or creating a special situation we cannot foresee?

#### thatoneguy

Joined Feb 19, 2009
6,359
Get out the 3 bits by using an AND mask with 0's to set the bits you don't care about and 1 to set the bits you do care about. e.g. mask = PORTC & 0xe0

Then OR that result with the byte to set the bits that you masked out using the AND e.g.PORTD = PORTD | mask

Last edited:

#### varunme

Joined Sep 29, 2011
60
yes,

I thought of something
Lighting three lights is difficult, so i modified to only one adjescent bit

inputstate = portc;
shift_res = 1>>inputstate;
adj_lit_result= shift_res & inputstate; // so that we can get adjescent bit high

isnt it ?,

I want to accomplish, if the one sensor is intercepted by something the corresponding light to sensor and the adjescent light have to go high, when the next sensor is intercepted, the corresponding light and its adjescent light has to go high and goes on according to the attached image, if a next sensor is not intercepted then that light should hold high until a certain time is reached to shut off it.

Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
Ok, let me get the application straight.

You have a conveyor belt, and 3 proximity sensors at fixed spacing along the belt.

As an item (smaller than the spacing between sensors) moves down the conveyor belt:
When it triggers sensor 1, LED1 will light for the duration the sensor is triggered.
When the item passes sensor 2, LED 2 lights, staying on for the duration the sensor is triggered.
When the item passes sensor 3, LED 3 lights, staying on for the duration the sensor is triggered, however, if there is no trigger from sensor 3 after receiving triggers from sensor1 and sensor2, you want an alarm or other action to take place.

Let me know if the above is roughly correct, the conveyor belt can go at infinite speed, and the items can be as large or small as desired.

What frequency would the sensors in your application be activated at?
Is there a chance that more than one sensor will be activated at the same time?

I didn't understand the picture, so let me know if the analogy above is correct.

#### varunme

Joined Sep 29, 2011
60
Ok, let me get the application straight.

You have a conveyor belt, and 3 proximity sensors at fixed spacing along the belt.

As an item (smaller than the spacing between sensors) moves down the conveyor belt:
When it triggers sensor 1, LED1 will light for the duration the sensor is triggered.
When the item passes sensor 2, LED 2 lights, staying on for the duration the sensor is triggered.
When the item passes sensor 3, LED 3 lights, staying on for the duration the sensor is triggered, however, if there is no trigger from sensor 3 after receiving triggers from sensor1 and sensor2, you want an alarm or other action to take place.

Let me know if the above is roughly correct, the conveyor belt can go at infinite speed, and the items can be as large or small as desired.

What frequency would the sensors in your application be activated at?
Is there a chance that more than one sensor will be activated at the same time?

I didn't understand the picture, so let me know if the analogy above is correct.
yes almost correct,
But light1 has to be at 100% till a next sensor is activated.
It is a streetlight application,
once a vehicle activates the sensor and goes, then the light has to be at 100% intensity till an adjescent sensor is activated, when the adjescent sensor is activated then the first light goes 50% unless otherwise its corresponding sensor is activated, if the first one's sensor too is activated then that one too goes 100%.
if one adjescent sensor is not activated then the light which is last activated has to hold its 100%

This code almost does it , excluding that if one adjesent sensor didnot activated the the previous light holds it 100%

Rich (BB code):
input_state = portc; // store the input value
portd |= input_state  // set to high the outputs that represent an input that has 1

portd ^= ~input_state;   // invert the portd bits when the same bit in input_state is 0 , leaves bits with 1 in input_state unchanged
delay_ms(a);
portd ^= ~input_state;  // invert the portd bits when the same bit in input_state is 0
delay_ms(b);

Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
What compiler are you using?

I'm thinking along the lines of

if (test_bit(x-1))
{
set_bit(x);
}

Or something similar. As you have delays, you can extend the code that much without having an issue.

I'll admit to be confused about the 50% and 100% on street lights.

I'm thinking of a circular conveyor belt now.

#### varunme

Joined Sep 29, 2011
60
I'll admit to be confused about the 50% and 100% on street lights.
as a manner for conservation of energy in less traffic period , the light glows where there is someone
I am using mikroC

Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
So would the test_bit pseudo-code I posted above work?

If not, when do you want them to go from 50% to 100%, and how are you doing the 50% PWM?

What controls the brightness, in other words?