en.radzio.dxp.pl

1-wire master driver
1-wire devices, for example DS18B20, are very often used in microcontroller projects. This library use Delay library and can be used with any AVR device.

1-wire bus reset routine
Each transmission via 1-wire bus are started from Reset signal. Source code of OWReset routine is shown below:

;------------------------------------------------------------------------------
; Output : T - presence bit
;------------------------------------------------------------------------------
OWReset:
  cbi   OW_PORT,OW_DQ
  sbi   OW_DDR,OW_DQ
  ldi   XH, HIGH(DVUS(470))
  ldi   XL, LOW(DVUS(470))
  rcall Wait4xCycles
  cbi   OW_DDR,OW_DQ
  ldi   XH, HIGH(DVUS(70))
  ldi   XL, LOW(DVUS(70))
  rcall Wait4xCycles
  set
  sbis  OW_PIN,OW_DQ
  clt
  ldi   XH, HIGH(DVUS(240))
  ldi   XL, LOW(DVUS(240))
  rcall Wait4xCycles
  ret

After call this routine in T flag in SREG are placed presence bit from slave 1-wire device (0 if device is present, 1 otherwise)

Write single bit to 1-wire bus
Basic write operation. Before call this routine to C flag in SREG must be loaded bit value to send. Source code of OWWriteBit routine is shown below:

;------------------------------------------------------------------------------
; Input : C - bit to write
;------------------------------------------------------------------------------
OWWriteBit:
  brcc  OWWriteZero
  ldi   XH, HIGH(DVUS(1))
  ldi   XL, LOW(DVUS(1))
  rjmp  OWWriteOne
OWWriteZero:
  ldi   XH, HIGH(DVUS(120))
  ldi   XL, LOW(DVUS(120))
OWWriteOne:
  sbi   OW_DDR, OW_DQ
  rcall Wait4xCycles
  cbi   OW_DDR, OW_DQ

  ldi   XH, HIGH(DVUS(60))
  ldi   XL, LOW(DVUS(60))
  rcall Wait4xCycles
  ret

Write byte to 1-wire bus
This routine rotate r16 through C flag and calls OWWriteBit 8 times in loop for transmit entire byte. Source code of OWWriteByte iis shown below:

;------------------------------------------------------------------------------
; Input : r16 - byte to write
;------------------------------------------------------------------------------
OWWriteByte:
  push  OWCount
  ldi   OWCount,0
OWWriteLoop:
  ror   r16
  rcall OWWriteBit
  inc   OWCount
  cpi   OWCount,8
  brne  OWWriteLoop
  pop   OWCount
  ret

Read single bit from 1-wire bus
Basic read operation. After execute this routine C flag i SREG contains received bit.

;------------------------------------------------------------------------------
; Output : C - bit from slave
;------------------------------------------------------------------------------
OWReadBit:
  ldi   XH, HIGH(DVUS(1))
  ldi   XL, LOW(DVUS(1))
  sbi   OW_DDR, OW_DQ
  rcall Wait4xCycles
  cbi   OW_DDR, OW_DQ
  ldi   XH, HIGH(DVUS(5))
  ldi   XL, LOW(DVUS(5))
  rcall Wait4xCycles
  clt
  sbic  OW_PIN,OW_DQ
  set
  ldi   XH, HIGH(DVUS(50))
  ldi   XL, LOW(DVUS(50))
  rcall Wait4xCycles
  sec
  brts  OWReadBitEnd
  clc
OWReadBitEnd:
  ret

Read byte from 1-wire bus
This routine call OWReadBit and rotate r16 through C flag. After 8 rotates, r16 contains received byte.

;------------------------------------------------------------------------------
; Output : r16 - byte from slave
;------------------------------------------------------------------------------
OWReadByte:
  push  OWCount
  ldi   OWCount,0
OWReadLoop:
  rcall OWReadBit
  ror   r16
  inc   OWCount
  cpi   OWCount,8
  brne  OWReadLoop
  pop   OWCount
  ret

How to use 1-wire library
In file with entry code place include directive :

#include 1-wire.asm" ; AVR Assembler v2

// or

.include "1-wire.asm" ; AVR Assembler v1

Download library : 1-wire.zip
This library require Delay library
See also : Computing CRC for 1-wire devices 1-wire serial number reading

 

 
(c) Radosław Kwiecień