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

Discussion in 'Embedded Systems and Microcontrollers' started by varunme, Jan 11, 2012.

  1. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    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
     
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    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
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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 ;) )
     
  4. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Is the fact of being adjacent adding an unknown difficulty or creating a special situation we cannot foresee?
     
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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: Jan 11, 2012
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
  7. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    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 ?,

    [​IMG]

    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: Jan 11, 2012
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  9. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    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%

    Code ( (Unknown Language)):
    1.  
    2. input_state = portc; // store the input value
    3. portd |= input_state  // set to high the outputs that represent an input that has 1
    4.  
    5. portd ^= ~input_state;   // invert the portd bits when the same bit in input_state is 0 , leaves bits with 1 in input_state unchanged
    6. delay_ms(a);
    7. portd ^= ~input_state;  // invert the portd bits when the same bit in input_state is 0
    8. delay_ms(b);
    9.  
     
    Last edited: Jan 12, 2012
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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. :)
     
  11. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    as a manner for conservation of energy in less traffic period , the light glows where there is someone
    I am using mikroC
     
    Last edited: Jan 12, 2012
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
Loading...