The following code controls a Microchip Technology Inc.'s MCP9804 digital temperature sensor.
The following was developed on a Zipit Z2 running openWRT. The distribution includes the nano text editor and picoc c language compiler. Some of the following code was blatantly cut and paste from other peoples web sites and from many many internet searches.
The first half of the code executes a user command and pipes the output of the command into an array of chars. The user command that gets executed is:
i2cget -y 0 0xlf 0x05 w
- i2cget is a program which reads data from an i2c device
- -y option disables user intervention
- 0 designates which i2c bus to use for communication
- 0xlf is the chip address of the temperature sensor
- 0x05 is the memory address location of the temperature data
- w specifies that a word length of data is to be retrieved.
The second half of the code rearranges the order of the chars since they are received from the i2c sensor in a funny order. After rearrangement, the array of chars is converted to an integer. The most significant three bits are masked off. The remaining integer can take a value from 0 to 8192 representing temperatures from -40 °C to 125 °C.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.