This is the code I made to write on register 0x1A and to read on register 0x00 to see what the chip_id is. The chip id should be 0x50.When I read the register, I'm getting either 255 or 0 as the output, indicating a mistake in my code. Does someone know what I did wrong? #include <SPI.h> const int ssPin = 6; // Set the SS (Slave Select) pin #define BMP3_REG_IF_CONF UINT8_C(0x1A) #define BMP3_REG_CHIP_ID UINT8_C(0x00) uint32_t clockFrequency = 100000; void get_reg(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, uint8_t dummy_byte); void read(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin); void write(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin); void set_reg(uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len); void setup() { Serial.println("SPI SETUP BEGIN"); pinMode(ssPin, OUTPUT); // Set the SS pin as an output SPI.begin(); // Initialize the SPI library Serial.begin(9600); // Initialize serial communication for debugging Serial.println("SPI SETUP DONE"); } void loop() { Serial.println("Start writing"); int8_t rslt; uint8_t reg_addr1 = BMP3_REG_IF_CONF; /* bit i want to change*/ uint8_t op_mode_reg_val = 1; /* Write the power mode in the register */ set_reg(®_addr1, &op_mode_reg_val, 1); Serial.println("end writing"); Serial.println("wait 5 sec"); delay(5000); Serial.println("start reading"); uint8_t reg_addr2; uint8_t reg = 0; uint8_t dummy_byte = 1; uint32_t len = 1; reg_addr2 = BMP3_REG_CHIP_ID; get_reg(reg_addr2, ®, 1, dummy_byte); // Print the value of reg Serial.print("Register value: "); Serial.println(reg); Serial.println("end reading"); delay(5000); } void get_reg(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, uint8_t dummy_byte){ uint32_t idx; uint32_t temp_len = len + dummy_byte; uint8_t temp_buff[len + dummy_byte]; reg_addr = reg_addr | 0x80; read(reg_addr, temp_buff, temp_len, ssPin); for (idx = 0; idx < len; idx++) { reg_data[idx] = temp_buff[idx + dummy_byte]; } } void read(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin){ SPI.beginTransaction(SPISettings(clockFrequency, MSBFIRST, SPI_MODE0)); digitalWrite(pin, LOW); SPI.transfer(regAddress | 0x80); // Read all requested bytes for(uint32_t i = 0; i < numBytes; i++) { dataBuffer[i] = SPI.transfer(0); } // End transmission digitalWrite(pin, HIGH); SPI.endTransaction(); } void set_reg(uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len) { int8_t rslt; uint8_t temp_buff[len * 2]; uint32_t temp_len; uint8_t reg_addr_cnt; temp_buff[0] = reg_data[0]; for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++) { reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F; } temp_len = len; write(reg_addr[0], temp_buff, temp_len, ssPin); } void write(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin){ SPI.beginTransaction(SPISettings(clockFrequency, MSBFIRST, SPI_MODE0)); digitalWrite(pin, LOW); SPI.transfer(regAddress); /* Write all the data */ for(uint32_t i = 0; i < numBytes; i++) { SPI.transfer(dataBuffer[i]); } // End transmission digitalWrite(pin, HIGH); SPI.endTransaction(); }
... View more