You are on page 1of 11

TIMERS AND INTERRUPTS

I/ Introduction

- Timers are used for many function such as: timming reference to generate an event, count
the number of events, waveform generation. Pic16F887 has 3 timers: Timer0 (8bit) , Timer1
(16bit), Timer2 (8bit). The timer can configure as internal or external clock source.

- The PIC16F887 has one external hardware interrupt. Pin RB0 of the PIC16F887,
designated as INTO is used as external hardware interrupt. Upon activation of this pin, the
PIC 16F887 gets interrupted in whatever it is doing and jumps to the vector table to perform
the interrupt service routine.

II/ Experimental objectives:

On completion of this exercise, students should be able to:

- Become familiar with the operation and calculation of Timers

- Gain experience enabling peripheral interrupts

- Use the subroutine for LCD

III/ Equipments And Facilities

- Computer

- MPlab software using version 8.63, Protues software V7.8 or higher

- PICKit2, USB B Type cable. DC Power Supply

- Workbench

- Projector

IV/ Apparatus And Material Learnning

- Lab manual: Eastern International University

- Electronic tools, electronic devices.

V/ Preparation

1. Backround

- Micro processor and digital circuit


2. Preparation

Page 8
- The software must be installed.
- Required read lab manual at home.
VI/ Content: Programing for timers and interrupts

1. Timer0
- The Timer0 module can be used as either an 8-bit timer or an 8-bit counter with the
following feature

• 8-bit timer/counter register (TMR0)


• 8-bit prescaler (shared with Watchdog Timer)
• Programmable internal or external clock source
• Programmable external clock edge selection
• Interrupt on overflow

Figure 2.1 block diagram of the Timer0 module.

- The formula for Timer0:



Time delay = /

Or

- TMR0 = 256 – (Time Delay x Frequency)/(4*Prescaler)

Page 9
Example:

Chosing PS2 =PS0=0 and PS1 =1 to configure timer0 prescaler of 1:8. We need a way to
ensure that Timer 0 interrupts every 500Hz instructions or 2 millisecond at fosc 4MHz

TMR0 = 256 – (0.002 x 4000000)/(4*8) = 6.

2. Timer1
- The Timer1 is a 16-bit timer or counter with the following features:
• 16-bit timer/counter register pair (TMR1H:TMR1L)
• Programmable internal or external clock source
• 3-bit prescaler
• Optional LP oscillator
• Synchronous or asynchronous operation
• Timer1 gate (count enable) via comparator or 1 pin
• Interrupt on overflow
• Wake-up on overflow (external clock, Asynchronous mode only)
• Time base for the Capture/Compare function
• Special Event Trigger (with ECCP)
• Comparator output synchronization to Timer1 clock

Figure 2.2 block diagram of the Timer1 module.

Page 10
- The formula for Timer1:

Time delay = /
Or
- TMR1 = 65536 – (Time Delay x Frequency)/(4*Prescaler)
Example:

Chosing T1CKPS1 = T1CKPS0 =0 to configure timer0 prescaler of 1:1. We need a way to


ensure that Timer 1 interrupts every 100Hz instructions or 10 millisecond at fosc 4MHz

TMR1 = 65536 – (0.01 x 4000000)/(4*8) = 55536 = 0xD8F0.

3. Timer2

The Timer2 module is an eight-bit timer with the following features:


• 8-bit timer register (TMR2)
• 8-bit period register (PR2)
• Interrupt on TMR2 match with PR2
• Software programmable prescaler (1:1, 1:4, 1:16)
• Software programmable postscaler (1:1 to 1:16)

Figure 2.3 block diagram of the Timer2 module.


- The formula for Timer2:

