9.2.3 ei Macro

A macro that enables interrupts.

Include

<xc.h>

Prototype

void ei(void);

Remarks

This macro sets the GIE bit in the INTCON register. The MCC-generated INTERRUPT_GlobalInterruptEnable() function performs the same task as the ei() macro .

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <xc.h>
#include <stdio.h>

unsigned int count;

void __interrupt() myIsr (void) {
    if(INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1) {
        INTCONbits.TMR0IF = 0;
        TMR0 = timer0ReloadVal;
        count++;
    }
}

unsigned int getTicks(void) {
  unsigned int val;

  di();         /* disable interrupts */
  val = count;  /* protected non-atomic operation */
  ei();         /* re-enable interrupts */
  return val;
}

int main(void) {
  SYSTEM_Initialize();                 /* MCC generated code */
  INTERRUPT_GlobalInterruptEnable();   /* MCC alternative to ei() */
  INTERRUPT_PeripheralInterruptEnable();
  while(getTicks() < 0x400)            /* wait for 0x400 overflows */
    continue;
  INTERRUPT_GlobalInterruptDisable();  /* MCC alternative to di() */
  printf("done");
  while(1)
    continue;
}