Editing Instrument control package
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 377: | Line 377: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
In the example case the temperature sensor is connected to the i2c-ch341-usb device, | 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: | |||
<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | ||
i2cdev = i2c("/dev/i2c-1", 0x40) | i2cdev = i2c("/dev/i2c-1", 0x40) | ||
}} | |||
To read the | To read the termaturem register 0xF3 must be addressed and read (2 bytes of data): | ||
<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | ||
TEMP_MEASURE_NOHOLD = hex2dec("F3"); | TEMP_MEASURE_NOHOLD = hex2dec("F3"); | ||
fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | ||
pause (0.02); | pause (0.02); | ||
data = fread (i2cdev, 3); | data = fread (i2cdev, 3); | ||
}} | |||
The data | The data needs to be converted to a 16 bit value and the unused part masked out. | ||
<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | ||
value = uint16(data(1))*256 + uint16(data(2)); | value = uint16(data(1))*256 + uint16(data(2)); | ||
value = bitand (value, hex2dec("FFFC")); | value = bitand (value, hex2dec("FFFC")); | ||
temp_Code = double(value); | temp_Code = double(value); | ||
}} | |||
Now convert the temperature to degrees C and display: | Now convert the temperature to degrees C and display: | ||
<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | ||
C = (175.72*temp_Code/65536)-46.85; | C = (175.72*temp_Code/65536)-46.85; | ||
printf ("temperature read %f C\n", C); | printf ("temperature read %f C\n", C); | ||
}} | |||
== TCP == | == TCP == |