How to write values to the settings 1 register? #45
-
How to write values to the settings 1 register?I received the following question via e-mail:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The following code is an example how to write values to the settings 1 register of the AS5047P sensor. This example can be adopted for the settings 2 register by changing the corresponding datatypes. /**
* @file main.cpp
* @author Jonas Merkle [JJM] ([email protected])
* @brief Write to the settings 1 register of a AS5047P sensor.
* @date 2023-06-29
*
* @copyright Copyright (c) 2023 Jonas Merkle. This project is released under the GPL-3.0 License License.
*
* More Information can be found here:
* https://github.com/jonas-merkle/AS5047P
*/
// include the library for the AS5047P sensor.
#include <AS5047P.h>
// include the arduino main library (not needed when using the official arduino ide)
#include <Arduino.h>
// define the chip select port.
#define AS5047P_CHIP_SELECT_PORT 9
// define the spi bus speed
#define AS5047P_CUSTOM_SPI_BUS_SPEED 100000
// initialize a new AS5047P sensor object.
AS5047P as5047p(AS5047P_CHIP_SELECT_PORT, AS5047P_CUSTOM_SPI_BUS_SPEED);
void waitForAnySerialKeyPress() {
Serial.read();
}
// arduino setup routine
void setup() {
// initialize the serial bus for the communication with your pc.
Serial.begin(115200);
// wait for serial port to connect. Needed for native USB
while (!Serial) {
;
}
// initialize the AS5047P sensor and hold if sensor can't be initialized.
while (!as5047p.initSPI()) {
Serial.println(F("Can't connect to the AS5047P sensor! Please check the connection..."));
delay(5000);
}
// initialize a new object which represents the settings 1 register
auto settingsToWrite = new AS5047P_Types::SETTINGS1_t();
// initialize a new object which represents error information
auto errorInfo = AS5047P_Types::ERROR_t();
// set the raw register value
settingsToWrite->data.raw = 0x0088;
// or optional: you can set the individual values of the register entries with the following commands:
//settingsToWrite->data.values.FactorySetting = 0;
//settingsToWrite->data.values.NOISESET = 0;
//settingsToWrite->data.values.DIR = 0;
//settingsToWrite->data.values.UVW_ABI = 1;
//settingsToWrite->data.values.DAECDIS = 0;
//settingsToWrite->data.values.ABIBIN = 0;
//settingsToWrite->data.values.Dataselect = 0;
//settingsToWrite->data.values.PWMon = 1;
// (optional) wait for any key to be pressed in the serial terminal to continue
Serial.print("Press any key to continue...");
while (!Serial.available()) {
;
}
Serial.println("");
// log
Serial.print("Wirte settings 1 register to sensor... ");
// write the settings 1 register to the sensor
bool result = as5047p.write_SETTINGS1(settingsToWrite, &errorInfo, true, true);
// log
if (result) {
Serial.println(" Done successful!");
}
else {
Serial.println(" Failed!");
// print potential error information
Serial.println(errorInfo.toArduinoString());
}
// print all entires from settings 1 register
// read the settings 1 register from the sensor
auto settingsReadBack = as5047p.read_SETTINGS1();
// print the settings 1 register which was read from the sensor
Serial.print("SETTINGS1.values.FactorySetting: "); Serial.println(settingsReadBack.data.values.FactorySetting);
Serial.print("SETTINGS1.values.NOISESET: "); Serial.println(settingsReadBack.data.values.NOISESET);
Serial.print("SETTINGS1.values.DIR: "); Serial.println(settingsReadBack.data.values.DIR);
Serial.print("SETTINGS1.values.UVW_ABI: "); Serial.println(settingsReadBack.data.values.UVW_ABI);
Serial.print("SETTINGS1.values.DAECDIS: "); Serial.println(settingsReadBack.data.values.DAECDIS);
Serial.print("SETTINGS1.values.ABIBIN: "); Serial.println(settingsReadBack.data.values.ABIBIN);
Serial.print("SETTINGS1.values.Dataselect: "); Serial.println(settingsReadBack.data.values.Dataselect);
Serial.print("SETTINGS1.values.PWMon: "); Serial.println(settingsReadBack.data.values.PWMon);
}
// arduino loop routine
void loop() {
// nothing to do here
} |
Beta Was this translation helpful? Give feedback.
The following code is an example how to write values to the settings 1 register of the AS5047P sensor. This example can be adopted for the settings 2 register by changing the corresponding datatypes.
Please keep in mind that the code may need to be modified to be compatible with the hardware you are using. And please check (with the help of the sensor-datasheet) if the values are valid before writing the values into the Settings 1 register.