You are on page 1of 18

TIMERS

TIMER1 (16 BIT)

16-bit timer/counter register pair;


Programmable internal or external clock source;
3-bit prescaler;
Optional LP oscillator;
Synchronous or asynchronous operation;
Timer TMR1 gate control (count enable) via
comparator or T1G pin;
Interrupt on overflow;
Wake-up on overflow (external clock); and
Time base for Capture/Compare function.
TIMER1 CONFIGURATION REGISTER

Prescaler
T1GINV - Timer1 Gate Invert bit T1CKPS1 T1CKPS0
Rate
TMR1GE - Timer1 Gate Enable bit 0 0 1:1
T1CKPS1, T1CKPS0 – Determine Rate of prescaler 0 1 1:2
1 0 1:4
T1OSCEN - LP Oscillator Enable Control bit
1 1 1:8
T1SYNC - Timer1 External Clock Input Synch bit
TMR1CS - Timer TMR1 Clock Source Select bit
TMR1ON - Timer1 On bit
OPERATION OF TIMER1
TIMER1 İN TIMER MODE
TMR1CS = 0
TIMER1 in COUNTER MODE
T1OSCEN = 0 TMR1CS = 1
TIMER1 İN LP OSCILLATOR MODE
T1OSCEN = 1 TMR1CS = 1
FLASHING LIGHTS ON EACH TIMER1 OVERFLOW

U1
1 15
RE3/MCLR/VPP RC0/T1OSO/T1CKI
16
RC1/T1OSI/CCP2

D
A

B
2 17
RA0/AN0/ULPWU/C12IN0- RC2/P1A/CCP1
3 18
RA1/AN1/C12IN1- RC3/SCK/SCL
4 23
RA2/AN2/VREF-/CVREF/C2IN+ RC4/SDI/SDA
5 24
RA3/AN3/VREF+/C1IN+ RC5/SDO
6 25
RA4/T0CKI/C1OUT RC6/TX/CK
7 26
RA5/AN4/SS/C2OUT RC7/RX/DT
14
RA6/OSC2/CLKOUT
13 19
RA7/OSC1/CLKIN RD0
R1 20
RD1
33 21
RB0/AN12/INT RD2
330 34 22
RB1/AN10/C12IN3- RD3
35 27
RB2/AN8 RD4
R2 36 28
RB3/AN9/PGM/C12IN2- RD5/P1B
LED-BLUE D1 37
RB4/AN11 RD6/P1C
29
330 38 30
RB5/AN13/T1G RD7/P1D
39
RB6/ICSPCLK
D2 40
RB7/ICSPDAT RE0/AN5
8
LED-RED 9
RE1/AN6
10
RE2/AN7
PIC16F887
FLASHING LIGHTS ON EACH TIMER1 OVERFLOW

void main() {
ANSEL = 0; // All I/O pins are configured as digital
ANSELH = 0;
TRISB = 0; // Port B pins are configured as outputs
PORTB = 0x10; // Initially, Blue Light is turned ON
GIE_bit=PEIE_bit=1; // Enable interrupt (bits GIE and PEIE)
TMR1IE_bit= 1; // Enable interrupt on Timer1 overflow
TMR1IF_bit=0; // Reset bit TMR1IF
T1CKPS1_bit = 1; //Prescaler is set to 1:8
T1CKPS0_bit= 1;
TMR1ON_bit = 1; // Start TMR1
TMR1H = 0x00; // Set initial value for timer TMR1
FLASHING LIGHTS ON EACH TIMER1 OVERFLOW
while (1){
// Endless loop
}}
void interrupt() {
TMR1IF_bit=0; // Reset bit TMR1IF
RB0_bit= ~RB0_bit; // Lights are reversed on each interrupt
RB4_bit= ~RB4_bit;
TMR1H = 0x00; // Set initial value for timer TMR1
TMR1L = 0x00;
}
ULTRASONIC DISTANCE SENSOR

LCD1
LM016L
R2

