This is a segment of an Arduino sketch, that I am trying to understand.
I know that the purpose of the entire code is to control 2 stepper motors linked to their respective drivers which are linked to a Mega 2560, but since I am new to Arduino, I am having problems with some functions.
This is a segment of the code. I am having problems with some statements such as unsigned short FLG = 0;
and I cannot make out at all the timer interrupts part.
What does it all mean?
/* The “define” function is NOT A PROGRAMMING STATEMENT. Actually it sets up a macro which causes a text replace to occur before code is compiled. The pre-processor of the IDE will replace the variables such as “stp” to the integer value 22 in the following subsequent sections like the “void setup” function and the “loop” function if they are called. This function helps reduce RAM usage. */
#define stp 22
#define dir 23
#define stp2 26
#define dir2 27
#define EN 24
#define dir_ 25
int i, j, t, X = 0, Y = 0, Z = 0;
// here, the integer is creating variables such as i, j, t
//and also assigning values to some of them such as X, Y and Z.
unsigned short FLG = 0;
unsigned int Speed1 = 0, Speed2 = 0; // creating variables assigning initial values; due to unsinged only positive
int ser_1[3];
int po(int, int);
void setup() { // You define how your Arduino will be used and set up any pins and communications you will be using. This will run first and only once.
Serial.begin(9600); // begin the data transmission between Arduino and pc at speed of 9600 bits per sec
delay(500); // stop for 500 milliseconds
Serial.println(“hi”); // this will print the word “hi” on the serial monitor window on pc via serial port
pinMode(stp, OUTPUT); // tells arduino to use digital Pin 22 to output electricity
pinMode(dir, OUTPUT); // tells arduino to use digital Pin 23 to output electricity
pinMode(stp2, OUTPUT); // tells arduino to use digital Pin 26 to output electricity
pinMode(dir2, OUTPUT); // tells arduino to use digital Pin 27 to output electricity
pinMode(EN, OUTPUT); // tells arduino to use digital Pin 24 to output electricity
noInterrupts(); // to prevent disruption of timing of codes, disables some functions in the background
/* To use timers, the built-in timer registers on the AVR chip that store timer settings need to be configured. There are a number of registers per timer. Two of these registers –the Timer/Counter Control Registers- hold setup values, and are called TCCRxA and TCCRxB, where x is the timer number (TCCR1A and TCCR1B, etc.). Each register holds 8 bits, and each bit stores a configuration value. */
TCCR1A = 0x80; //
TCCR1B = 0x0A; // Enable reset on compare match, 1:8 clock. don’t understand
OCR1AH = 0x9C;
OCR1AL = 0x3F;
TIMSK1 = (1 << OCIE1A); // enable timer compare interrupt
interrupts();
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
res();
}