'Demonstration of reading and writing the Dallas DS1991 'secure I-button. Using the 1-wire statements. '-------------------------------------------------------------- ' ' You must use Bascom-8051 1.09 or higher !! ' ' ' pull-up of 4K7 required to VCC from P3.2 ' DS1991 serial button connected to P3.2 '-------------------------------------------------------------- ' E.K.Dekker 1999 Lampje@Xs4all.nl '-------------------------------------------------------------- Config 1wire = P3.2 'use this pin Dim Ar(16) As Byte , I As Byte , Temp As Byte Dim Text As String * 16 , Temp2 As String * 1 Main: 'Reading first the ROM-number 1wreset 'reset the device 1wwrite &H33 'read ROM command Ar(1) = 1wread(8) 'read 8 bytes Print "ROM number : "; For I = 1 To 8 Printhex Ar(i); 'print ROM-number Next Wait 1 'wait a moment 'Writing scratchpad 'It is possible to write 64 bytes of data to the scratchpad. In this example 'i only used 16 bytes.You can write/read the other bytes if you change the 'array or start address. Text = "Scratchpad data." Ar(1) = &H96 'Write scratch command Ar(2) = &HC0 'Start address + C0H Ar(3) = &HFF - Ar(2) 'Complement of Ar(2) 1wreset 'reset the device 1wwrite &HCC 'Skip read ROM command 1wwrite Ar(1) , 3 'Write command to DS1991 Gosub Fill_write_array 'Fill array with "Text" 1wwrite Ar(1) , 16 'Write 16 bytes to DS1991 Wait 1 'wait a moment 'Reading scratchpad back Ar(1) = &H69 'Read scratch command Ar(2) = &HC0 'Start adress + COH Ar(3) = &HFF - Ar(2) 'Complement of Ar(2) 1wreset 'reset the device 1wwrite &HCC 'Skip read ROM command 1wwrite Ar(1) , 3 'Write command to DS1991 Ar(1) = 1wread(16) 'Read 16 bytes from DS1991 Print "Scratch pad : "; ' For I = 1 To 16 'Prints the contents of the Temp = Ar(i) ' scratchpad Print Chr(temp); ' Next I ' Wait 1 'wait a moment 'Writing subkeyid and password 'Writing new subkeyid and password will destroy all data in subkey. 'The DS1991 has 3 Subkeys, 1=00H , 2=40H , 3=80H 'The variable "text" must be exactly 16 bytes, the first 8 for Subkey-id and the 'last 8 for password. Every subkey has it's own password. Text = "Subkyid1PASSWORD" 'New subkey-id and password Ar(1) = &H5A 'Change password command Ar(2) = &H00 'Subkey-id to change Ar(3) = &HFF - Ar(2) 'Complement of Ar(2) 1wreset 'reset the device 1wwrite &HCC 'Skip read ROM command 1wwrite Ar(1) , 3 'Write command to DS1991 Ar(1) = 1wread(8) 'Read old-id 1wwrite Ar(1) , 8 'Write old-id back to DS1991 Gosub Fill_write_array 'Fill array with "Text" 1wwrite Ar(1) , 16 'Write New-id and password Wait 1 'wait a moment 'Writing subkey 'It is possible to write 64 bytes of data to the subkey. In this example 'i only used 16 bytes.You can write/read the other bytes if you change the 'array or start address. Text = "Hello world!!!!!" 'New subkey Ar(1) = &H99 'Writing subkey command Ar(2) = &H10 'Subkey(00H) + Start address (10H) Ar(3) = &HFF - Ar(2) 'Complement of Ar(2) 1wreset 'reset the device 1wwrite &HCC 'Skip read ROM command 1wwrite Ar(1) , 3 'Write subkey write command Text = "Subkyid1" 'Must be the same as the first 8 Gosub Fill_write_array ' bytes of "writing subkeyid & passw." 1wwrite Ar(1) , 8 'Write Subkeyid for conformation Text = "PASSWORD" 'Must be the same as the last 8 Gosub Fill_write_array ' bytes of "writing subkeyid & passw." 1wwrite Ar(1) , 8 'Write password to DS1991 Text = "Hello world !!!!" 'New subkey data Gosub Fill_write_array 1wwrite Ar(1) , 16 'Write 16 bytes to DS1991 Wait 1 'Reading subkey Ar(1) = &H66 'Read secured subkey command Ar(2) = &H10 'Subkey(00H) + Start adress (10H) Ar(3) = &HFF - Ar(2) 'Complement of Ar(2) 1wreset 'reset the device 1wwrite &HCC 'Skip read ROM command 1wwrite Ar(1) , 3 'Write subkey read command Ar(1) = 1wread(8) 'DS1991 respons with the Subkey-id Text = "PASSWORD" 'Must be the same as the last 8 Gosub Fill_write_array ' bytes of "writing subkeyid & passw." 1wwrite Ar(1) , 8 'Write password to DS1991 Ar(1) = 1wread(16) 'Reads 16 bytes from DS1991 Print "Sub key : "; ' For I = 1 To 16 ' Temp = Ar(i) 'Prints the contents of the Print Chr(temp); ' subkey. Next I ' Wait 1 'wait a moment Goto Main 'And do it again Fill_write_array: For I = 1 To 16 Temp2 = Mid(text , I , 1) Ar(i) = Asc(temp2) Next I Return