VDD
VSS

VEE

RW
RS
10k

D0
D1
D2
D3
D4
D5
D6
D7
E
RV1 SONAR1 U1

1
2
3

4
5
6

7
8
9
10
11
12
13
14
ULTRASONIC SENSOR 1 15
RE3/MCLR/VPP RC0/T1OSO/T1CKI
16
www.TheEngineeringProjects.com
RC1/T1OSI/CCP2
2 17
2%

TestPin RA0/AN0/ULPWU/C12IN0- RC2/P1A/CCP1


3 18
RA1/AN1/C12IN1- RC3/SCK/SCL
4 23
RA2/AN2/VREF-/CVREF/C2IN+ RC4/SDI/SDA
5 24
1k RA3/AN3/VREF+/C1IN+ RC5/SDO
6 25
RA4/T0CKI/C1OUT RC6/TX/CK
7 26
RA5/AN4/SS/C2OUT RC7/RX/DT
14
Trigger

RA6/OSC2/CLKOUT
13 19
Echo
Gnd

RA7/OSC1/CLKIN RD0
+5V

20
RD1
33 21
RB0/AN12/INT RD2
34 22
RB1/AN10/C12IN3- RD3
35 27
RB2/AN8 RD4
36 28
RB3/AN9/PGM/C12IN2- RD5/P1B
37 29 R1
RB4/AN11 RD6/P1C
38 30
RB5/AN13/T1G RD7/P1D
39 330
RB6/ICSPCLK
40 8
RB7/ICSPDAT RE0/AN5
9
RE1/AN6
10
RE2/AN7
B

A
D

PIC16F887
D1
LED-BLUE
void main() {
ANSEL = 0x00; //All pins are digital
ANSELH = 0x00;
T1GSS_bit = 1; //T1G(RB5) input is used for gate control
TRISB=0x21; //B5/T1G and B0 are input to detect ECHO Pulse
GIE_bit=1; //Global interrupt is enabled
INTE_bit=1; //RB0/INT is enabled to detect the ECHO Pulse at T1G
INTF_bit=0; //INTF flag is reset
TRISD=0x00; // All PortD pins are outputs to drive the LCD and buzzer
PORTD =0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Distance ");
T1CON = 0xC1; //Start Timer1 and set TMR1GE and T1GINV to enable T1G
TMR1L=0x00; //Timer is reset to start measuring the period
while(1){
RD6_bit = 0; //Reset RD6 before generating the triggering pulse
RD6_bit=1; //Send a trigger pulse to the TRIG input of the sensor
Delay_us(10); // pulse period is 10 us
RD6_bit= 0;
period = TMR1L | (TMR1H<<8); // TMR1 is saved by combining L & H bytes
distance = period/58 ; // Distance target is obtained using the pulse period
intToStr(distance, txt);
Lcd_Out(1,11,txt);
if(distance < 50)alarm(); //Alarm is triggered if distance < 50 cm
Delay_ms(1000);
}}

* Fosc is assumed to be 4 MHz. Hence the Timer pulse period is 1 us,


Vsound =343m/s
distance = Vsound x period/2
= 34300 cm/s x (period us/106)/2
= period/58
ALARM & INTERRUPT FUNCTIONS
void alarm() {
RD1_bit=1; //Turn ON buzzer
delay_ms(200);
RD1_bit=0; //Turn OFF buzzer
delay_ms(200);
}

void interrupt() {
INTF_bit=0; //Reset RB0/INT flag
TMR1L=TMR1H=0; //TMR1 is reset at each rising edge of ECHO
pulse
}
TIMER2
TIMER2 CONFIGURATION SFR

TOUTPS3 - TOUTPS0 - Timer2 Output Postcaler Select bits

TMR2ON - Timer2 On bit

T2CKPS1, T2CKPS0 - Timer2 Clock Prescale bits


0 0 1:1
0 1 1:4
1 x 1:16
Timer2 in PWM Mode

You might also like