Hi everyone!
I am new in these forum.. I have read a lot of discussions here and thout I can get a little bit of help here.
Latley I stated to work in a project with the arduino MEGA2560.
I managed to operate motors utilizing pins associated with Output Compare Registered (timer 3 and timer 4). I used Fast PWM according to the following line in the microcontroller datasheet:
After that i tried to use another Compare Match interrupt (with timer 2) in the same code, in order to change motors speed and direction. That is where the fun begins.
As i learned from another project (with Arduino Uno) each interrupt have hierarchy, and it goes in the order at the following table where the higher lines are higher in hierarchy (sorry for the link, cannot attach more than one image).
In the Uno project it woked! But for some reason it didn't work here, maybe i'm wrong and this is not the hierarchy?..
to be sure, I replced the interrupt command with a simple operation (blinking LED).
When I put setup of timers 3 and 4 under comment it blinks. When I DO setup at least one of the PWM timers then the motor works properly. Though the LED is not blinking anymore..
I am using two lines for each motor in order to work with two directions. That why two OCRx registers for each motor/timer.
I am programming in VS Code with Platform IO. Here is my code:
#include <Arduino.h>
#define RIGHT_FORWARD 2
#define RIGHT_BACKWARD 3
#define LEFT_FORWARD 6
#define LEFT_BACKWARD 7
byte RFS = 0x8F,
RBS = 0,
LFS = 0x8F,
LBS = 0;
bool ledState = 0;
byte state = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, 0);
pinMode(RIGHT_FORWARD, OUTPUT);
pinMode(RIGHT_BACKWARD, OUTPUT);
pinMode(LEFT_FORWARD, OUTPUT);
pinMode(LEFT_BACKWARD, OUTPUT);
// // Drive D2 & D3 HIGH when TCNT3 < OCR3x while up-counting
// // Drive D2 & D3 LOW when TCNT3 > OCR3x while up-counting
// //>>>>>>>> DATASHEETS PG. 154
// // Set outputs to work in Fast PWM mode
// // >>>>>>>> DATASHEETS PG. 145 <<<<<<<<
// // Set timers to count without prescalar
// // >>>>>>>> DATASHEETS PG. 156 <<<<<<<<
TCCR3A = 0b00101001; // Pin toggling mode (HIGH to LOW)
TCCR3B |= 0b00001001; // Prescalar and Fast PWM
TCCR3B &= 0b11111001;
TIMSK3 |= 0b00001100; // Enable On-Compare-Interrupt B&C
OCR3B = RFS; // RIGHT_FORWARD speed (MAX is 0xFF)
OCR3C = RBS; // RIGHT_BACKWARD speed (MAX is 0xFF)
// //Drive D6 & D7 HIGH when TCNT4 < OCR4x while up-counting
// // Drive D6 & D7 LOW when TCNT4 > OCRx while up-counting
// //>>>>>>>> DATASHEETS PG. 154
// // Set outputs to work in Fast PWM mode
// // >>>>>>>> DATASHEETS PG. 145 <<<<<<<<
// // Set timers to count without prescalar
// // >>>>>>>> DATASHEETS PG. 156 <<<<<<<<
TCCR4A = 0b10100001; // Pin toggling mode (HIGH to LOW)
TCCR4B |= 0b00001001; // Prescalar and Fast PWM
TCCR4B &= 0b11111001;
TIMSK4 |= 0b00000110; // Enable On-Compare-Interrupt B&C
OCR4A = LFS; // LEFT_FORWARD speed (MAX is 0xFF)
OCR4B = LBS; // LEFT_BACKWARD speed (MAX is 0xFF)
// LED blink interrupt
TCCR1A = 0;
TCCR1B = 0b00000100;
TCNT1 = 0;
TIMSK1 = 0x02;
OCR1A = 65000;
sei();
}
void loop() {
}
ISR(TIMER1_COMPA_vect){
TCNT1 = 0;
digitalWrite(13, ledState);
ledState = !ledState;
}
Thank you all!