Application Note 2420 1-Wire Communication With A Microchip Picmicro Microcontroller

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Application Note 2420 1-Wire Communication with a Microchip PICmicro Microcontroller

www.maxim-ic.com

INTRODUCTION
Microchips PICmicro microcontroller devices (PICs) have become a popular design choice for low-power and lowcost system solutions. The microcontrollers have multiple general-purpose input/output (GPIO) pins, and can be easily configured to implement Dallas Semiconductors 1-Wire protocol. The 1-Wire protocol allows interaction with many Dallas Semiconductor parts including battery and thermal management, memory, iButtons , and more. This application note will present general 1-Wire routines for a PIC16F628 and explain the timing and associated details. For added simplicity, a 4MHz clock is assumed for all material presented, and this frequency is available as an internal clock on many PICs. Appendix A of this document contains an include file with all 1-Wire routines. Appendix B presents a sample assembly code program designed for a PIC16F628 to read from a DS2761 HighPrecision Li+ Battery Monitor. This application note is limited in scope to regular speed 1-Wire communication.

General Macros
In order to transmit the 1-Wire protocol as a master, only two GPIO states are necessary: high impedance and logic low. The following PIC assembly code snippets achieve these two states. The PIC16F628 has two GPIO ports, PORTA and PORTB. Either of the ports could be setup for 1-Wire communication, but for this example, PORTB is used. Also, the following code assumes that a constant DQ has been configured in the assembly code to indicate which bit in PORTB will be the 1-Wire pin. Throughout the code, this bit number is simply called DQ. Externally, this pin must be tied to a power supply via a pullup resistor. OW_HIZ:MACRO ;Force the DQ line into a high impedance state. BSF STATUS,RP0 ; Select Bank 1 of data memory BSF TRISB, DQ ; Make DQ pin High Z BCF STATUS,RP0 ; Select Bank 0 of data memory ENDM OW_LO:MACRO ;Force the DQ line to a logic low. BCF STATUS,RP0 ; Select Bank 0 of data memory BCF PORTB, DQ ; Clear the DQ bit BSF STATUS,RP0 ; Select Bank 1 of data memory BCF TRISB, DQ ; Make DQ pin an output BCF STATUS,RP0 ; Select Bank 0 of data memory ENDM Both of these snippets of code are written as macros. By writing the code as a macro, it is automatically inserted into the assembly source code by using a single macro call. This limits the number of times the code must be rewritten. The first macro, OW_HIZ, forces the DQ line to a high impedance state. The first step is to choose the bank 1 of data memory because the TRISB register is located in bank 1. Next, the DQ output driver is changed to a high impedance state by setting the DQ bit in the TRISB register. The last line of code changes back to bank 0 of data memory. The last line is not necessary, but is used so that all macros and function calls leave the data memory in a known state. The second macro, OW_LO, forces the DQ line to a logic low. First, bank 0 of data memory is selected, so the PORTB register can be accessed. The PORTB register is the data register, and contains the values that will be forced to the TRISB pins if they are configured as outputs.
PICmicro is a registered trademark of Microchip Technology Inc. 1-Wire and iButton are registered trademarks of Dallas Semiconductor.

1 of 10

REV: 090903

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller The DQ bit of PORTB is cleared so the line will be forced low. Finally, bank 1 of data memory is selected, and the DQ bit of the TRISB register is cleared, making it an output driver. As always, the macro ends by selecting bank 0 of data memory. A final macro labeled WAIT is included to produce delays for the 1-Wire signaling. WAIT is used to produce delays in multiples of 5ms. The macro is called with a value of TIME in microseconds, and the corresponding delay time is generated. The macro simply calculates the number of times that a 5ms delay is needed, and then loops within WAIT5U. The routine WAIT5U is shown in the next section. For each instruction within WAIT, the processing time is given as a comment to help understand how the delay is achieved. WAIT:MACRO TIME ;Delay for TIME ms. ;Variable time must be in multiples of 5ms. MOVLW (TIME/5) - 1 ;1ms to process MOVWF TMP0 ;1ms to process CALL WAIT5U ;2ms to process ENDM

General 1-Wire Routines


