Sunteți pe pagina 1din 47

UNIT II INTERRUPTS AND TIMER

PIC micro controller Interrupts- External Interrupts-Interrupt


Programming–Loop time subroutine - Timers-Timer
Programming– Front panel I/O-Soft Keys– State machines and
key switches– Display of Constant and Variable strings.

1
• PIC16C7X have 3 timer modules
1 2 3

Timer 0 Timer 1 Timer 2


Interrupt
Timer0 Module
Over Flow Interrupt
Timer1 Module
Over Flow Interrupt
Timer2 Module
Over Flow

2
• Pre-scaler and Post-Scaler:
We are using timer 0 at 4Mhz
One increment of Timer 0 in 8 bit mode will take
4 Clock Cycles = 1 µ Sec
Pre-scaler in (1:1)
If Pre-scaler changed to (1:2)
4 Clock Cycles = 2 µ Sec
If Pre-scaler changed to (1:8)
4 Clock Cycles = 8 µ Sec

Pre-scaler is for Timers


3
Post – Scaler is between Counter Overflow
‘TMRxIF ‘bit

If Post-scaler (2:1)
PIC takes 2 Overflow of counter to set Interrupt bit.

If Post-scaler (4:1)
PIC takes 4 Overflow of counter to set Interrupt bit.

If Post-scaler (8:1)
PIC takes 8 Overflow of counter to set Interrupt bit.

Post-scalar is for Counter


4
Timer 0 Timer 0 (TMR0) 8-bit timer/counter
with 8-bit prescaler
clock source - internal clock or
external clock.
external clock - increment on
either the rising or falling edge.
programmable pre scaler
option

5
6
Option Register: R/W Register

7
-ve
+ve

8
Additional Point to be remembers for timer
• Prescaler is assigned - (timer/counter),
TMR0 register = Written, then prescaler = 0
• Prescaler assigned - WDT,
CLRWDT, clear “prescaler and WDT”
• TMR0 register - timer, start two instruction cycles
delay.
• Sleep mode, oscillator = off. Overflow cannot
occur no pulses to count. TMR0 overflow
interrupt cannot wake up the processor from
Sleep mode.
• GIE and TMR0IE bits of the INTCON register
should be set.
9
Timer 1 Timer 1 (TMR1)
16-bit timer/counter

overflow occurs, an interrupt is generated


TMR1 increments from 0000h to FFFFh and rolls over to
000h
TMR1 operating mode is determined by the clock select
bit, TMR1CS
TMR1 module can operate in 2 basic modes-
(i). a timer (ii).a counter 10
Timer 1 Timer 1 (TMR1)
16-bit timer/counter

TMR1 module can operate in 2 basic modes-


(i). a timer – increments every instruction cycle.
(ii).a counter – increments on every rising edge of external
clock i/p.

11
12
T1SYNC = 1
External
clock input is not
Timer 1
synchronized.
Operation
Asynchronized
Timer Mode Counter Mode

Synchronized
T1SYNC = 0 Counter Mode
External clock input is
synchronized with What happens now in Sleep mode ?
internal phase clocks.
13
Oscillator does not depend on internal clocking
TMR1 Prescaler:
Operate even in sleep mode Prescaler - 1, 2, 4 or 8 divisions
Enabled T1OSCEN, T1CON register of the clock input.
Programmer must provide a software time delay - not directly R/W.
Prescaler
to ensure proper oscillator start-up

TMR1 Oscillator:
RC0/T1OSO & RC1/T1OSI - get pulses
peripheral electronics.
RC0 &TMR1
RC1 -Gate:
simultaneously configured for
Sourcequartz
Low Power is software configurable
oscillator.
gate allows the timer to directly
time external events

14
Timer 2 Timer 2 (TMR2)
8-bit timer with Prescaler and Postscaler

TMR2 can be used as – PWM


TMR2 register is R/W – cleared on any device reset.
Timer2 module has an 8-bit period register PR2
Timer2 increments from 00h till reaches PR2
PR2 is initialized to FFh upon reset.

