ogahpaul:
Thank you johnwasser for following me up with patience even in my ignorance. I am not willing to give up, not until my problem is solved. Please check what I drew up from your explanation and verify if the program is correct. Don't get pissed if am messing up things the more, am new to this./*
Timers 1 and 2 are used to generate pulses for the the low side transistors.
TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM
mode set by the COM bits for each Timer.
*/
const byte Q5Mask = 0b00000001; // Arduino Pin 8
const byte Q6Mask = 0b00000010; // Arduino Pin 9 (PWM on OCR1A)
const byte Q4Mask = 0b00000100; // Arduino Pin 10 (PWM on OCR1B)
const byte Q2Mask = 0b00001000; // Arduino Pin 11 (PWM on OCR2A)
const byte Q3Mask = 0000010000; // Arduino Pin 12
const byte Q1Mask = 0b00100000; // Arduino Pin 13
void SetPins(byte bitmask)
{
// For the three PWM pins, enable PWM if the bit is 1, otherwise disable PWM on that pin
// The COM1A0, COM1B0 and COM2A0 bits are always 0
// Setting the COM1A1, COM1B1 or COM2A1 bits to 1 will set that pin to PWM
// Setting the COM1A1, COM1B1 or COM2A1 bits to 0 will set that pin to Normal
bitWrite(TCCR1A, COM1A1, (bitmask & Q2Mask));
bitWrite(TCCR1A, COM1B1, (bitmask & Q4Mask));
bitWrite(TCCR2A, COM2A1, (bitmask & Q6Mask));
// Now write all of the bits to PORTB. The Normal pins will be set to the state
// in the bit: 0=LOW, 1=HIGH. The pins set to PWM will override the PORTB values.
PORTB = bitmask;
const byte Steps[6] =
{
B00100110, // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
B00110010, // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
B00011010, // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
B00011001, // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
B00001101, // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
B00100101 // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
};
byte StepCount = 0;
const byte PotentiometerAIPin = A3; //The speed control is done using a potentiometer connected on pin A3
unsigned motorspeed = 4000;
unsigned long beginMicros = 0; //time counting using the microfunction
}
void setup()
{
Serial.begin (250000);
while (!Serial); // In case a version ever runs on a 32U4 processor (Leonardo, Micro, etc)
beginMicros = micros();
}
void loop()
{
unsigned long currentMicros = micros();
// Set the time between steps 1 to 1000µs using the potentiometer
motorspeed = map(analogRead(PotentiometerAIPin), 0, 1024, 1, 1000);
// Display the motor speed if it has been changed
static unsigned oldMotorSpeed = 0;
if (motorspeed != oldMotorSpeed)
{
oldMotorSpeed = motorspeed;
Serial.println(motorspeed);
}
// Is it time for a step?
if (currentMicros - beginMicros >= motorspeed)
{
beginMicros += motorspeed; // Step taken
// Rotor moves from Phase A to C'
PORTB = Steps[StepCount];
StepCount = (StepCount + 1) % 6; // 0, 1, 2, 3, 4, 5, 0, 1, 2, ...
}
}
The attached document might give a little more insight to what the controls should look like.