1
\$\begingroup\$

I am trying to make a module that needs to communicate over USART module, and also, do some tasks on a timer basis. (1ms / 30sec / 2 minutes)

Now I need to enable USART interrupt and enable a 1ms timer simultaneously so I can keep track of the running time and also, not losing the RX data while computing in my main loop.

Back on MikroC i could enable USART interrupts, but never managed to get both running at the same time. Now I have switched to XC8 compiler and I couldn't find any resources on how to achieve what I am trying to do. All the examples are either turning on the TIMER0 or USART interrupts and not both at the same time.

(I am a computer back-end developer, but because of some job requirements, I need to deliver this project on PIC18F14K50)

I would appreciate it if someone can give me some guide lines or point me toward a book/resource I can study!

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$
  1. Enable interrupt generation bit for each peripheral in the PIE registers
  2. If you need priority for interrupts (which you should) set priority bits in the IPR registers for each peripheral. this way a UART reception is guaranteed in case of a simultaneous interrupt with a timer interrupt.
  3. Enable peripheral and global interrupts.
  4. Write 2 ISRs; one for hi-priority interrupts and one for low priority ones.
  5. In the ISRs, check the flags to find out which peripheral has generated the interrupt, and act accordingly.

Code example:

//set low priority interrupt for timer 0
INTCON2bits.TMR0IP  = 0;
//enable it's interrupt on INTCON
INTCONbits.TMR0IE   = 1;

//enable high priority uart interrupts
RCONbits.IPEN = 1;
PIE1bits.RCIE    = 1;
//eusart 0 high priority interrupt
IPR1bits.RCIP    = 1;
//enable all peripheral and global interrupts
INTCONbits.GIE_GIEH  = 1;
INTCONbits.PEIE_GIEL = 1;

//==============================================================================
void __interrupt (high_priority) h_isr()
{
  if (PIR1bits.RCIF == 1)
  {
    //UART RX irq routine.
  }
  return;
}

void __interrupt (low_priority) l_isr()
{
  if (INTCONbits.TMR0IF)
  {
    //timer 0 irq routine.
    INTCONbits.TMR0IF = 0;
  }
  return;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.