Tuesday, January 5, 2021

PIC16F887 Timer0 Interrupt Programming in MikroC

Introduction

Timer0 module is able to trigger interrupt even it's operate in timer or counter mode. It must be enabled and handled in ISR to get full advantage. 

Timer0 interrupt is a thread written in ISR that doesn't need to be polled in main program loop. It's automatically called whenever its interrupt flag is set, then it's executed and automatically return to previous program context.

Timer interrupt is useful for creating a timer tick that schedule tasks in microcontroller program. However we use this advantage to create a simple 1ms timing delay seeing its effect. It's other than an output square wave.

PIC16F887 Timer0 Interrupt Programming in MikroC
Simulation sample of this programming example

Program Configuration

Main Program

In main program setting up, RD0 of Port D is output pin. It must be configure to output direction in its corresponding TRISD register.

Oscillator is driven from PIC16F887 internal RC oscillator using its maximum 8MHz frequency. 

Timer0 module is selected to operate in timer mode with 1:2 prescaler.

Global interrupt, Timer0 interrupt must be enabled. It's interrupt flag and Timer0 register must be cleared first.

Main program loop stay idle as there's no additional code to handle.

Interrupt Service Routine 

To handle interrupt, targeting interrupt source must be written in interrupt service routine (ISR). Timer0 interrupt flag must be tested first and clear before remaining code.

void interrupt() {
     if(INTCON.T0IF) {
        counter++;
        if(counter==Ticks_Per_Ms)
           { counter=0; OUTPUT^=1; }
           }
     INTCON.T0IF=0;    
}

The void interrupt() is MikroC compiler's reserve keyword used for ISR. It's unique for all interrupt source in Mid-Range PIC16F887 microcontroller.



Creating Timer Tick 

PIC executing speed is,

Fins = (Fosc)/4 = (8MHz)/4 = 2MHz.

Its executing period is,

Tins = 1/(Fins) = 1/(2MHz) =  500ns.

Timer0 pulses at 1:2 rate. It's increment for every,

Ttimer0 = 2 * 500ns = 1us.

Timer0 overflows when,

256 * 1us = 256us.

If it overflows for four time it create a timing tick of about 1ms.

4 * 256us = 1024us. 

MikroC Program

C program lists about is written for this programming example.



Click here to download this example.

Diagram lists below is its full schematic.

PIC16F887 Timer0 Interrupt Programming in MikroC
Schematic diagram

 

No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)