# Biphase data encoding =\> rising/falling edge

**URL:** <https://forum.arduino.cc/t/biphase-data-encoding-rising-falling-edge/685478>\
**Category:** Programming\
**Created:** [December 3, 2020, 3:36pm UTC](https://forum.arduino.cc/t/biphase-data-encoding-rising-falling-edge/685478 "2020-12-03T15:36:20Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![sherzaad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sherzaad/32/226173_2.png) [@sherzaad](https://forum.arduino.cc/u/sherzaad)\
**Post date:** [January 7, 2021, 12:24pm UTC](https://forum.arduino.cc/t/biphase-data-encoding-rising-falling-edge/685478/16 "2021-01-07T12:24:53Z")

</div>

> Triv38:  
> Hi,  
> i'm sorry, i make a fault concerning the direction of time on oscilloscope on trace.  
> The time go from left to right and not the inverse.  
> So the first bit sent to output is the bit at the leftmost and the last is at the right. (see my graph trace2 in attached file).
> 
> I wish to have the behavior as the diagram in attached file "trace3".  
> I try to test your code of the function "send\_biphase\_msb\_first" routine but the behavior is not as expected (see "trace4")  
> I have on output "0b1000 0000" instead of "0b0111 1111".
> 
> I continue to work to have the good behavior.

I found where there error was in that routine. just needed to move a line! 😉

```arduino
void send_biphase_msb_first(uint8_t frame, uint8_t bits) {

  uint16_t previous_state = 0x01 & (!digitalRead(TxRx_PIN));
  uint16_t MSB_mask = (1 << (bits - 1));
  uint8_t bit = (frame & MSB_mask > 0) ? 1 : 0;

  for (int i = 0; i < bits * 2; ++i) {

    digitalWrite(TxRx_PIN, (previous_state ^ ((i & 0x01)&bit)));
    if (i & 0x01) {
      frame <<= 1;
      bit = (frame & MSB_mask > 0) ? 1 : 0;
    }
    previous_state = 0x01 & (!digitalRead(TxRx_PIN)); //<--- line moved out of 'if' statement
    delayMicroseconds(T_HBBIT);
  }

}

```

hope that helps...

---

_[View the full topic](https://forum.arduino.cc/t/biphase-data-encoding-rising-falling-edge/685478)._
