Hi,
I want to use interrupts and enable global interrupts in my robot project so that it checks the sensor and moves the motors accordingly every 20 ms. But i found very little information about using sei() except that it's to enable global variables and that its equivalent is interrupts()
I found some bits of code in my research but in some of them, they use only sei() with no cli() and in others, the cli() comes before the sei(). Is there a reason to disable interrupts before enabling them at the end of the function?
An example:
void init_PID() {
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to set sample time 5ms
OCR1A = 9999;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for prescaling by 8
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // enable global interrupts
}