Instrument control package: Difference between revisions
Jump to navigation
Jump to search
→i2c
m (Remove redundant Category:Packages.) |
(→i2c) |
||
Line 360: | Line 360: | ||
== i2c == | == i2c == | ||
i2c | Example with a Si7021 i2c breakout board (https://www.sparkfun.com/products/1376) connected to a i2c master interface. | ||
To work out the devices available (assuming i2ctools are installed) run the i2cdetect command from a terminal window. | |||
$ i2cdetect -l | |||
i2c-1 i2c i2c-ch341-usb at bus 002 device 008 I2C adapter | |||
i2c-0 unknown SMBus I801 adapter at 4000 N/ | |||
In the example case the temperature sensor is connected to the i2c-ch341-usb device, as as sufficient permissions to access the device as a user. | |||
Accoridng to the datasheet, the temperature device uses address 0x40, so create a i2c device using the linux device and address: | |||
i2cdev = i2c("/dev/i2c-1", 0x40) | |||
To read the termaturem register 0xF3 must be addressed and read (2 bytes of data): | |||
TEMP_MEASURE_NOHOLD = hex2dec("F3"); | |||
fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | |||
pause (0.02); | |||
data = fread (i2cdev, 3); | |||
The data needs to be converted to a 16 bit value and the unused part masked out. | |||
value = uint16(data(1))*256 + uint16(data(2)); | |||
value = bitand (value, hex2dec("FFFC")); | |||
temp_Code = double(value); | |||
Now convert the temperature to degrees C and display: | |||
C = (175.72*temp_Code/65536)-46.85; | |||
printf ("temperature read %f C\n", C); | |||
== TCP == | == TCP == |