A First Command

After discovering that there is at least one device connected to the bus the master has to issue a ROM command. In many cases the ROM command used first will be the Search ROM command which enumerates the 64-bit codes of all of the devices on the bus. After collecting all of these codes the master can used the Match ROM commands with a specific 64-bit code to select the device the master wants to talk to. 

While it is perfectly possible to implement the Search ROM procedure it is simpler to work with the single device by using commands which ignore the 64-bit code and address all of the devices on the bus at the same time. Of course this only works as long as there is only one device on the bus. 

If there is only one device then we can use the Skip ROM command 0xCC to tell all the devices on the bus to be active. 

As we have a writeBit function this is easy: 

void sendskip(){
 writeBit(0);
 writeBit(0);
 writeBit(1);
 writeBit(1);

 writeBit(0);
 writeBit(0);
 writeBit(1);
 writeBit(1);
}

Notice that 0xCC is 1100 1100 in binary and the 1-wire bus sends the least significant bit first.

If you try this out you should find it works but device doesn't respond because it is waiting for another command.

Again as the time between writing bits isn't critical we can take this first implementation of the function and write something more general if slightly slower.  

The writeByte function will write the low 8 bits of an int to the device: 

void writeByte(int byte){
int i;
 for(i=0;i<8;i++){
  if(byte & 1){
   writeBit(1);
  }else{
   writeBit(0);
  }
  byte=byte>>1;
 }
}

 

Using this we can send a Skip ROM command using:

writeByte(0xCC);

You can see the pattern of bits sent on a logic analyzer:

Reading Bits

We already know how the master sends a one and a zero the protocol for the slave device is exactly the same except that the master still provides the slots starting pulse. 

That is the master starts a 60 microsecond slot by pulling the bus down for a bit more than 1 microsecond. Then the slave device either holds the line down for a further 15 microseconds minimum or it simply allows the line to float high. See below for the exact timings:

 

So all we have to do to read bits is to pull the line down for just a bit more than 1 microsecond and then sample the bus at the end of a 15 microsecond pause. 

int readBit() {
 volatile int i;
 P0.setDigitalValue(0);
 P0.setDigitalValue(1);for (i = 1; i < 20; i++) {
 };
 int b = P1.getDigitalValue();
 for (i = 1; i < 60; i++) {
 };
 return b;
}

The readBit function pulls the line low for about 4 microsecond and measures the line state at around 15 microseconds. The total slot time is around 70 microseconds.  Again it is better to use busy waits as the time periods are short and need to be repeatable. 

A logic analyzer shows the typical pattern of bits from the device: 

data