riccardo
Member
Hello. Me again!
I'm trying to set up the four 16-bit timers of an ATMega2560 to make a pulse on four output pins (to step 4 motors at different speeds). At the moment I am just trying to make one work and not succeeding. I was hoping to use the OCnA function so that when my program is doing other things, the motor steps are not interfered with signigficantly.
Reading the datasheet about the different configurations and registers is making my head spin and I think I am just making some mistakes in the register config and inmplementation.
The code below compiled in the Arduino IDE is what I have so far. My intention is to have it set a pin (M1_STEP) high for some time, then low for a different amount.
The LED lights up and I can see a sqare wave on the LED pin so It seems the interrupt part works. However....
The duty seems stuck on 50%
Nothing is happening on the OC1A pin
Any suggestions welcome.
I'm trying to set up the four 16-bit timers of an ATMega2560 to make a pulse on four output pins (to step 4 motors at different speeds). At the moment I am just trying to make one work and not succeeding. I was hoping to use the OCnA function so that when my program is doing other things, the motor steps are not interfered with signigficantly.
Reading the datasheet about the different configurations and registers is making my head spin and I think I am just making some mistakes in the register config and inmplementation.
The code below compiled in the Arduino IDE is what I have so far. My intention is to have it set a pin (M1_STEP) high for some time, then low for a different amount.
C:
#define LED_M1_SIG 36
#define M1_STEP 11
volatile boolean pinState = LOW;
ISR(TIMER1_COMPA_vect) {
TCNT1 = 0; // Reset timer
pinState = !pinState; // OC1A pin should have just toggled state, so record it here.
if (pinState) {
OCR1A = 100; // Set pin low time
} else {
OCR1A = 1000; // Set pin High time
}
digitalWrite(LED_M1_SIG,pinState); // Light LED just to show interrupt is working
}
void setup() {
pinMode(LED_M1_SIG, OUTPUT);
pinMode(M1_STEP, OUTPUT);
OCR1A = 1000;
TCCR1A |= (1 << COM1A0) | (1 << WGM10) | (1 << WGM11) | (1 << WGM12) | (1 << WGM13); // Toggle pin on compare match for OC1 only // also sets WGM (Datasheet p145)
TCCR1B |= (1 << CS01) | (1 << CS00); // Set prescaler
TIMSK1 |= (1 << OCIE1A); // Set interrupt on compare match
sei(); // set global interrupts
}
void loop() {
// put your main code here, to run repeatedly:
}
The LED lights up and I can see a sqare wave on the LED pin so It seems the interrupt part works. However....
The duty seems stuck on 50%
Nothing is happening on the OC1A pin
Any suggestions welcome.