The 1-Wire timing protocol has specific timing constraints that must be followed in order to achieve successful communication. To aid in making specific timing delays, the routine WAIT5U is used to generate 5ms delays. This routine is shown below. WAIT5U: ;This takes 5ms to complete NOP NOP DECFSZ TMP0,F GOTO WAIT5U RETLW 0

;1ms to process ;1ms to process ;1ms if not zero or 2ms if zero ;2ms to process ;2ms to process

When used in combination with the WAIT macro, simple timing delays can be generated. For example, if a 40ms delay is needed, WAIT 0.40 would be called. This causes the first 3 lines in WAIT to execute resulting in 4ms. Next, the first 4 lines of code in WAIT5U executes in 5ms and loops 6 times for a total of 30ms. The last loop of WAIT5U takes 6ms and then returns back to the WAIT macro. Thus, the total time to process would be 30 + 4 + 6 = 40ms.

Table 1. Regular Speed 1-Wire Interface Timing


2.5V VDD 5.5V, TA = -20C to 70C.) PARAMETER Time Slot Recovery Time Write 0 Low Time Write 1 Low Time Read Data Valid Reset Time High Reset Time Low Presence Detect High Presence Detect Low SYMBOL tSLOT tREC tLOW0 tLOW1 tRDV tRSTH tRSTL tPDH tPDL MIN 60 1 60 1 480 480 15 60 TYP MAX 120 120 15 15 960 60 240 UNITS ms ms ms ms ms ms ms ms ms

The start of any 1-Wire transaction begins with a reset pulse from the master device followed by a presence detect pulse from the slave device. Figure 1 illustrates this transaction. This initialization sequence can easily be transmitted via the PIC, and the assembly code is shown below Figure 1. The 1-Wire timing specifications for initialization, reading, and writing are given above in Table 1. These parameters are referenced throughout the rest of the document. 2 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller

Figure 1. 1-Wire Initialization Sequence


tRSTL tPDH DQ tPDL tRSTH

LINE TYPE LEGEND: Bus master active low Both bus master and DS27XX active low DS27XX active low Resistor pullup

OW_RESET: OW_HIZ CLRF OW_LO WAIT OW_HIZ WAIT BTFSS INCF WAIT RETLW

PDBYTE .500 .70 PORTB,DQ PDBYTE,F .430 0

; Start with the line high ; Clear the PD byte ; Drive Low for 500ms ; Release line and wait 70ms for PD Pulse ; Read for a PD Pulse ; Set PDBYTE to 1 if get a PD Pulse ; Wait 430ms after PD Pulse

The OW_RESET routine starts by ensuring the DQ pin is in a high impedance state so it can be pulled high by the pullup resistor. Next, it clears the PDBYTE register so it is ready to validate the next presence detect pulse. After that, the DQ pin is driven low for 500ms. This meets the tRSTL parameter shown in Table 1, and also provides a 20ms additional buffer. After driving the pin low, the pin is released to a high impedance state and a delay of 70ms is added before reading for the presence detect pulse. Using 70ms ensures that the PIC will sample at a valid time for any combination of tPDL and tPDH. Once the presence detect pulse is read, the PDBYTE register is adjusted to show the logic level read. The DQ pin is then left in a high-impedance state for an additional 430ms to ensure that the tRSTH time has been met, and includes a 20ms additional buffer. The next routine needed for 1-Wire communication is DSTXBYTE, which is used to transmit data to a 1-Wire slave device. The PIC code for this routine is shown below Figure 2. This routine is called with the data to be sent in the WREG register, and it is immediately moved to the IOBYTE register. Next, a COUNT register is initialized to 8 to count the number of bits sent out the DQ line. Starting at the DSTXLP, the PIC starts sending out data. First the DQ pin is driven low for 5ms regardless of what logic level is sent. This ensures the tLOW1 time is met. Next, the lsb of the IOBYTE is shifted into the CARRY bit, and then tested for a one or a zero. If the CARRY is a one, the DQ bit of PORTB is set which changes the pin to a high impedance state and the line is pulled high by the pullup resistor. If the CARRY is a zero, the line is kept low. Next a delay of 60ms is added to allow for the minimum tLOW0 time. After the 60ms wait, the pin is changed to a high impedance state, and then an additional 2ms are added for pullup resistor recovery. Finally, the COUNT register is decremented. If the COUNT register is zero, all eight bits have been sent and the routine is done. If the COUNT register is not zero, another bit is sent starting at DSTXLP. A visual interpretation of the write zero and write one procedure is shown in Figure 2.

