;------------------------------------------------------------------------------ ; iButton serial number reader ; Addapter pour lire le 18b20 par http://adriy.be ; http://avr-mcu.dxp.pl ; e-mail: radek(at)dxp.pl ; (c) Radoslaw Kwiecien ;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------ ; Defines ;------------------------------------------------------------------------------ #define F_CPU 16000000 ;Table 3. DS18B20 Function Command Set p12 #define ReadRom 0x33 #define SkipRom 0xcc #define ConvertTemp 0x44 ; Initiates temperature conversion. #define WScratch 0x4e ; Writes data into scratchpad bytes 2, 3, and4 (TH, TL, and configuration registers). #define RScratch 0xbe ; Reads the entire scratchpad including theCRC byte. ;------------------------------------------------------------------------------ ; Data segment, variable definitions ;------------------------------------------------------------------------------ .dseg SerialNumber: .byte 8 ;------------------------------------------------------------------------------ ; Code segment ;------------------------------------------------------------------------------ .cseg ;------------------------------------------------------------------------------ ; Include required files ;------------------------------------------------------------------------------ #include "vectors.asm" #include "hd44780.asm" #include "wait.asm" #include "1-wire.asm" #include "crc8.asm" ;------------------------------------------------------------------------------ ; Constants definition ;------------------------------------------------------------------------------ Text1 : .db "iButton Reader",0,0 Text2 : .db "avr-mcu.dxp.pl",0,0 Tp : .db ".",0,0 ;------------------------------------------------------------------------------ ; Program entry point ;------------------------------------------------------------------------------ ProgramEntryPoint: ldi r16, LOW(RAMEND) ; Initialize stack pointer out SPL, r16 ; rcall LCD_Init ; Initialize LCD ldi r16, (HD44780_LINE0 + 1) ; rcall LCD_SetAddressDD ; Set Display Data address to (0,1) ldi ZL, LOW(Text1 << 1) ; Load string address to Z ldi ZH, HIGH(Text1<< 1) ; rcall LCD_WriteString ; Display string ldi r16, (HD44780_LINE1 + 1) ; rcall LCD_SetAddressDD ; Set Display Data address to (1,1); ;ldi ZL, LOW(Text2 << 1) ; ;ldi ZH, HIGH(Text2<< 1) ; Load string address to Z ;rcall LCD_WriteString ; Display string ConfigResolTo10Bits: rcall OWReset brts ConfigResolTo10Bits ldi r16, SkipRom ; Write Skip Rom one wire in "single-drop" rcall OWWriteByte ldi r16, WScratch rcall OWWriteByte clr r16 rcall OWWriteByte ;th,tl rcall OWWriteByte ;th,tl ldi r16, 63 rcall OWWriteByte ;resol MainLoop: ldi r16, (HD44780_LINE1 + 1) ; rcall LCD_SetAddressDD ; Set Display Data address to (1,1); rcall OWReset ; One wire reset brts MainLoop ; If device not present go to MainLoop rcall TempRequest rcall MainReadTemp rcall ConvertTempForLCD ;rcall CRC8Init ; Initialize CRC8 value jmp LoadLoop rcall OWReadByte ; Read first byte (Family ID) cpi r16,0 ; If first byte equal to zero, go to MainLoop breq MainLoop ; (short circuit on one wire bus) rcall CRC8Update ; Update the CRC ldi YL, LOW(SerialNumber) ; ldi YH, HIGH(SerialNumber) ; Load to Y address of SerialNumber table st Y+, r16 ; Store first byte to table, and increment pointer ldi r17, 7 ; 7 bytes remaining StoreLoop: rcall OWReadByte ; read next byte rcall CRC8Update ; update the CRC st Y+, r16 ; store next byte to table, and increment pointer dec r17 ; decrement loop counter brne StoreLoop ; if greater than zero, jump to StoreLoop rcall GetCRC8 ; Read computet CRC8 cpi r16,0 ; copmare with zero brne MainLoop ; if not equal, jump to MainLoop (bad CRC) ; else ldi r16, (HD44780_LINE1 + 0) ; rcall LCD_SetAddressDD ; Set DisplayData address to (0,1) ldi YL, LOW(SerialNumber) ; ldi YH, HIGH(SerialNumber) ; Load to Y address of SerialNumber table ldi r17,2 ; 8 digits to display LoadLoop: push r16 mov r16, XL ; load to r16 byte from table push r16 ldi r19,0xF0 and r16,r19 rcall bin2bcd8 lsr r16 lsr r16 lsr r16 lsr r16 rcall LCD_WriteHexDigit ; display it on LCD in HEX pop r16 rcall bin2bcd8 ldi r18,0x0F and r16,r18 rcall LCD_WriteHexDigit ; display it on LCD in HEX mov r16, r18 ; load to r16 byte from table push r16 ldi r19,0xF0 and r16,r19 rcall bin2bcd8 lsr r16 lsr r16 lsr r16 lsr r16 rcall LCD_WriteHexDigit ; display it on LCD in HEX pop r16 rcall bin2bcd8 ldi r18,0x0F and r16,r18 rcall LCD_WriteHexDigit ; display it on LCD in HEX nop nop nop jmp MainLoop brne LoadLoop ; if not zero, jump to LoadLoop rjmp MainLoop ; jump to MainLoop ;------------------------------------------------------------------------------ ; End of file ;------------------------------------------------------------------------------ ;======= Converting from HEX to BCD ====================================================https://evileg.com/en/post/19/ ;***************************************************** ;* "bin2BCD8" - 8-bit Binary to BCD conversion ;* This subroutine converts an 8-bit number (temp) to a 2-digit ;* i.e 0x15 becomes 0x21 ;* result in temp ;********************************************************** ;.def tBCD = r21 ;add this to main asm file ; bin2bcd8: clr r21 ;clear temp reg bBCD8_1: subi r16,10 ;input = input - 10 brcs bBCD8_2 ;abort if carry set subi r21,-$10 ;tBCD = tBCD + 10 rjmp bBCD8_1 ;loop again bBCD8_2: subi r16,-10 ;compensate extra subtraction add r16,r21 ret