+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Member level 3
    Points: 489, Level: 4

    Join Date
    Aug 2018
    Posts
    61
    Helped
    0 / 0
    Points
    489
    Level
    4

    Moving The bit value to array

    Dear all

    I have digital IO which reads high(1) or LOW(0). I have created char buf which read the 1 byte of data thats 8 bits
    Currently i am moving my data like this .Where i store the values like this

    need simple code where i could able to move 0A into Buffer that buffer value to be move to ser_data[]
    ser_data[3]=0X0A;
    ser_data[4]=0XFF;
    ser_data[5]=0XA0;
    ser_data[6]=0XFF;

    0A means -> 8 digital Io -> 0000 1010 this is value it read.This might change Io pin state changes. SImple code to move bit value to array to form byte

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 81,718, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,401
    Helped
    4471 / 4471
    Points
    81,718
    Level
    69

    Re: Moving The bit value to array

    I don't understand your question. Do you mean you want to convert 8 bits arriving one at a time into a byte?
    If that is what you want, either use a UART if there is one or you can construct a shift register in software to assemble the bits together.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  3. #3
    Member level 3
    Points: 489, Level: 4

    Join Date
    Aug 2018
    Posts
    61
    Helped
    0 / 0
    Points
    489
    Level
    4

    Re: Moving The bit value to array

    I have written simple code in arduino platform.

    Here INF will store the digital status of Input configured. Now i wanted to move all 8 pin digital IO to ser_data[] buffer and print Ser_data to represent 8 bit

    if all digital io read is FF means all input are 1 then

    Ser_data= 0XFF where all bit represent like this

    INPUT_T.INF1 = 1;
    INPUT_T.INF2 = 1;
    INPUT_T.INF3 = 1;
    INPUT_T.INF4 = 1;
    INPUT_T.INF5 = 1;
    INPUT_T.INF6 = 1;
    INPUT_T.INF7 = 1;
    INPUT_T.INF8= 1;

    Ser_data= 0X0A

    INPUT_T.INF1 = 0;
    INPUT_T.INF2 = 1;
    INPUT_T.INF3 = 0;
    INPUT_T.INF4 = 1;
    INPUT_T.INF5 = 0;
    INPUT_T.INF6 = 0;
    INPUT_T.INF7 = 0;
    INPUT_T.INF8= 0;




    Code:
    unsigned char ser_data[10];
    #define LED D1
    
    typedef union
    {
      struct
      {
        unsigned INF1: 1;
        unsigned INF2: 1;
        unsigned INF3: 1;
        unsigned INF4: 1;
        unsigned INF5: 1;
        unsigned INF6: 1;
        unsigned INF7: 1;
        unsigned INF8: 1;
      };
      unsigned char INF1_9;
    } INPUT_TYPE;
    
    INPUT_TYPE INPUT_T;
    
    void setup() {
      // put your setup code here, to run once:
    
      Serial.begin(9600);
      pinMode(0, OUTPUT);
      pinMode(D1, INPUT);
    
    }
    
    void loop()
    {
      // put your main code here, to run repeatedly:
    
      if (digitalRead(0) == 1)
      {
        INPUT_T.INF1 = 1;
    
      } else
      {
        INPUT_T.INF1 = 0;
      }
    
      if (digitalRead(D1) == 1)
      {
        INPUT_T.INF2 = 1;
        Serial.println("Pin high");
      } else
      {
        INPUT_T.INF2 = 0;
        Serial.println("Pin LOW");
      }
      delay(1000);
    
    
    }
    Last edited by BradtheRad; 23rd October 2018 at 00:52. Reason: Boldfaced example commands



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 78,448, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,912
    Helped
    3610 / 3610
    Points
    78,448
    Level
    68

    Re: Moving The bit value to array

    Hi,

    I also don´t understand the question.
    In my eyes you mix different problems.
    1) to get a byte from 8 input pins (one complete 8 bit port)
    2) to store a byte in an array
    3) to trnsmit one byte of the array via UART

    Please focus on one problem.

    for 1):
    Just reading whole port input state will give you the desired byte like 0x0A. No need to convert anything.

    ****
    For further discussion: you should tell which microcontroller and which compiler you use.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--