Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5322
Скачиваний: 0
12. One Bit at a Time 357
The various DS1820 functions, such as Convert (44h), Read temperature (BEh), are initiated by the Master sending the appropriate data as 8-bit codes, each byte comprising a Start condition ( \ ) and eight Write
slots, as shown in Fig. 12.24(b). As in the I2C case, the data line DQ is pulled high with a pull-up resistor and the Master simulates the logic 1 state by changing its port line from low to input (see Fig. 12.14(b)). In this state the Master can listen to data sent by the Slave as shown in Fig. 12.24(c).
For our example we are required to write two subroutines that will respectively write a byte to a 1-Wire Slave and read a byte from the Slave.
Solution
From Fig. 12.24(b) we see that writing a bit to a Slave involves the following tasks:
1.The Master starts the process by forcing the data line low for at least 1 µs.
2.The Master either keeps the line low (Write 0) or releases the line (Write 1) for 60 – 120 µs.
3.The Slave reads the line state between 15 – 45 µs later.
4.The Master releases the line (if Write 0) for at least 1 µs to relax the system.
The subroutines of Program 12.16 assume that the port line driving DQ has been set up as described on page 325 for the I2C bus to give the two states as hard low and open circuit – pulled up high. Also we assume that we have the delay macro Delay_us in situ which gives a Kµs delay, where K is the parameter passed to the macro.
Delay_us macro |
K |
; K is the |
number of usec delay |
local |
DELAY_US_LOOP |
||
movlw |
(K*XTAL)/(4*3)+1 ; |
1˜ |
|
DELAY_US_LOOP |
|||
addlw |
-1 |
; Decrement count: N˜ |
|
btfss |
STATUS,Z ; to zero |
: N + 1˜ |
|
goto |
DELAY_US_LOOP ; |
: 2(N-1)˜ |
|
endm |
|||
Both subroutines begin by driving DQ low for a minimum of 1 µS, defining the Start condition. Writing a single bit to DQ occurs in a slot which has a duration of 60 – 120 µs, and commences with DQ either low or released to be pulled high, defining a Write-0 or Write-1 condition. The Slave samples the state of the data line sometime after 15 µs into the slot. Although the duration of the slot is not critical, care needs to be taken as a low duration of between 480 and 960 µs is interpreted by the Slave as a Reset command (see SAQ 12.3).
358 The Quintessential PIC Microcontroller
Program 12.16 Reading and writing on a 1-Wire system.
; *************************************************************
; * |
FUNCTION: |
Writes a |
byte datum to a 1-Wire slave |
* |
||||
; * |
RESOURCE: |
macro |
Delay_us giving N microsecond delay |
* |
||||
; |
* |
ENTRY |
: |
Datum |
is |
in |
DATA_OUT |
* |
; |
* |
EXIT |
: |
DATA_OUT |
is |
zero, W, STATUS altered |
* |
|
; *************************************************************
WRITE_1W movlw |
8 |
; Loop count |
movwf |
COUNT |
|
W_LOOP bcf |
INDF,DAT |
; Low edge signals Start |
Delay_us |
1 |
; for 1us |
rrf |
DATA_OUT,f |
; LSB first shift into Carry |
btfsc |
STATUS,C |
; Was it a 1? |
bsf |
INDF,DAT |
; IF it was THEN output high |
Delay_us |
d’60’ |
; Hold for 60us |
bsf |
INDF,DAT |
; Release line to go high |
Delay_us |
1 |
; Relax for 1us |
decfsz |
COUNT,f |
; Repeat eight times |
goto |
W_LOOP |
|
return |
; *************************************************************
; * |
FUNCTION: |
Reads a byte datum from a 1-Wire slave |
* |
||
; * |
RESOURCE: |
macro Delay_us giving N microsecond delay |
* |
||
; |
* |
ENTRY |
: |
None |
* |
; |
* |
EXIT |
: |
Datum is in DATA_IN, W, STATUS altered |
* |
; *************************************************************
READ_1W |
movlw |
8 |
; Loop count |
movwf |
COUNT |
||
R_LOOP |
bcf |
INDF,DAT |
; Low edge signals Start |
Delay_us |
1 |
; for 1us |
|
bsf |
INDF,DAT |
; Release line |
|
Delay_us |
8 |
; Wait 8us for Slave to O/P data |
|
bcf |
STATUS,C |
; Clear Carry |
|
btfsc |
INDF,DAT |
; Check input state |
|
bsf |
STATUS,C |
; IF high THEN set Carry |
|
rrf |
DATA_IN,f |
; Shift bit in -> LSB |
|
Delay_us |
d’48’ |
; Wait to end of slot |
|
decfsz |
COUNT,f |
; Repeat eight times |
|
goto |
R_LOOP |
||
return |
Eight Write slots are used with a 1 µs relax period interval to transmit the byte, each slot’s state following the bit rotated into the Carry flag of the datum byte DATA_OUT. After eight shift/output cycles the process terminates.
Reading from a Slave involves the following tasks:
1.The Master starts the process by forcing the data line low for at least 1 µs.
2.The Master then listens to data placed on the line by the Slave which is valid for up to 15 µs after the Start edge.
3.The Slave releases the line after 15 µs which should be pulled high by the end of the 60 µs slot.
12. One Bit at a Time 359
4. The Master waits for a minimum of 1 µs before commencing the next slot.
The input subroutine READ_1W follows this task list, sampling the data line sometime before 15 µs into the slot, at which time the Slave’s data should have settled to the appropriate voltage level. Each bit is used to set the Carry flag which is then shifted into DATA_IN. After eight sample/shift loops, DATA_IN has the received byte datum.
Unlike the I2C bus, the 1-Wire architecture is designed for a single Master. However, 1-Wire Slaves have device addresses comprising a 64bit unique code as part of an internal ROM. The first eight bits are a 1-Wire family code – the DS1820 code is 10h. The following 48 bits are a unique serial number and the last eight bits are an error checking byte.
Self-assessment questions
12.1Rewrite Program 11.3 on page 288 but based on the SPI hardware of Fig. 12.5. Hint: Rather than shifting in whole bytes it may be more e cient to simply shift in and test on a bit-by-bit basis.
12.2Show how you could connect four MAX518 ADCs (see Fig 12.16) on the one I2C circuit and how channel 1 on the third ADC could be written to.
12.3Communications along a 1-Wire link begins with a Reset operation where the Master pulls the line low for 480 – 960 µs after which the line is released. The Slave then responds by dragging the line low
after no more than 60 µs delay. This low persists for a further 60
– 240 µs after which the Slave releases this line. Design a subroutine that will do this procedure when called. Assume the resources of Program 12.16 are available to you.
12.4 Parity is a technique whereby the number of digits in a word is always either even or odd. This is accomplished by adding an extra bit which is calculated by the transmission software to be 0 or 1 to meet this overall criterion. For instance, for odd parity of an 8-bit word 01101111 we have 1 01101111. The receiver will check that all nine received bits have an odd count. If one bit (or any odd number) has been corrupted by noise, then a parity error is said to have occurred.
Based on the PIC USART, write software to set the asynchronous protocol to 9 bit word and calculate the odd one’s parity bit of DATA_OUT which should be placed in TX9D of the TXSTA register prior to the loading of the data into TXREG and transmission.