15
16
Interrupts

Polling
External
System
Interrupts

Software Hardware
Interrupt Interrupt
Request from software Request from external
17
Hardware device
Hardware Interrupts

IE = 1
0

Maskable
Non Interrupts
Maskable Interrupts

PIC has one vectored interrupt location (i.e., 0004H)


12 interrupt sources
No interrupt priority
Only one interrupt is served at a time
Interrupts can be masked. 18
PIC16C7X family has up to 12 sources of interrupt External
• Timer 0
• Timer 1
• RB Port Change
• Parallel Slave Port Read/Write
• A/D Converter
• USART Receive
• USART Transmit
• Synchronous Serial Port
• CCP1 (Capture, Compare, PWM)
• CCP2 (Capture, Compare, PWM)
• TMR2 to PR2 Match

19
20
5 registers that used to control the operation of
Interrupts

• INTCON
• PIE
• PIR

21
Interrupt Registers
INTCON Register

22
INTCON Register

23
INTCON Register

pins 4, 5, 6 and 7 of port B 24


GIE = 0 GIE = 1

“return from interrupt” instruction, RETFIE,


exits the interrupt routine
sets the GIE bit,re-enables interrupts.

25
RETFIE

GIE = 0 GIE = 1

26
Interrupt Registers

27
Interrupt Registers

28
Interrupt Registers

29
30
org 0x00
goto start

start: movlw 0x02


movwf DAT1H
movlw 0x82
movwf DAT1L

movlw 0x07
movwf DAT2H
movlw 0xa0
movwf DAT2L

main: call add16

loop: goto loop 31


add16: clrf RESH
movf DAT1L, W
addwf DAT2L, W
movwf RESL
btfsc STATUS, C
incf RESH, F

movf DAT1H, W
addwf DAT2H, W
addwf RESH, W
movwf RESH
return

end
32
; Interrupt
; RB0 - external interrupt
#INCLUDE "p16f877.inc“
org 0x00
goto main
org 0x04
goto isr
org 0x100
main: bsf STATUS, RP0
bcf STATUS, RP1
movlw 0x7f ; pull up
movwf OPTION_REG
movlw 0x00
movwf TRISC
movlw 0x01
movwf TRISB
movlw 0x90
movwf INTCON
bcf STATUS, RP0
movlw 0xff
movwf PORTC
33
xx: goto xx
isr: movlw 0x00
movwf PORTC
bcf INTCON, INTF
call delay
movlw 0xff
movwf PORTC
bsf INTCON, GIE
RETFIE
delay: movlw .500
movwf 0x21
d1: movlw 0xff
movwf 0x22
d2: nop
decfsz 0x22,1
goto d2
decfsz 0x21,1
goto d1
return
end
34
movlw 0x00
movwf PORTC
bcf INTCON, INTF
call delay
movlw 0xff
movwf PORTC
bsf INTCON, GIE
RETFIE
delay: movlw .500
movwf 0x21
d1: movlw 0xff
movwf 0x22
d2: nop
decfsz 0x22,1
goto d2
decfsz 0x21,1
goto d1
return
end 35
Front Panel I/O Soft keys

36
37
Front Panel I/O Soft keys
• LCD Alphanumeric Display
• Key Switches
• Rotary Pulse Generator (RPG)

38
• ROTARY PULSE GENERATORS (RPG’S) TRANSFORM SHAFT
ROTATION

TO COUNT PULSE TRAINS FOR COUNTERS,


TACHOMETERS
MOTION MONITORS & CONTROLS

39
40
State Machines and Key Switches

Key switches - Debounc


State
not changed e
Variable
very fast called
Keybounce KEYSTATE
time = 10ms

Wait for Key Which Key


release State Pressed

Action

41
42
Display Plus RPG USE

43
44
45
Display of Variable String

String Variable, VSTRING


Cursor Positioning
ASCII String of Character
End of String designation,
H “00”
RS=0; send the first
character to display.
RS=1; subsequent
character to display.
H’00’ - stop

46
47

S-ar putea să vă placă și