Thanks Brandon, that makes sense.
Still having some trouble getting it to run. Would you mind having a look over my code and see if you can spot any obvious errors?
Main loop:
setup();
while(1){
settargetvelocityfor();
delaywhileresetting(2000);
settargetvelocityback();
delaywhileresetting(2000);
}
Functions:
void resetcommandtimeout(void){
uint8_t command = 0x8C;
uint8_t command0 = (0x1C);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command, 1, HAL_MAX_DELAY);
}
void exitsafestart(void){
uint8_t command = 0x83;
uint8_t command0 = (0x1C);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command, 1, HAL_MAX_DELAY);
}
void energize(void){
uint8_t command = 0x85;
uint8_t command0 = (0x1C);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command, 1, HAL_MAX_DELAY);
}
void setup(void){
HAL_Delay(20);
exitsafestart();
energize();
}
void delaywhileresetting(uint32_t ms){
uint32_t start = HAL_GetTick();
resetcommandtimeout();
uint32_t comp = HAL_GetTick();
resetcommandtimeout();
while ((comp - start) <= ms){
resetcommandtimeout();
comp = HAL_GetTick();
}
}
void settargetvelocityfor(void){
uint8_t command0 = (0x1C);
uint8_t command1 = 0xE3;
uint8_t command2 = 0x40;
uint8_t command3 = 0xD;
uint8_t command4 = 0x3;
uint8_t command5 = 0x0;
HAL_I2C_Master_Transmit(&hi2c2, command0, &command1, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command2, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command3, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command4, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command5, 1, HAL_MAX_DELAY);
}
void settargetvelocityback(void){
uint8_t command0 = (0x1C);
uint8_t command1 = 0xE3;
uint8_t command2 = 0xC0;
uint8_t command3 = 0xF2;
uint8_t command4 = 0xFC;
uint8_t command5 = 0xFF;
HAL_I2C_Master_Transmit(&hi2c2, command0, &command1, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command2, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command3, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command4, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Transmit(&hi2c2, command0, &command5, 1, HAL_MAX_DELAY);
}
Thanks again for all the help. Hopefully we can get this running.