Thats my issue... I have NO idea on how to write the register, and handle low level programming...
Tomorrow the logic analyzer will arrive and then i'll be able to test it precisely, I just found out the un-sync with a oscilloscope, I'll look on the leaflabs and the stm32duino cores to try finding the how to's!!!
I did a while ago fixed a SSD from OCZ... lord knows how but the atmel eeprom was corrupted, had tree ssd's two working just cloned one to the other and it worked...
The ssd's should have a diagnostic system... usually they have a RX TX pins but nothing useful for us mortals...
Here's the code, it's messy for now I put 3 arrays one for each fase... I'll use only one with a 120 degree diference for each phase, I tought that the problem was there so i made the code as direct as possible... Only after all that I discovered that the problem was the un-sync timers! And the speed control etc. is just for testing! At least the hardware works perfectly
Code: Select all
//INIT
const int8_t sineU[] = {
0,3,7,10,14,17,21,24,28,31,34,37,41,44,47,50,53,56,59,62,64,67,69,72,74,77,79,81,83,85,
87,88,90,91,93,94,95,96,97,98,98,99,99,99,99,99,99,99,99,99,98,98,97,96,95,94,93,91,90,88,
87,85,83,81,79,77,74,72,69,67,64,62,59,56,53,50,47,44,41,37,34,31,28,24,21,17,14,10,7,3,
0,-3,-7,-10,-14,-17,-21,-24,-28,-31,-34,-37,-41,-44,-47,-50,-53,-56,-59,-62,-64,-67,-69,-72,-74,-77,-79,-81,-83,-85,
-87,-88,-90,-91,-93,-94,-95,-96,-97,-98,-98,-99,-99,-99,-99,-99,-99,-99,-99,-99,-98,-98,-97,-96,-95,-94,-93,-91,-90,-88,
-87,-85,-83,-81,-79,-77,-74,-72,-69,-67,-64,-62,-59,-56,-53,-50,-47,-44,-41,-37,-34,-31,-28,-24,-21,-17,-14,-10,-7,-3};
const int8_t sineV[] = {
87,85,83,81,79,77,74,72,69,67,64,62,59,56,53,50,47,44,41,37,34,31,28,24,21,17,14,10,7,3,
0,-3,-7,-10,-14,-17,-21,-24,-28,-31,-34,-37,-41,-44,-47,-50,-53,-56,-59,-62,-64,-67,-69,-72,-74,-77,-79,-81,-83,-85,
-87,-88,-90,-91,-93,-94,-95,-96,-97,-98,-98,-99,-99,-99,-99,-99,-99,-99,-99,-99,-98,-98,-97,-96,-95,-94,-93,-91,-90,-88,
-87,-85,-83,-81,-79,-77,-74,-72,-69,-67,-64,-62,-59,-56,-53,-50,-47,-44,-41,-37,-34,-31,-28,-24,-21,-17,-14,-10,-7,-3,
0,3,7,10,14,17,21,24,28,31,34,37,41,44,47,50,53,56,59,62,64,67,69,72,74,77,79,81,83,85,
87,88,90,91,93,94,95,96,97,98,98,99,99,99,99,99,99,99,99,99,98,98,97,96,95,94,93,91,90,88};
const int8_t sineW[] = {
-87,-88,-90,-91,-93,-94,-95,-96,-97,-98,-98,-99,-99,-99,-99,-99,-99,-99,-99,-99,-98,-98,-97,-96,-95,-94,-93,-91,-90,-88,
-87,-85,-83,-81,-79,-77,-74,-72,-69,-67,-64,-62,-59,-56,-53,-50,-47,-44,-41,-37,-34,-31,-28,-24,-21,-17,-14,-10,-7,-3,
0,3,7,10,14,17,21,24,28,31,34,37,41,44,47,50,53,56,59,62,64,67,69,72,74,77,79,81,83,85,
87,88,90,91,93,94,95,96,97,98,98,99,99,99,99,99,99,99,99,99,98,98,97,96,95,94,93,91,90,88,
87,85,83,81,79,77,74,72,69,67,64,62,59,56,53,50,47,44,41,37,34,31,28,24,21,17,14,10,7,3,
0,-3,-7,-10,-14,-17,-21,-24,-28,-31,-34,-37,-41,-44,-47,-50,-53,-56,-59,-62,-64,-67,-69,-72,-74,-77,-79,-81,-83,-85};
//Motor
int32_t motorStep = 0, motorPhase = 0, motorStepTimer = 0;
byte NRD = 1;
int throttle = 0;
//Debounces
uint32_t encoderDebounce = 0, gearDebounce = 0;
//PIN ASSIGNMENT
#define UH PB9
#define UL PB8
#define VH PB7
#define VL PB6
#define WH PA10
#define WL PA9
#define REVERSESW PB1
#define DRIVESW PB0
//int UH = PB9, UL = PB8, VH = PB7, VL = PB6, WH = PA10, WL = PA9;
//int REVERSESW = PB1, DRIVESW = PB0;
//FLOATS AND OTHERS
int32_t kU = 0, kV = 0, kW = 0;
int16_t jU = 0, jV = 0, jW = 0, currentStep = 0, oldStep = 0;
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup(){
//PINMODE
afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY);
adc_set_sample_rate(ADC1, ADC_SMPR_7_5); adc_set_sample_rate(ADC2, ADC_SMPR_7_5);
HardwareTimer timer1(1); timer1.setPeriod(40);
HardwareTimer timer4(4); timer4.setPeriod(40);
//Serial3.begin(200000);
pinMode(REVERSESW, INPUT_PULLUP); pinMode(DRIVESW, INPUT_PULLUP);
pinMode(UL, PWM); pinMode(VL, PWM); pinMode(WL, PWM); pwmWrite(UL,0); pwmWrite(VL,0); pwmWrite(WL,0);
pinMode(UH, PWM); pinMode(VH, PWM); pinMode(WH, PWM); pwmWrite(UH,0); pwmWrite(VH,0); pwmWrite(WH,0);
//Charge BOOTSTRAP
pwmWrite(UL,2880); pwmWrite(VL,2880); pwmWrite(WL,2880);
delay(50);
pwmWrite(UL,0); pwmWrite(VL,0); pwmWrite(WL,0);
motorSquare();
//Throttle
pinMode(PA0, INPUT_ANALOG); pinMode(PA2, INPUT_PULLUP); pinMode(PA7, INPUT_PULLUP);
} //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop(){
//Throttle
throttle = adc_read(ADC1, 0);
//if(throttle < 409){throttle = 0;}
//GEAR
//if(digitalRead(REVERSESW) == digitalRead(DRIVESW)){NRD = 2;}
//if(digitalRead(REVERSESW) == LOW){NRD = 1;} if(digitalRead(DRIVESW) == LOW){NRD = 3;}
//if(NRD == 2){throttle = 0;}
if(micros() - motorStepTimer > throttle*100){motorSine();}
} // VOID LOOP END --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void motorSine(){
kU = (sineU[motorStep]*288)/10;
if(kU == 0){pwmWrite(UH,0); pwmWrite(UL,0);}
if(kU > 0){pwmWrite(UH,kU); pwmWrite(UL,0);}
if(kU < 0){kU = kU*-1; pwmWrite(UH,0); pwmWrite(UL,kU);}
kV = (sineV[motorStep]*288)/10;
if(kV == 0){pwmWrite(VH,0); pwmWrite(VL,0);}
if(kV > 0){pwmWrite(VH,kV); pwmWrite(VL,0);}
if(kV < 0){kV = kV*-1; pwmWrite(VH,0); pwmWrite(VL,kV);}
kW = (sineW[motorStep]*288)/10;
if(kW == 0){pwmWrite(WH,0); pwmWrite(WL,0);}
if(kW > 0){pwmWrite(WH,kW); pwmWrite(WL,0);}
if(kW < 0){kW = kW*-1; pwmWrite(WH,0); pwmWrite(WL,kW);}
motorStep++;
if(motorStep < 0){motorStep = 179;}
if(motorStep > 179){motorStep = 0;}
motorStepTimer = micros();
}