PR2 = [(Time Delay x Frequency)/(4*(Prescaler*Postscaler)]-1

Example:

Chosing Postscaler is 1:1 and Prescaler is 1:4. We need a way to ensure that Timer 2
interrupts every 1KHz instructions or 1 millisecond at fosc 4MHz

PR2 = [(0.001x4000000)/(4x1x4)]-1 = 249.

Page 11
4. Interrupts

- The PIC 16F887 has multiple interrupt sources:

• External Interrupt RB0/INT


• Timer0 Overflow Interrupt
• PORTB Change Interrupts
• 2 Comparator Interrupts
• A/D Interrupt
• Timer1 Overflow Interrupt
• Timer2 Match Interrupt
• EEPROM Data Write Interrupt
• Fail-Safe Clock Monitor Interrupt
• Enhanced CCP Interrupt
• EUSART Receive and Transmit Interrupts
• Ultra Low-Power Wake-up Interrupt
• MSSP Interrupt

- Once an interrupt occurs (associated flag bit is set):

• The processor will finish its current instruction and the address of the next
instruction in the program counter will be stored onto the top of the stack.

• The program counter will then point to address 004h (interrupt vector) and begin
sequentially executing the code at that point.

Page 12
Figure 2.4 Interrupt Logic

5. External Interrupt

When a valid edge appears on the RB0/INT pin (set in OPTION REGISTER), the INTF
bit is set. This interrupt can be disabled by clearing the INTE control bit. The INTF bit must
be cleared in software in the Interrupt Service Routine before re-enabling this interrupt.

6. LCD Interfacing
- In recent years the LCD is finding widespread use replacing LEDs (seven-segment LEDs
or other multisegment LEDs). This section introduces the library for text LCD and the ways
to use these subroutine.
- The defination for subroutine of LCD:

Page 13
void lcd_init();
unsigned char lcd_busy();
unsigned char lcd_get_byte(unsigned char rs);
void lcd_put_byte(unsigned char a,unsigned char b);
void lcd_gotoxy(unsigned char col, unsigned char row);
void lcd_putc(char c);
void lcd_puts(const char* s);
- An easy example to display EIU on the first row and Students on the second row
void main()
{
IRCF2=1; // internal clock at 4Mhz
IRCF1=1;
IRCF0=0;
ANSEL = 0;ANSELH = 0;
lcd_init();
lcd_gotoxy(6, 0);
lcd_puts("EIU");
lcd_gotoxy(3, 1);
lcd_puts("Students");
while(1);
{
}
}
7. Lab Examples Code:

Lab Circuit: The schematic below is used for Lab-2 programming


VCC 1k
MCLR 1 U1 40 PGD RB7 RE0 220
RA0 2 MCLR/VPP/RE3 RB7/ICSPDAT 39 PGC RB6
RA1 3 RA0/AN0/ULPWU/C12IN0- RB6/ICSPCLK 38 RB5
RA2 4 RA1/IN1/C12IN1- RB5/AN13/T1G 37 RB4
RA3 5 RA2/AN2/VREF-/CVREF/C2IN+ RB4/AN11 36 RB3 VCC VCC
RA4 6 RA3/AN3/VREF+/C1IN+ RB3/AN9/PGM/C12IN2- 35 RB2
RA5 7 RA4/T0CKI/C1OUT RB2/AN8 34 RB1
RE0 8 RA5/AN4/SS/C2OUT RB1/AN10/C12IN3- 33 RB0 VCC
RE1 9 RE0/AN5 RB0/AN12/INT 32 R1 R2
VCC RE2 10 RE1/AN6 VDD1 31 4k7 4k7
11 RE2/AN7 VSS1 30 D7
12 VDD RD7/P1D 29 D6 RB0 RB1
RA7 13 VSS RD6/P1C 28 D5
RA6 14 RA7/OSC1/CLKIN RD5/P1B 27 D4
RC0 15 RA6/OSC2/CLKOUT RD4 26 RC7 RX
RC1 16 RC0/T1OSO/T1CK1 RC7/RX/DT 25 RC6 TX
RC2 17 RC1/T1OSI/CCP2 RC6/TX/CK 24 RC5
RC3
RD0
E
SCL
18
19
20
RC2/P1A/CCP1
RC3/SCK/SCL
RD0
RC5/SDO
RC4/SDI/SDA
RD3
23
22
21
RC4 SDA
RS
RW
EX-INTERRUPT
RD1 pic16f 887 RD2

CONTROLLER LCD CHARACTERS 16 * 2 LINES

J1
10
11
12
13
14
15

16
1
2
3

4
RW 5
RS 6

7
8
9

C3 1 MCLR VCC
D4
D5
D6
D7

RA6 2
E

3
PGD
R3 100

22P Y1 4
5 PGC
C2 RA7 20MHz 6 R4

20k
LCD VCC
22P CON6

PROGRAMMER VCC

Figure 2.5: Schematic of Timers and Interupts

Example 1:

Write an 16F887 C program to toggle bit RB0 continuously with the cycle is 2
milisecond using Timer0.
#include <htc.h>

Page 14
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);

