Hi, i have an imu sensor. It communicates over SPI. I use Mega2560 ,
I want to read one simple register WHO_AM_I register.
/*
on documentation
SPI Operational Features
- Data is delivered MSB first and LSB last
- Data is latched on the rising edge of SCLK
- Data should be transitioned on the falling edge of SCLK
- The maximum frequency of SCLK is 1MHz
- SPI read and write operations are completed in 16 or more clock cycles (two or more bytes). The
first byte contains the SPI Address, and the following byte(s) contain(s) the SPI data. The first
bit of the first byte contains the Read/Write bit and indicates the Read (1) or Write (0) operation.
The following 7 bits contain the Register Address. In cases of multiple-byte Read/Writes, data is
two or more bytes:
SPI Address format
MSB LSB
R/W A6 A5 A4 A3 A2 A1 A0
SPI Data format
MSB LSB
D7 D6 D5 D4 D3 D2 D1 D0
*/
On Registermap WHO_AM_I
Hex Add / Dec Add / Register Name / Serial I/F / bit7 / bit 6 ..... bit 0
75 / 117 / WHO_AM_I / R / WHOAMI[7:0]
Pin Assigments
51 > SDI
52 > SCLK
50 > SDO/ADO
53 > CS
#include <SPI.h> //add arduino SPI library;
//list of all registers binary addresses;
byte WHO_AM_I = 0B01110101;
byte Read = 0B10000000;
byte Write = 0B00000000;
//chip select pin on arduino;
const int CS = 53;
/*
SPI.h sets these for us in arduino
const int SDI = 11;
const int SDO = 12;
const int SCL = 13;
*/
void setup() {
Serial.begin(57600);
//start the SPI library;
SPI.begin();
//initalize the chip select pins;
pinMode(CS, OUTPUT);
}
void loop() {
byte rValue = ReadRegister(WHO_AM_I);
Serial.print(Read | WHO_AM_I,BIN);
Serial.print(" > Value: ");
Serial.println(rValue);
delay(1000);
}
byte ReadRegister(byte Address){
byte result = 0;
digitalWrite(CS, LOW);
SPI.transfer(Read | Address);
result = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
return(result);
}
void WriteRegister(byte Address, byte Value){
digitalWrite(CS, LOW);
SPI.transfer(Write | Address);
SPI.transfer(Value);
digitalWrite(CS, HIGH);
}
Could anyone help ?