3 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller

Figure 2. 1-Wire Write Time Slots


WRITE 0 SLOT tSLOT tLOW0 VPULLUP GND
LINE TYPE LEGEND: Bus master active low Both bus master and 1-Wire Device active low 1-Wire Device active low Resistor pullup

tLOW1 tREC

WRITE 1 SLOT tSLOT

DSTXBYTE: MOVWF MOVLW MOVWF DSTXLP: OW_LO NOP NOP NOP NOP NOP RRF BTFSC BSF WAIT OW_HIZ NOP NOP DECFSZ GOTO RETLW

IOBYTE .8 COUNT

; Byte to send starts in W ; We send it from IOBYTE ; Set COUNT equal to 8 to count the bits

IOBYTE,F STATUS,C PORTB,DQ .60

; Drive the line low for 5ms ; The data byte ; Check the LSB of IOBYTE for 1 or 0 ; Drive the line high if LSB is 1 ; Continue driving line for 60ms ; Release the line for pullup ; Recovery time of 2ms ; Decrement the bit counter

COUNT,F DSTXLP 0

The final routine for 1-Wire communication is DSRXBYTE, which allows the PIC to receive information from a slave device. The code is shown below Figure 3. The COUNT register is initialized to 8 before any DQ activity begins and its function is to count the number of bits received. The DSRXLP begins by driving the DQ pin low to signal to the slave device that the PIC is ready to receive data. The line is driven low for 6ms, and then released by putting the DQ pin into a high impedance state. Next, the PIC waits an additional 4ms before sampling the data line. There is 1 line of code in OW_LO after the line is driven low, and 3 lines of code within OW_HIZ. Each line takes 1ms to process. Adding up all the time results in 1 + 6 + 3 + 4 = 14ms which is just below the tRDV spec of 15ms. After the PORTB register is read, the DQ bit is masked off, and then the register is added to 255 to force the CARRY bit to mirror the DQ bit. The CARRY bit is then shifted into IOBYTE where the incoming byte is stored. Once the byte is stored a delay of 50ms is added to ensure that tSLOT is met. The last check is to determine if the COUNT register is zero. If it is zero, 8 bits have been read, and the routine is exited. Otherwise, the loop is repeated at DSRXLP. The read zero and read one transactions are visually shown in Figure 3.

4 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller

Figure 3. 1-Wire Read Time Slots


READ 0 SLOT tSLOT tREC VPULLUP GND
Master Sample Window >1ms Master Sample Window

READ 1 SLOT tSLOT

tRDV
LINE TYPE LEGEND: Bus master active low Both bus master and 1-Wire Device active low

tRDV

1-Wire Device active low Resistor pullup

DSRXBYTE: MOVLW MOVWF DSRXLP: OW_LO NOP NOP NOP NOP NOP NOP OW_HIZ NOP NOP NOP NOP MOVF ANDLW ADDLW RRF WAIT DECFSZ GOTO RETLW 0

; Byte read is stored in IOBYTE .8 COUNT ; Set COUNT equal to 8 to count the bits

; Bring DQ low for 6ms

PORTB,W 1<<DQ .255 IOBYTE,F .50 COUNT,F DSRXLP

; Change to HiZ and Wait 4ms ; Read DQ ; Mask off the DQ bit ; C = 1 if DQ = 1: C = 0 if DQ = 0 ; Shift C into IOBYTE ; Wait 50ms to end of time slot ; Decrement the bit counter

SUMMARY
Dallas Semiconductors 1-Wire communication protocol can easily be implemented on Microchips PICmicro line of microcontrollers. In order to complete 1-Wire transactions, only two GPIO states are needed, and the multiple GPIOs on a PIC are easily configured for this task. There are three basic routines necessary for 1-Wire communication: Initialization, Read Byte, and Write Byte. These three routines have been presented and thoroughly detailed to provide accurate 1-Wire regular speed communication. This allows a PIC to interface with any of the many Dallas Semiconductor 1-Wire devices. Appendix A of this document has all three routines in a convenient include file. Appendix B contains a small assembly program meant to interface a PIC16F628 to a DS2761 High-Precision Li+ Battery Monitor. 5 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller

