This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

bit banging: how it works + examples

Other Parts Discussed in Thread: MSP430G2553

Hi Everybody,

I need to do a project with bit banging to get data from an external adc

I'm using msp430g2553 and ccs for this project.

I think i can use SPI but i want to learn.

I new to bit banging and i wanted to know how it works (how we get/send data) and an examples will be great.

Thabnk you all.

  • Here is an example how to send data - since every read operation is also a write operation you only have to add the functionality to read an input after the desired transition of clock.



    Dennis

  • Thank you for the reply,
    according to the example: p1.1 is data and p1.2 is clock and clock pin 's out is set to 0(low)
    when i send the data if the left most bit is 1 then i change the data pin's out 1 else to 0
    and shift left for the next bit

    a question about this:
    do i need to set P1DIR |= DATA_OUT_PIN so the direction should be output(for sending)?

    from your example i wrote a bitbang_data_in and i wanted to know if the logic ok:

    void bitbang_data_in()
    {
    uint8_t data = 0;
    uint8_t i;

    for( i = 0; i < 8; i++ )
    {

    data |= (DATA_OUT_PIN & 0x01); //check the right most bit if it is 1 or 0 and set it to data
    CLOCK_OUT_PORT |= CLOCK_OUT_PIN;
    CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;

    data >>=1; // shift to right for reading and setting the next bit
    }
    }

    the logic is correct?
  • Joe Shoshana said:
    do i need to set P1DIR |= DATA_OUT_PIN so the direction should be output(for sending)?

    You would need to do this for both, data and clock (this wasn't shown in the example), but not because it is sending - you need to do this to configure the pin to be an output that is driven by the microcontroller. You now need to specify another pin as input. The function depends on your slave...if data is captured at the falling or rising edge of clock and if clock idles low or high.

    Example:

    uint8_t bitbang_databyte_in( void ) // Clock idles low, data changed on the 1st edge (rising), captured on 2nd (falling)
    {
      uint8_t received_data = 0x00;
    
      CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
      SLAVE_SELECT_OUT_PORT &= ~SLAVE_SELECT_OUT_PIN;
    
      for( i = 0; i < 8; i++ )
      {
        CLOCK_OUT_PORT |= CLOCK_OUT_PIN;
        CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
    
        if( DATA_IN_PORT & DATA_IN_PIN )
        {
          received_data |= 0x01;
        }
        
        received_data <<= 1;
      }
    
      SLAVE_SELECT_OUT_PORT |= SLAVE_SELECT_OUT_PIN;
    
      return received_data;
    }

    Dennis

  • Oh I see it is for the somehow strange interface of the HX711 from your other post here:

  • Yes i posted a few minutes ago
    after your advise about the bit banging
    i tried it but still can't get nothing
    and the hx711 doesn't have another pin
  • Joe Shoshana said:
    and the hx711 doesn't have another pin

    Yes, I posted before I knew it was for the HX711 - this one only has DOUT and it counts additional clock pulses for configuration.

**Attention** This is a public forum