I am trying to do this with arduino uno. My code is given below:
#include <SPI.h>
#define CSN 10 // Chip Select pin for SPI
void setup() {
Serial.begin(115200);
// Set pin modes
pinMode(CSN, OUTPUT);
digitalWrite(CSN, HIGH); // Chip select initially high (not selected)
// Initialize SPI
SPI.begin();
}
void writeAndVerify(uint16_t address, uint16_t value) {
uint16_t currentValue = readRegister(address);
uint16_t maskedValue = (currentValue & ~value) | (value & ~0xFFFF);
uint16_t command = (address & 0x3FFF) | 0x4000; // Write command
uint16_t parity = __builtin_parity(command) << 15;
command |= parity;
uint16_t data = (maskedValue & 0x3FFF);
data |= (__builtin_parity(data) << 15); // Add parity bit to data
Serial.print("Writing command: 0x");
Serial.print(command, HEX);
Serial.print(", data: 0x");
Serial.println(data, HEX);
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1)); // Reduced SPI speed
digitalWrite(CSN, LOW);
delayMicroseconds(100); // Longer delay for stability
SPI.transfer16(command);
SPI.transfer16(data);
digitalWrite(CSN, HIGH);
SPI.endTransaction();
delay(100);
uint16_t readBack = readRegister(address);
Serial.print("Read back value of register 0x");
Serial.print(address, HEX);
Serial.print(": 0x");
Serial.println(readBack, HEX);
if (readBack != maskedValue) {
Serial.print("Error: Failed to write register 0x");
Serial.print(address, HEX);
Serial.print(". Expected: 0x");
Serial.print(maskedValue, HEX);
Serial.print(", Got: 0x");
Serial.println(readBack, HEX);
} else {
Serial.print("Successfully wrote 0x");
Serial.print(maskedValue, HEX);
Serial.print(" to register 0x");
Serial.println(address, HEX);
}
}
void writeRegister(uint16_t address, uint16_t value) {
uint16_t command = (address & 0x3FFF) | 0x4000; // Write command
uint16_t parity = __builtin_parity(command) << 15;
command |= parity;
uint16_t data = (value & 0x3FFF);
data |= (__builtin_parity(data) << 15); // Add parity bit to data
Serial.print("Writing command: 0x");
Serial.print(command, HEX);
Serial.print(", data: 0x");
Serial.println(data, HEX);
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1)); // Reduced SPI speed
digitalWrite(CSN, LOW);
delayMicroseconds(100); // Longer delay for stability
SPI.transfer16(command);
SPI.transfer16(data);
digitalWrite(CSN, HIGH);
SPI.endTransaction();
}
uint16_t readRegister(uint16_t address) {
uint16_t command = (address & 0x3FFF) | 0x8000; // Read command
uint16_t parity = __builtin_parity(command) << 15;
command |= parity;
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1)); // Reduced SPI speed
digitalWrite(CSN, LOW);
delayMicroseconds(50); // Longer delay for stability
SPI.transfer16(command);
uint16_t response = SPI.transfer16(0x0000); // Read data
digitalWrite(CSN, HIGH);
SPI.endTransaction();
return response & 0x3FFF; // Return 14-bit data
}
void writeABIBinAndABIRes() {
// Write ABIBIN = 1 to SETTINGS1 register (bit 5)
writeRegister(0x0018, (1 << 5)); // ABIBIN = 1
// Write ABIRES = 1 to SETTINGS2 register (bit 5)
writeRegister(0x0019, (1 << 5)); // ABIRES = 1
}
void loop() {
// SETTINGS1: ABIBIN = 1 (Binary mode)
writeAndVerify(0x0018, (1 << 5)); // ABIBIN = 1
// SETTINGS2: ABIRES = 001 (PPR = 512)
writeAndVerify(0x0019, (1 << 5)); // ABIRES = 001
uint16_t settings1 = readRegister(0x0018);
uint16_t settings2 = readRegister(0x0019);
bool abibin = (settings1 >> 5) & 0x01;
uint8_t abires = (settings2 >> 5) & 0x07;
Serial.print("ABIBIN: ");
Serial.println(abibin);
Serial.print("ABIRES: ");
Serial.println(abires);
delay(5000);
}
Can you help me to change abires and abibin values ?
Factroy settings changed 1 to 0 but others are not.