Hi,
I'm trying to check the value of a bit in a byte register.
To start, this is the readRegister function for the i2c
ignore the SPI_MODE part
// ReadRegister
//
// Parameters:
// *outputPointer -- Pass &variable (address of) to save read data to
// offset -- register to read
//
//****************************************************************************//
status_t LSM6DS3Core::readRegister(uint8_t* outputPointer, uint8_t offset) {
//Return value
uint8_t result = 0;
uint8_t numBytes = 1;
status_t returnError = IMU_SUCCESS;
switch (commInterface) {
case I2C_MODE:
Wire.beginTransmission(I2CAddress);
Wire.write(offset);
if( Wire.endTransmission() != 0 )
{
returnError = IMU_HW_ERROR;
}
Wire.requestFrom(I2CAddress, numBytes);
while ( Wire.available() ) // slave may send less than requested
{
result = Wire.read(); // receive a byte as a proper uint8_t
}
break;
case SPI_MODE:
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset | 0x80); //Ored with "read request" bit
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
if( result == 0xFF )
{
//we've received all ones, report
returnError = IMU_ALL_ONES_WARNING;
}
break;
default:
break;
}
*outputPointer = result;
return returnError;
}
Here is my register, the FUNC_SRC in highlighted in yellow
readRegister reads a byte and the FUNC_SRC is a byte, unless i'm wrong ?? am i ??
Does readRegister reads the whole byte and packs them up into the output variable ?
How do i get the value of the 5'th bit of FUNC_SRC, from the output variable ??
the 5'th bit is TILT_IA.
It would really help a lot.
kind regards,
gitduino
If you are going to read just one byte, then why use a while-loop ?
I know you took it from the Sparkfun source code, but that does not mean that you have to write the same cheesy code.
I think that this makes more sense:
Wire.requestFrom(I2CAddress, 1);
if (Wire.available() == 1)
{
result = Wire.read();
}
This command is wrong and can be removed: "// slave may send less than requested".
You can turn a parameter that is a pointer into "by reference". Then the source code is easier to read.
So if i'm supposed to use bitRead(x, n);
then i replace the x with output then the n is 5 right ?
example:
int something = 0;
something = bitRead(output, 5);
the return output definitely does contain some values
but when i print output
say,
Serial.print(output);
I get 0 as a result
I know uint8_t returns a byte
after return output, should i convert the content of output into another type
before bitRead(x, n); ??
or perhaps using uint8_t isn't the right approach to extract byte from the register
The bit number starts counting from 0.
Bit 0 is always shown on the right.
You don't need to test this with I2C. You can make a small sketch with a value and print that value hexadecimal, binary and with bitRead().
If your variable 'something' is zero, then that bit was really zero.
Please show a full sketch. There is a website for that: https://snippets-r-us.com/