APPENDIX A: 1-WIRE INCLUDE FILE (1W_16F6X.INC)


; ******************************************************* ; ; Dallas 1-Wire Support for PIC16F628 ; ; Processor has 4MHz clock and 1ms per instruction cycle. ; ; ******************************************************* ; ******************************************************* ; Dallas Semiconductor 1-Wire MACROS ; ******************************************************* OW_HIZ:MACRO BSF STATUS,RP0 ; Select Bank 1 of data memory BSF TRISB, DQ ; Make DQ pin High Z BCF STATUS,RP0 ; Select Bank 0 of data memory ENDM ; -------------------------------------------------------OW_LO:MACRO BCF STATUS,RP0 ; Select Bank 0 of data memory BCF PORTB, DQ ; Clear the DQ bit BSF STATUS,RP0 ; Select Bank 1 of data memory BCF TRISB, DQ ; Make DQ pin an output BCF STATUS,RP0 ; Select Bank 0 of data memory ENDM ; -------------------------------------------------------WAIT:MACRO TIME ;Delay for TIME ms. ;Variable time must be in multiples of 5ms. MOVLW (TIME/5)-1 ;1ms MOVWF TMP0 ;1ms CALL WAIT5U ;2ms ENDM ; ******************************************************* ; Dallas Semiconductor 1-Wire ROUTINES ; ******************************************************* WAIT5U: ;This takes 5uS to complete NOP ;1ms NOP ;1ms DECFSZ TMP0,F ;1ms or 2ms GOTO WAIT5U ;2ms RETLW 0 ;2ms ; -------------------------------------------------------OW_RESET: OW_HIZ ; Start with the line high CLRF PDBYTE ; Clear the PD byte OW_LO WAIT .500 ; Drive Low for 500ms OW_HIZ WAIT .70 ; Release line and wait 70ms for PD Pulse BTFSS PORTB,DQ ; Read for a PD Pulse INCF PDBYTE,F ; Set PDBYTE to 1 if get a PD Pulse 6 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller WAIT .400 ; Wait 400ms after PD Pulse RETLW 0 ; -------------------------------------------------------DSRXBYTE: ; Byte read is stored in IOBYTE MOVLW .8 MOVWF COUNT ; Set COUNT equal to 8 to count the bits DSRXLP: OW_LO NOP NOP NOP NOP NOP NOP ; Bring DQ low for 6ms OW_HIZ NOP NOP NOP NOP ; Change to HiZ and Wait 4ms MOVF PORTB,W ; Read DQ ANDLW 1<<DQ ; Mask off the DQ bit ADDLW .255 ; C=1 if DQ=1: C=0 if DQ=0 RRF IOBYTE,F ; Shift C into IOBYTE WAIT .50 ; Wait 50ms to end of time slot DECFSZ COUNT,F ; Decrement the bit counter GOTO DSRXLP RETLW 0 ; -------------------------------------------------------DSTXBYTE: ; Byte to send starts in W MOVWF IOBYTE ; We send it from IOBYTE MOVLW .8 MOVWF COUNT ; Set COUNT equal to 8 to count the bits DSTXLP: OW_LO NOP NOP NOP NOP NOP ; Drive the line low for 5ms RRF IOBYTE,F ; The data byte BTFSC STATUS,C ; Check the LSB of IOBYTE for 1 or 0 BSF PORTB,DQ ; Drive the line high if LSB is 1 WAIT .60 ; Continue driving line for 60ms OW_HIZ ; Release the line for pullup NOP NOP ; Recovery time of 2ms DECFSZ COUNT,F ; Decrement the bit counter GOTO DSTXLP RETLW 0 ; --------------------------------------------------------

7 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller

APPENDIX B: PIC16F628 TO DS2761 ASSEMBLY CODE (PIC_2_1W.ASM)


