Activity 1. Interrupt every 10ms

The purpose of this activity is to write a program with an interrupt service routine from Timer0 that blinks the LEDs connected to PORTB every 200 ms. Caution! No delay functions will be used in the program. As a time base, Timer0 will be set to trigger an interrupt every 10ms.

(60 minutes)


Activity 1


Step 1. The circuit is drawn in the Proteus Design Suite.



Step 2. Timer0 configurations.


The frequency in the input of the Prescaler is 12MHz.

We chose from the Prescaler the value of 1/64, therefore, the frequency in the output of the Prescaler is 12 / 64 = 0.1875MHz.

Accordingly, the period at the input of timer 0 (Timer0) will be: 83.33 ns X 64 =5333ns

 

Timer0 should be initialized so that timer0 overflows every 10ms.

Overflowing timer0 means going from the value FFFF to the value 0000. The time it takes for timer0 to go from the initial value given to it until it overflows (and thus interrupts) should be:

10ms=10 000 μs=10 000 000 ns    

 

It is reminded that (FFFF)h = (65535)d

 

The number of steps from the initial value of Timer0 until it overflows will be: 65536-(Initial value of Timer0). That is:

[65536-(Initial value of Timer0)]x5333 ns= 10 000 000 ns.

(Initial value of Timer0) = 65536-1875 =63661




Step 3. The program in C language is written.

Write in CCS C Compiler the program in C language

#include <main.h> // the file main.h with the

          // initial settings is included.

          // This file must be placed in the same

          // folder with the project.

          // Also the 18F4550.h file must exist

          // in the same folder with the project

               

                           

#byte PORTB =0xF81  

/* We attribute to the memory position 0xF81 the name PORTB. This means that we define an 8-bit variable whose value will be stored to the memory position F81h.*/

#byte PORTD =0xF83

// The position F83h is the PORTD data register.

 

void init (void);

int counter1=20;            

            

void main()

{        

   init();

   while (TRUE){

        ;

   }

}        

 

// Interrupt Service Routine

#INT_TIMER0

void timer0_int(void){

   set_timer0(63661);

   counter1--;

   if (counter1==0){

      counter1=20;

      PORTB=PORTB^0b11111111;      

   }             

}                

 

 

void init (void)

{      

   // Prescaler value = 1/64

   SETUP_TIMER_0(T0_INTERNAL | T0_DIV_64 );

   set_timer0(63661);

   enable_interrupts(INT_TIMER0);

   enable_interrupts(GLOBAL);

 

   set_tris_b(0x00);                   

   PORTB=0x00;                         

}

          

/* Interrupt time:  (65536-63661)*[1/(Fclock/4)]*Prescaler = 9.994ms   */


Step 4. Compile the program in C in order to create the program in the microcontroller machine code (hex file).



Step 5. Load to the microcontroller the hex file (program in machine code) that was created from the CCS C Compiler.



Step 6.  Run the simulation and check the correct operation of the circuit.