void main()
{
IRCF2=1; // internal clock at 4Mhz
IRCF1=1;
IRCF0=0;
ANSEL = 0;ANSELH = 0;

TRISB0 = 0;

T0CS=0;
PSA = 0; // Internal instruction cycle clock (FOSC/4)
T0SE=1; // Increment on low-to-high transition on T0CKI pin
PS0=PS2= 0 ; // Prescale 1:8
PS1=1;
TMR0 = 6;

T0IE =1;
GIE = 1; //enable gloabal interrupts
while(1);
{
}
}
void interrupt timer()
{if(T0IF && T0IE){
T0IF = 0;
TMR0 = 6;
RB0^=1;
}
}

Example 2:

Write an 16F887 C program to toggle bit RB0 continuously with the cycle is 10
milisecond using Timer1.
#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);

void main()
{
IRCF2=1; // internal clock at 4Mhz
IRCF1=1;
IRCF0=0;
ANSEL = 0;ANSELH = 0;

TRISB0 = 0;

TMR1CS = 0; // internal osc


T1OSCEN = 0; // disable external osc
T1CKPS1 = 0; //
T1CKPS0 = 0; //presclaer = 1:1
TMR1GE = 0; //don't use Gate Control
T1SYNC = 0; //asynchoronize
TMR1IF = 0; //clear flag
TMR1H = 0xD8; // value for time
TMR1L = 0XF0; // (65536 - time)*4/fosc = 10ms => time = 55536 = D8F0
TMR1IE = 1; //enable timer t1

Page 15
TMR1ON = 1;

INTEDG = 0;
INTF = 0;
INTE = 1;
PEIE = 1; //enable interrupts peripral
GIE = 1; //enable gloabal interrupts
while(1);
{
}
}
void interrupt uart_isr()
{if(TMR1IF && TMR1IE){
TMR1IF = 0;
TMR1H = 0xD8;
TMR1L = 0xF0;
RB0^=1;
}
}

Example 3:

Write an 16F887 C program to toggle bit RB0 continuously with the cycle is 1
milisecond using Timer2.
#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);

void main()
{
IRCF2=1; // internal clock at 4Mhz
IRCF1=1;
IRCF0=0;
ANSEL = 0;ANSELH = 0;

TRISB0 = 0;

T2CON = 0x01; // prescaller is 4


TMR2ON = 1;
TMR2IE = 1;
PR2 = 249;

PEIE = 1; //enable interrupts peripral


GIE = 1; //enable gloabal interrupts
while(1);
{
}
}
void interrupt uart_isr()
{if(TMR2IF && TMR2IE){
TMR2IF = 0;
PR2 = 249;
RB0^=1;
}
}

Example 4:

Write an 16F887 C program to increase imediately the value when the button at RB0 is

Page 16
pressed
#include <htc.h>
#include "lcd.h"
#include <stdio.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);

int count=0;
void main()
{
IRCF2=1; // internal clock at 4Mhz
IRCF1=1;
IRCF0=0;
ANSEL = 0;ANSELH = 0;

TRISB0 =1; // input configure

RBPU = 0;
WPUB = 0x01;
INTEDG = 1; // rising edge
INTF = 0;
INTE = 1;
GIE = 1;

lcd_init();
lcd_gotoxy(1, 0);
lcd_puts("Ex - Interrupt ");
while(1){
lcd_gotoxy(0, 1);
printf("Count Value:%d",count);
}
}
void interrupt isr()
{
if(INTE && INTF){
INTF = 0;
count++;
if(count==5) {count=0;}
}
}
void putch(char c)
{
lcd_putc(c);
}

8. Practices:

Exercise 1: Write an 16F887 C program to toggle bit RE0 continuously with the cycle is 3
milisecond using Timer0.

Exercise 2: Write an 16F887 C program to toggle bit RE0 continuously with the cycle is 5
milisecond using Timer1.

Exercise 3: Write an 16F887 C program to toggle bit RE0 continuously with the cycle is 7
milisecond using Timer2.

Page 17
Exercise 4: Write an 16F887 C program to control up or down imediately value when the
button at RB0 or RB1 is pressed

Exercise 5: Write an 16F887 C program to display the Time clock on LCD

Page 18

You might also like