; ******************************************* ; ; Dallas Semiconductor PIC code ; ; This code will interface a PIC16F628 microcontroller to ; a DS2761 High-Precision Li+ Battery Monitor ; ; *******************************************; ; ; VCC ; ^ ; | ; | ; / ; \ Rpup ; / ; \ ; | ; 16F628 | DS2761 ; RB1 (pin 7) ------------------------------ DQ (pin 7) ; ; *******************************************; ;--------------------------------------------------------; List your processor here. list p=16F628 ; Include the processor header file here. #include <p16F628.inc> ;--------------------------------------------------------; Assign the PORTB with Constants constant DQ=1 ; Use RB1 (pin7) for 1-Wire ;-------------------------------------------------------; These constants are standard 1-Wire ROM commands constant SRCHROM=0xF0 constant RDROM=0x33 constant MTCHROM=0x55 constant SKPROM=0xCC ;--------------------------------------------------------; These constants are used throughout the code cblock 0x20 IOBYTE TMP0 COUNT PICMSB PICLSB PDBYTE

; Address 0x23 ; Keep track of bits ; Store the MSB ; Store the LSB ; Presence Detect Pulse

endc ;--------------------------------------------------------; Setup your configuration word by using __config. 8 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller ; For the 16F628, the bits are: _____ ; CP1,CP0,CP1,CP0,N/A, CPD, LVP, BODEN, MCLRE, FOSC2, PWRTE, WDTE, FOSC1, FOSC0 ; CP1 and CP0 are the Code Protection bits ; CPD: is the Data Code Protection Bit ; LVP is the Low Voltage Programming Enable bit ; PWRTE is the power-up Timer enable bit ; WDTE is the Watchdog timer enable bit ; FOSC2, FOSC1 and FOSC0 are the oscillator selection bits. ; CP disabled, LVP disabled, BOD disabled, MCLR enabled, PWRT disabled, WDT disabled, INTRC I/O oscillator ; 11111100111000 __config 0x3F38 ;--------------------------------------------------------; Set the program origin for subsequent code. org 0x00 GOTO SETUP NOP NOP NOP GOTO INTERRUPT ; PC 0x04...INTERRUPT VECTOR! ;--------------------------------------------------------INTERRUPT: SLEEP ;--------------------------------------------------------; Option Register bits ; ____ ; RBPU,INTEDG,TOCS,TOSE,PSA,PS2,PS1,PS0 ; 7=PORTB Pullup Enable, 6=Interrupt Edge Select, 5=TMR0 Source, ; 4=TMR0 Source Edge, 3=Prescaler Assign, 2-0=Prescaler Rate Select ; 11010111 ; PORTB pullups disabled,rising edge,internal,hightolow,TMR0,1:256 SETUP: BCF STATUS,RP1 BSF STATUS,RP0 MOVLW 0xD7 MOVWF OPTION_REG BCF STATUS,RP0 ;--------------------------------------------------------BCF INTCON,7

; Select Bank 1 of data memory ; Select Bank 0 of data memory ; Disable all interrupts.

;--------------------------------------------------------GOTO START ;--------------------------------------------------------; Include the 1-Wire communication routines and macros #INCLUDE 1w_16f6x.inc ;--------------------------------------------------------START: ;--------------------------------------------------------GET_TEMP: CALL OW_RESET BTFSS PDBYTE,0

; Send Reset Pulse and read for Presence Detect Pulse ; 1 = Presence Detect Detected 9 of 10

App Note 2420: 1-Wire Communication with a Microchip PICmicro Microcontroller GOTO MOVLW CALL MOVLW CALL MOVLW CALL CALL MOVF MOVWF CALL MOVF MOVWF CALL NOPDPULSE SKPROM DSTXBYTE 0x69 DSTXBYTE 0x0E DSTXBYTE DSRXBYTE IOBYTE,W PICMSB DSRXBYTE IOBYTE,W PICLSB OW_RESET

; Send Skip ROM Command (0xCC) ; Send Read Data Command (0x69) ; Send the DS2761 Current Register MSB address (0x0E) ; Read the DS2761 Current Register MSB ; Put the Current MSB into file PICMSB ; Read the DS2761 Current Register LSB ; Put the Current LSB into file PICLSB ; Add some error processing here! ; Put PIC to sleep

NOPDPULSE: SLEEP ;--------------------------------------------------------end

10 of 10

You might also like