Skip to content

Instantly share code, notes, and snippets.

@OrhanYigitDurmaz
Last active April 8, 2024 18:54
Show Gist options
  • Save OrhanYigitDurmaz/84af60395e3d15bab7b69e8524561fed to your computer and use it in GitHub Desktop.
Save OrhanYigitDurmaz/84af60395e3d15bab7b69e8524561fed to your computer and use it in GitHub Desktop.
/*
*
* Dummy Header
*
* (if this is not here, i cant read the code)
*
*/
#include <Arduino.h>
#include <SPI.h>
#define MT6835_REG_ABZ_RES1 0x007
#define MT6835_REG_ABZ_RES2 0x008
#define MT6835_OP_READ 0b0011
#define MT6835_OP_WRITE 0b0110
#define MT6835_WRITE_ACK 0x55
int startSPI = 2; //pin to set the slave active
union MT6835ABZRes {
struct {
uint8_t ab_swap:1;
uint8_t abz_off:1;
uint8_t abz_res_low:6;
};
uint8_t reg;
};
union MT6835Command {
struct {
uint32_t unused:8;
uint32_t data:8;
uint32_t addr:12;
uint32_t cmd:4;
};
uint32_t val;
};
void setup() {
pinMode(startSPI, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//SPI.begin();
//SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
//SPI.setBitOrder(MSBFIRST);
Serial.begin(115200);
}
void loop() {
setABZResolution(10240);
writeEEPROM();
delay(7000); //wait at least 6s
digitalWrite(LED_BUILTIN, HIGH);
while (1) {
delay(1000);
}
}
void setABZResolution(uint16_t res){
uint8_t hi = (res >> 6);
MT6835ABZRes lo = {
.reg = readRegister(MT6835_REG_ABZ_RES2)
};
lo.abz_res_low = (res & 0x3F);
writeRegister(MT6835_REG_ABZ_RES1, hi);
writeRegister(MT6835_REG_ABZ_RES2, lo.reg);
};
bool writeRegister(uint16_t reg, uint8_t value) {
MT6835Command cmd;
cmd.cmd = MT6835_OP_WRITE;
cmd.addr = reg;
cmd.data = value;
transfer24(&cmd);
return cmd.data == MT6835_WRITE_ACK;
};
uint8_t readRegister(uint16_t reg) {
MT6835Command cmd;
cmd.cmd = MT6835_OP_READ;
cmd.addr = reg;
transfer24(&cmd);
return cmd.data;
};
void transfer24(MT6835Command* outValue) {
uint32_t buff = __builtin_bswap32(outValue->val);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
digitalWrite(startSPI, LOW);
SPI.transfer(&buff, 3);
digitalWrite(startSPI, HIGH);
SPI.endTransaction();
outValue->val = __builtin_bswap32(buff);
};
bool writeEEPROM(){
delay(1); // wait at least 1ms
MT6835Command cmd;
cmd.cmd = MT6835_OP_PROG;
cmd.addr = 0x000;
transfer24(&cmd);
return cmd.data == MT6835_WRITE_ACK;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment