AllDigital
Define 1WIRE_REG = PORTD 'define the pin where
Define 1WIRE_BIT = 2 '1wire device is connected
Dim presence As Bit 'precense bit
Dim rom_type As Byte 'Device TYPE 1 Byte
Dim rom_1_id As Byte 'Device ID 6 Byte
Dim rom_2_id As Byte
Dim rom_3_id As Byte
Dim rom_4_id As Byte
Dim rom_5_id As Byte
Dim rom_6_id As Byte
Dim rom_crc As Byte 'Device CRC 1 Byte
Dim id_bit As Bit
Dim cmp_id_bit As Bit
Dim search_temp As Byte
Dim ee_location As Byte
'Dim i As Byte
Dim bit_write As Bit
'Dim finish As Bit 'Variables for temp measure and control
'Dim temp As Byte
'Dim temp2 As Bit
'Dim sign As Byte
Dim last_device_flag As Bit 'Flag to indicate previus search was the last device
Dim id_bit_number As Byte 'The ROM bit number (1-64) currently being searched
Dim last_zero As Byte 'bit position of the last zero written where there was a discrepancy
Dim last_discrepancy As Byte 'bit index that identifies from which bit the ( NEXT ) search discrepancy check shoud start
Dim search_direction As Bit 'bit value indicating the direction of the search. All devices with this bit stay in the search and the rest go into a wait statefor a 1-wire reset
Dim last_family_discrepancy As Byte 'bit index that identifies the last_discrepancy within the first 8-bit family code of ROM number
Dim device_count As Byte 'Number of devices
'Define GLCD_DREG = PORTB '-defines the port where data lines are connected To(it has To be a full 8 - pins port)
'Define GLCD_RSREG = PORTD '- defines the port where RS line is connected To
'Define GLCD_RSBIT = 4 '- defines the pin where RS line is connected To
'Define GLCD_EREG = PORTD '- defines the port where E line is connected To
'Define GLCD_EBIT = 3 '- defines the pin where E line is connected To
'Define GLCD_RWREG = PORTD '- defines the port where R/W line is connected To
'Define GLCD_RWBIT = 5 '- defines the pin where R/W line is connected To
'Define GLCD_CS1REG = PORTD '- defines the port where CS1 line is connected To
'Define GLCD_CS1BIT = 7 '- defines the pin where CS1 line is connected To
'Define GLCD_CS2REG = PORTD '- defines the port where CS2 line is connected To
'Define GLCD_CS2BIT = 6 '- defines the pin where CS2 line is connected To
'WaitUs 100
'GLcdinit
'WaitMs 500
device_count = 0
last_discrepancy = 0
last_family_discrepancy = 0
last_device_flag = 0
last_zero = 0
'************************************************************************
search_rom:
1wireInit presence '1. The bus master begins the initialization sequence
If presence = 1 Then
'GLcdposition 0, 0
'GLcdwrite "No device(s)..."
Goto search_rom
Endif
'************************************************************************
'GLcdclear
If last_device_flag = 1 Then Goto reset_search 'if this bit is TRUE then goto reset_search
id_bit_number = 1 'initiate this byte
last_zero = 0 'initiate this byte, Discrepancy marker
rom_search_procedure:
1wireSendByte 0xf0 '2. The bus master will then issue the Search ROM
'1. for 1 -> 64
read_bit_and_complement:
1wireGetBit id_bit, cmp_id_bit '3. The bus master reads a bit from the 1–Wire bus.
'and it's complement
search_temp.0 = id_bit
search_temp.1 = cmp_id_bit
Select Case search_temp
Case 0 'binary ( 00 ) There are still devices attached which have conflicting bits in this position. Key to fix the problem is here
Gosub conflict
Case 1 'binary ( 01 ) All devices still coupled have a 0–bit in this Bit position.
search_direction = 1
Case 2 'binary ( 10 ) all devices still coupled have A 1–bit in this Bit position.
search_direction = 0
Case 3 'binary ( 11 ) There are no devices attached to the 1–Wire bus. Atypical situation
last_discrepancy = 0
EndSelect
'Set id_bit_number bit in ROM_NO to search_direction and send to 1-wire bus
1wireSendBit search_direction
bit_write = search_direction
Gosub write_rom_bit_value
ASM: incf id_bit_number,f
If id_bit_number <= 64 Then Goto read_bit_and_complement
If id_bit_number > 64 Then last_discrepancy = last_zero
If last_discrepancy = 0 Then last_device_flag = 1
'3.
check_crc:
'crc computation
'if crc computation is diferent then we most solve the problem
'FUTURE feature......
'***************************search finish****************************
'Here is stored all devices in 8 byte blocks
ee_location = device_count * 8
Write ee_location, rom_type
ee_location = ee_location + 1
Write ee_location, rom_1_id
ee_location = ee_location + 1
Write ee_location, rom_2_id
ee_location = ee_location + 1
Write ee_location, rom_3_id
ee_location = ee_location + 1
Write ee_location, rom_4_id
ee_location = ee_location + 1
Write ee_location, rom_5_id
ee_location = ee_location + 1
Write ee_location, rom_6_id
ee_location = ee_location + 1
Write ee_location, rom_crc
ASM: incf device_count,f
If last_device_flag = 0 Then Goto search_rom '1.
'2
reset_search:
Write 256, device_count 'number of devices found, be carefully PIC18F4620 have 1024 bytes of EEPROM
'so, 32 devices are stored correct ( 256 / 8 = 32 )
last_discrepancy = 0
last_family_discrepancy = 0
last_device_flag = 0
End
'******* This part is esential to solve the conflict problem ********
conflict:
If id_bit_number = last_discrepancy Then
search_direction = 1
Return
Endif
If id_bit_number > last_discrepancy Then
search_direction = 0
last_zero = id_bit_number
Return
Endif
'id_bit_number < last_discrepancy
Select Case id_bit_number 'last_discrepancy 'last_zero 'id_bit_number
Case 1
search_direction = rom_type.0
Case 2
search_direction = rom_type.1
Case 3
search_direction = rom_type.2
Case 4
search_direction = rom_type.3
Case 5
search_direction = rom_type.4
Case 6
search_direction = rom_type.5
Case 7
search_direction = rom_type.6
Case 8
search_direction = rom_type.7
'***************************** Device ID*****************************
'*******************************byte2********************************
Case 9
search_direction = rom_1_id.0
Case 10
search_direction = rom_1_id.1
Case 11
search_direction = rom_1_id.2
Case 12
search_direction = rom_1_id.3
Case 13
search_direction = rom_1_id.4
Case 14
search_direction = rom_1_id.5
Case 15
search_direction = rom_1_id.6
Case 16
search_direction = rom_1_id.7
'*******************************byte3********************************
Case 17
search_direction = rom_2_id.0
Case 18
search_direction = rom_2_id.1
Case 19
search_direction = rom_2_id.2
Case 20
search_direction = rom_2_id.3
Case 21
search_direction = rom_2_id.4
Case 22
search_direction = rom_2_id.5
Case 23
search_direction = rom_2_id.6
Case 24
search_direction = rom_2_id.7
'*******************************byte4********************************
Case 25
search_direction = rom_3_id.0
Case 26
search_direction = rom_3_id.1
Case 27
search_direction = rom_3_id.2
Case 28
search_direction = rom_3_id.3
Case 29
search_direction = rom_3_id.4
Case 30
search_direction = rom_3_id.5
Case 31
search_direction = rom_3_id.6
Case 32
search_direction = rom_3_id.7
'*******************************byte5********************************
Case 33
search_direction = rom_4_id.0
Case 34
search_direction = rom_4_id.1
Case 35
search_direction = rom_4_id.2
Case 36
search_direction = rom_4_id.3
Case 37
search_direction = rom_4_id.4
Case 38
search_direction = rom_4_id.5
Case 39
search_direction = rom_4_id.6
Case 40
search_direction = rom_4_id.7
'*******************************byte6********************************
Case 41
search_direction = rom_5_id.0
Case 42
search_direction = rom_5_id.1
Case 43
search_direction = rom_5_id.2
Case 44
search_direction = rom_5_id.3
Case 45
search_direction = rom_5_id.4
Case 46
search_direction = rom_5_id.5
Case 47
search_direction = rom_5_id.6
Case 48
search_direction = rom_5_id.7
'*******************************byte7********************************
Case 49
search_direction = rom_6_id.0
Case 50
search_direction = rom_6_id.1
Case 51
search_direction = rom_6_id.2
Case 52
search_direction = rom_6_id.3
Case 53
search_direction = rom_6_id.4
Case 54
search_direction = rom_6_id.5
Case 55
search_direction = rom_6_id.6
Case 56
search_direction = rom_6_id.7
'***************************** Device CRC****************************
'*******************************byte8********************************
Case 57
search_direction = rom_crc.0
Case 58
search_direction = rom_crc.1
Case 59
search_direction = rom_crc.2
Case 60
search_direction = rom_crc.3
Case 61
search_direction = rom_crc.4
Case 62
search_direction = rom_crc.5
Case 63
search_direction = rom_crc.6
Case 64
search_direction = rom_crc.7
EndSelect
If search_direction = 0 Then last_zero = id_bit_number
Return
write_rom_bit_value:
Select Case id_bit_number
'***************************** Device Type***************************
'*******************************byte1********************************
Case 1
rom_type.0 = bit_write
Case 2
rom_type.1 = bit_write
Case 3
rom_type.2 = bit_write
Case 4
rom_type.3 = bit_write
Case 5
rom_type.4 = bit_write
Case 6
rom_type.5 = bit_write
Case 7
rom_type.6 = bit_write
Case 8
rom_type.7 = bit_write
'***************************** Device ID*****************************
'*******************************byte2********************************
Case 9
rom_1_id.0 = bit_write
Case 10
rom_1_id.1 = bit_write
Case 11
rom_1_id.2 = bit_write
Case 12
rom_1_id.3 = bit_write
Case 13
rom_1_id.4 = bit_write
Case 14
rom_1_id.5 = bit_write
Case 15
rom_1_id.6 = bit_write
Case 16
rom_1_id.7 = bit_write
'*******************************byte3********************************
Case 17
rom_2_id.0 = bit_write
Case 18
rom_2_id.1 = bit_write
Case 19
rom_2_id.2 = bit_write
Case 20
rom_2_id.3 = bit_write
Case 21
rom_2_id.4 = bit_write
Case 22
rom_2_id.5 = bit_write
Case 23
rom_2_id.6 = bit_write
Case 24
rom_2_id.7 = bit_write
'*******************************byte4********************************
Case 25
rom_3_id.0 = bit_write
Case 26
rom_3_id.1 = bit_write
Case 27
rom_3_id.2 = bit_write
Case 28
rom_3_id.3 = bit_write
Case 29
rom_3_id.4 = bit_write
Case 30
rom_3_id.5 = bit_write
Case 31
rom_3_id.6 = bit_write
Case 32
rom_3_id.7 = bit_write
'*******************************byte5********************************
Case 33
rom_4_id.0 = bit_write
Case 34
rom_4_id.1 = bit_write
Case 35
rom_4_id.2 = bit_write
Case 36
rom_4_id.3 = bit_write
Case 37
rom_4_id.4 = bit_write
Case 38
rom_4_id.5 = bit_write
Case 39
rom_4_id.6 = bit_write
Case 40
rom_4_id.7 = bit_write
'*******************************byte6********************************
Case 41
rom_5_id.0 = bit_write
Case 42
rom_5_id.1 = bit_write
Case 43
rom_5_id.2 = bit_write
Case 44
rom_5_id.3 = bit_write
Case 45
rom_5_id.4 = bit_write
Case 46
rom_5_id.5 = bit_write
Case 47
rom_5_id.6 = bit_write
Case 48
rom_5_id.7 = bit_write
'*******************************byte7********************************
Case 49
rom_6_id.0 = bit_write
Case 50
rom_6_id.1 = bit_write
Case 51
rom_6_id.2 = bit_write
Case 52
rom_6_id.3 = bit_write
Case 53
rom_6_id.4 = bit_write
Case 54
rom_6_id.5 = bit_write
Case 55
rom_6_id.6 = bit_write
Case 56
rom_6_id.7 = bit_write
'***************************** Device CRC****************************
'*******************************byte8********************************
Case 57
rom_crc.0 = bit_write
Case 58
rom_crc.1 = bit_write
Case 59
rom_crc.2 = bit_write
Case 60
rom_crc.3 = bit_write
Case 61
rom_crc.4 = bit_write
Case 62
rom_crc.5 = bit_write
Case 63
rom_crc.6 = bit_write
Case 64
rom_crc.7 = bit_write
EndSelect
Return