10180 - Interrupt Programming

2011.06.30


Interrupt Programming

Is it too early to talk about ‘interrupts’ ?

If you cannot handle interrupts, then at the minimum, you cannot produce exact time intervals, which then means there is no real time clock etc. Although interrupts are sometimes called exceptions, almost all serious applications involve interrupts.

Most microcontrollers operate in about the same way, except interrupts. If you can write programs, in C language, for a particular microcontroller, you can always translate the program for another microcontroller by changing the names of the function registers. But this is not true if interrupts are involved. Different microcontrollers implement interrupts in different ways. In fact, it may be said that the only thing worth learning for a microcontroller is the topic on interrupts.

The problem of different implementation of interrupts by different microcontrollers is made more difficult as different C compilers handle interrupts differently for the same microcontroller. It is thus important to choose a suitable C compiler.

Hence, the handling of interrupts should be assured in the early stage of development before any decision to further develop applications.

This is the assurance:               (to be updated)
//  main63  On-board LED  Timer 4 Interrupt

#include <iostm8s105c6.h>

void mainRIM(void);
unsigned int m ;

int main( void ){
  
  PD_ODR = 0x00 ;   // D0  On-board LED  (0)ON
  PD_DDR = 0x01 ;   // Output
  PD_CR1 = 0x01 ;   // Push Pull
  PD_CR2 = 0x00 ;   // Slow
 
  TIM4_SR = 0x00 ;  // Clears Timer 4 Interrupt Flag
  TIM4_IER = 0x01 ; // Enable Timer 4 Interrupt
  TIM4_CR1 = 0x01 ; // Enable Timer 4
  m = 0 ;
  mainRIM();
 
  while(1){;}
  
  return 0;
}

#pragma vector = 25
__interrupt void TSL_Timer_ISR(void)
{
  TIM4_SR = 0x00 ;  // Clears Timer 4 Interrupt Flag

  m++ ;
  if(m==15000){
    PD_ODR = 0x00 ;           // PD0  On-board LED  '0'=ON
  }
  if(m==20000){ 
    m=0;
    PD_ODR = 0x01 ;           // D0  On-board LED  '1'=Off
  }
}

Of course, there is more to interrupts.