Activity 2. Delay function using Timer0

In this activity we will use Timer0 to create our own "delay" function that lasts 100us

(75 minutes)


Activity 2


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/1, therefore, the frequency in the output of the Prescaler is 12MHz.

Accordingly, the period at the input of timer 0 (Timer0) will be: 83.33 Χ 1 = 83.33 ns.

 

Timer0 should be initialized so that Timer0 overflows every 100us.

 

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:

100us=100000ns  

 

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)]x83.33 ns= 100000 ns.

(Initial value of Timer0) = 65536-1200 =64336

   

 


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.*/                                                           

 

void init (void);                      

void timer0_int(void);        

//function for our delay

void mydelay_100us(int); 

 

int32 counter_time=0;                                                            

//Declare a variable to count the interrupts.

//It will increment by 1 every 100 µs

 

int32 counter_time_old=0;                                                 

int32 aaa=1;                                                                      

 

void main(){                            

   init();    

   while (TRUE){

      PORTB=PORTB^0b11111111;

      mydelay_100us(500); 

      //delay 500Χ100μs=50 000 μs= 50 ms

   }                             

}

 

//Interrupt Service Routine

#INT_TIMER0                     

void timer0_int(void) {

   set_timer0(64336);                                                         

   counter_time++;                                                             

}

 

void init (void) {      

   SETUP_TIMER_0(T0_INTERNAL | T0_DIV_1 );                                        

   set_timer0(64336);                                                         

   enable_interrupts(INT_TIMER0);                                           

   enable_interrupts(GLOBAL);                                              

   set_tris_b(0x00);         

   PORTB=0x00;                                                               

}

 

void mydelay_100us(aaa){

   counter_time_old=counter_time;                                                 

   while(counter_time < counter_time_old+aaa) {  }

}


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.



Step 7. Modifications and discussion.
If the microcontroller oscillator is 48MHz, what is the maximum time it can count before the Timer0 overflows?