![]() The second phase is the data reading process and you can request as many bytes as you want with the second parameter in Wire.requestFrom. The first phase tells the I☬ slave device which memory register that we want to read but we have complete the read operation in two separate steps because the wire library buffers everything behind the scenes and does not actually send anything until it gets the Wire.endTransmission() command. requestFrom( deviceAddress, 2) // send me the data from 2 registersįirstRegisterByte = Wire. endTransmission() // completes the ‘move memory pointer’ transaction write( registerAddress) // move your memory pointer to registerAddress beginTransmission( deviceAddress) // get the sensors attention Reading data from a sensors memory register(s) requires two phases: Unfortunately this means you’ll find lots of poorly commented code examples out there where people simply write zero into a control register without explaining which of the eight different functions they were aiming for because seven of those were still at their default zero-values anyway. The startup default values for a given control register are often a string of zeros because all the chip functions being controlled by that register are turned off. You can think of this pointer as a read/write head and once that pointer is aiming at a specific register, the next byte you send along the wires will over-write the data that was previously stored there. The registerAddress moves a pointer inside the chip to the memory location you specified. Since the bus address of a given sensor IC can vary from one module to the next I keep Rob Tillaart’s bus scanner handy to find them, and more importantly to discover when two sensors are fighting with each other by trying to use the same address on the bus. The I☬ deviceAddress is set by the manufacturer but some can be modified from their defaults by connecting solder pads on the breakout board. write( dataByte) // new data to put into that memory register write( registerAddress) // command byte to target the register location beginTransmission( deviceAddress) // Attention sensor deviceAddress! Writing a byte to a sensor’s control register can be done with four basic steps: ![]() 0b00001111) so you can see the on/off states when you read through your code. Most people use byte variables for the sensor’s bus and register memory addresses, but once you’ve figured out the pattern you need to set up in control register switch-bits, it helps to write that information as a long form binary number (eg. This can potentially change eight of those control switches simultaneously and, for parameters that are controlled by more than one bit, sometimes it’s actually required that you set them in one register-writing operation. These can add an extra layer of control settings to take care of when you initialize the sensor.Īrduino’s wire library can only transfer 8-bit bytes over the I☬ bus, so that’s the smallest amount of information you can write into a register memory location at one time. And sometimes there are “special chip functions” that perform some kind of post processing on those sensor readings that would be hard to replicate on the Arduino. I☬ chip-based sensors often have a dozen or more operational settings for things like bit-depth, sampling speed, noise reduction, etc., so you usually need to set bits in several different control registers before you can actually take a reading. Think of control registers as banks of On/Off switches, which you turn on by setting a bit to 1 and turn off by setting that bit to 0. Most sensors change how they operate based on the values stored in control registers. ![]() The functions named with the i2c_ prefix should be generic enough to work with most I☬ sensors, but I’ll also be referring to a few specific cases to show how you might need to modify those basic functions. There are many kinds of registers but for this introduction I am going to group them into three general types: Control, Data and Status registers, and provide brief examples of code that you can use to work with each of them. Even if you’ve never worked with registers before, jrowberg’s visual grid layout makes it easy to see how the sensor’s memory is divided into sections, which are doing different things.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |