Skip to content
Snippets Groups Projects
Commit 5ccc38e7 authored by Gijs Schoonderbeek's avatar Gijs Schoonderbeek
Browse files

Added multi I2C support to ADC scripts

Added the option to select a different I2C bus on the Rapsberry Pi. This had to be set... see confluence page.
parent 783d777a
No related branches found
No related tags found
1 merge request!2Modified the scripts to run on Raspberry Pi.
...@@ -41,8 +41,7 @@ def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ): ...@@ -41,8 +41,7 @@ def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ):
# #
# Read Byte from the ADC # Read Byte from the ADC
# #
I2C_device = I2C(ADDRESS) I2C_device = I2C(ADDRESS, , BUSNR=I2CBUSNR)
I2C_device.bus = I2CBUSNR
ADC_rw = 0x01 # 0 for write, 1 for read ADC_rw = 0x01 # 0 for write, 1 for read
stri = "Read ADC from Address {:8x}".format(ADC_reg_address) stri = "Read ADC from Address {:8x}".format(ADC_reg_address)
......
...@@ -23,6 +23,7 @@ if os.name =="posix": ...@@ -23,6 +23,7 @@ if os.name =="posix":
else: else:
from I2C_serial import * from I2C_serial import *
I2CBUSNR=1
sleep_time=0.05 sleep_time=0.05
CHECK_I2C_DEVICES = False CHECK_I2C_DEVICES = False
WRITE_DATA = True WRITE_DATA = True
...@@ -40,7 +41,7 @@ ADC_ORDER = [0, 1, 2] ...@@ -40,7 +41,7 @@ ADC_ORDER = [0, 1, 2]
def blink_led(address=0x76, times = 5): def blink_led(address=0x76, times = 5):
print("Blink LED") print("Blink LED")
I2C_device = I2C(address) I2C_device = I2C(address, BUSNR=I2CBUSNR)
I2C_device.write_bytes(0x06, 00) I2C_device.write_bytes(0x06, 00)
I2C_device.write_bytes(0x07, 00) I2C_device.write_bytes(0x07, 00)
for cnt in range(times): for cnt in range(times):
...@@ -55,7 +56,7 @@ def Write_byte_ADC(ADC_reg_address, ADC_data, ADC_bytes=1, ADC_NR = 0, ADDRESS=0 ...@@ -55,7 +56,7 @@ def Write_byte_ADC(ADC_reg_address, ADC_data, ADC_bytes=1, ADC_NR = 0, ADDRESS=0
# #
# Write Byte to the ADC # Write Byte to the ADC
# #
I2C_device = I2C(ADDRESS) I2C_device = I2C(ADDRESS, BUSNR=I2CBUSNR)
ADC_rw = 0x00 # 0 for write, 1 for read ADC_rw = 0x00 # 0 for write, 1 for read
stri = "Write : {0:2x} to Address : {1:2x}".format(ADC_data, ADC_reg_address) stri = "Write : {0:2x} to Address : {1:2x}".format(ADC_data, ADC_reg_address)
print(stri) print(stri)
...@@ -86,7 +87,7 @@ def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ): ...@@ -86,7 +87,7 @@ def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ):
# #
# Read Byte from the ADC # Read Byte from the ADC
# #
I2C_device = I2C(ADDRESS) I2C_device = I2C(ADDRESS, BUSNR=I2CBUSNR)
ADC_rw = 0x01 # 0 for write, 1 for read ADC_rw = 0x01 # 0 for write, 1 for read
stri = "Read ADC from Address {:8x}".format(ADC_reg_address) stri = "Read ADC from Address {:8x}".format(ADC_reg_address)
...@@ -132,7 +133,7 @@ def rcu_sensors(): ...@@ -132,7 +133,7 @@ def rcu_sensors():
addr = 0x14 addr = 0x14
Vref = 3.3 Vref = 3.3
one_step = Vref/(2**(16+1)) one_step = Vref/(2**(16+1))
I2C_device = I2C(addr) I2C_device = I2C(addr, BUSNR=I2CBUSNR)
I2C_device.write_bytes(0xB8, 0xB0) I2C_device.write_bytes(0xB8, 0xB0)
sleep(1) sleep(1)
ret_ack, ret_value = I2C_device.read_last_reg(3) ret_ack, ret_value = I2C_device.read_last_reg(3)
...@@ -165,7 +166,7 @@ def rcu_sensors(): ...@@ -165,7 +166,7 @@ def rcu_sensors():
def rw_eeprom(value=0xAB): def rw_eeprom(value=0xAB):
ADDR = 0x50 ADDR = 0x50
I2C_eeprom = I2C(0x53) I2C_eeprom = I2C(0x53, BUSNR=I2CBUSNR)
I2C_eeprom.write_bytes(0x00, value) I2C_eeprom.write_bytes(0x00, value)
I2C_eeprom.write_pointer(0x00) I2C_eeprom.write_pointer(0x00)
ret_ack, ret_value = I2C_eeprom.read_last_reg(1) ret_ack, ret_value = I2C_eeprom.read_last_reg(1)
...@@ -177,7 +178,7 @@ def rw_eeprom(value=0xAB): ...@@ -177,7 +178,7 @@ def rw_eeprom(value=0xAB):
def power(state): def power(state):
ADDRESS_IO = 0x76 ADDRESS_IO = 0x76
I2C_IO_device = I2C(ADDRESS_IO) I2C_IO_device = I2C(ADDRESS_IO, BUSNR=I2CBUSNR)
I2C_IO_device.write_bytes(0x06, 00) I2C_IO_device.write_bytes(0x06, 00)
if state=='ON': if state=='ON':
print("Switch ON") print("Switch ON")
...@@ -212,7 +213,7 @@ def set_gain(gain, channel): ...@@ -212,7 +213,7 @@ def set_gain(gain, channel):
else: else:
set_gain = max_gain-min_gain set_gain = max_gain-min_gain
bits_to_set = set_gain bits_to_set = set_gain
I2C_IO_device = I2C(ADDRESS_IO) I2C_IO_device = I2C(ADDRESS_IO, BUSNR=I2CBUSNR)
I2C_IO_device.write_bytes(0x06, 00) I2C_IO_device.write_bytes(0x06, 00)
I2C_IO_device.write_bytes(0x07, 00) I2C_IO_device.write_bytes(0x07, 00)
if channel == 0: if channel == 0:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment