Professional Documents
Culture Documents
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.
- Computer
- Workbench
- Projector
V/ Preparation
1. Backround
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
Or
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
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
Page 10
- The formula for Timer1:
∗
Time delay = /
Or
- TMR1 = 65536 – (Time Delay x Frequency)/(4*Prescaler)
Example:
3. Timer2
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
Page 11
4. Interrupts
• 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:
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
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;
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;
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;
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
Page 18