avr: fixed code for DS18B20 (works now!).

This commit is contained in:
Martin Preuss
2024-09-12 11:42:59 +02:00
parent 39933a957b
commit 0fabc6d613

View File

@@ -20,6 +20,7 @@
.equ DS18B20_FLAGS_CONV_STARTED = 0x01
.equ DS18B20_FLAGS_CONV_UPDATED = 0x02
.equ DS18B20_POLYNOMIAL = 0x8c
; ***************************************************************************
@@ -96,13 +97,13 @@ Ds18b20_OnTimer:
; conversion complete, read value, set flags
ldi xl, LOW(ds18b20DataBuffer)
ldi xh, HIGH(ds18b20DataBuffer)
in r15, SREG
push r15
in r15, SREG
cli
rcall ds18b20ReadScratchPad
brcc Ds18b20_OnTimer_popr15ret
out SREG, r15
pop r15
out SREG, r15
ld r16, X+ ; copy temp from scratchpad
sts ds18b20DataTemp, r16
@@ -116,8 +117,8 @@ Ds18b20_OnTimer:
ret
Ds18b20_OnTimer_startConversion:
in r15, SREG
push r15
in r15, SREG
cli
ldi r16, DS18B20_FNCMD_CONVERT
rcall Ds18b20_SendCommand
@@ -126,8 +127,8 @@ Ds18b20_OnTimer_startConversion:
ori r16, DS18B20_FLAGS_CONV_STARTED
sts ds18b20Flags, r16
Ds18b20_OnTimer_popr15ret:
out SREG, r15
pop r15
out SREG, r15
Ds18b20_OnTimer_done:
ret
@@ -190,11 +191,10 @@ Ds18b20_ReadRom_loop1:
dec r23
brne Ds18b20_ReadRom_loop1
sbiw xh:xl, 8
ldi r18, 7
ldi r19, 0x99
rcall crc8Calc ; (R16, R17, R18, R20, X)
ld r17, X+
cp r16, r17
ldi r18, 8
ldi r19, DS18B20_POLYNOMIAL
rcall ds18b20Crc8Calc ; (R16, R17, R18, R20, R21, X)
tst r16
brne Ds18b20_ReadRom_error
sec
ret
@@ -229,12 +229,11 @@ ds18b20ReadScratchPad_loop1:
dec r23
brne ds18b20ReadScratchPad_loop1
sbiw xh:xl, 9
ldi r18, 8
ldi r19, 0x99
rcall crc8Calc ; (R16, R17, R18, R20, X)
ld r17, X+
cp r16, r17
; brne ds18b20ReadScratchPad_error
ldi r18, 9
ldi r19, DS18B20_POLYNOMIAL
rcall ds18b20Crc8Calc ; (R16, R17, R18, R20, R21, X)
tst r16
brne ds18b20ReadScratchPad_error
sbiw xh:xl, 9
sec
ret
@@ -260,6 +259,41 @@ Ds18b20_SendTemp:
; ---------------------------------------------------------------------------
; @routine ds18b20Crc8Calc @global
; calc crc8 checksum using given polynomial
;
; @return r16 crc8 checksum
; @return X points directly after last checked byte
; @param X pointer to data to calc crc8 for
; @param r18 number of bytes to calc crc8 for
; @param r19 polynomial to use
; @clobbers: R16, R17, R18, R20, R21, X
ds18b20Crc8Calc:
clr r16 ; start crc
ds18b20Crc8Calc_loop1:
ld r17, X+ ; running var
ldi r20, 8 ; counter for loop2
ds18b20Crc8Calc_loop2: ; r16=crc so far, r17=current inbyte
mov r21, r16
lsr r16
eor r21, r17
lsr r17
andi r21, 1
breq ds18b20Crc8Calc_withoutPoly
eor r16, r19
ds18b20Crc8Calc_withoutPoly:
dec r20
brne ds18b20Crc8Calc_loop2
dec r18
brne ds18b20Crc8Calc_loop1
ret
; @end
DS18B20_END:
.equ MODULE_SIZE_DS18B20 = DS18B20_END-DS18B20_BEGIN