Professional Documents
Culture Documents
After each count up to 255, TMR0 resets its value to zero and starts with a new cycle of counting to 255. During each transition from 255 to zero, T0IF bit in INTCOM register is set If interrupts are allowed to occur, this can be taken advantage of in generating interrupts and in processing interrupt routine It is up to programmer to reset T0IF bit in interrupt routine, so that new interrupt, or new overflow could be detected
3
TMR0 Timing
TMR0 is readable and writable
MOVF TMR0,0 MOVWF TMR0
If TMR0 register is written, the increment is inhibited for the following two cycles
Exercise
Write a program to sent a 0xFF to Port B after every 1s. Assume that you have a 250Hz clock at RA4/TOCKI pin.
Prescaler
The 8-bit Prescaler divides oscillator clock before it will reach logic that increases timer status The highest divisor is 256. This actually means that only at every 256th clock, timer value would increase by one For example, if the TMR0 prescaler is set to divide-by-4 and the PIC is running at 4 MHz, the prescaler will send a 250 KHz clock to the TMR0 register.
9
PS2:PS0
The prescaler is not readable or writable. The prescale value is defined through first three bits in OPTION register
10
Prescaler Assignment
The prescaler is shared between TMR0 and WDT It is mutually exclusive between TMR0 and WDT
A prescaler assignment for TMR0 means that there is no prescaler for WDT, and vice-versa
11
Clearing Prescaler
When assigned to TMR0, all instructions writing to TMR0 (e.g., CLRF 1, MOVWF 1, BSF 1,x ....etc.) will clear the prescaler. When assigned to WDT, a CLRWDT instruction will clear the prescaler along with WDT
12
TMR0 Example
Example showing how to initialize timer to signal falling edges from external clock source with a prescaler 1:4.
14
TMR0 Interrupt
TMR0 interrupt is generated when the TMR0 register overflows from 0xff to 0x00 This overflow sets the T0IF bit (INTCON<2>). The interrupt can be masked by clearing enable bit T0IE (INTCON<5>). The T0IF bit must be cleared by the TMR0 interrupt service routine before re-enabling this interrupt. TMR0 interrupt cannot wake the processor up from SLEEP since the timer is shut off during SLEEP.
15
Example
Example showing how to initialize timer to signal falling edges from external clock source with a prescaler 1:4
16
Interrupts
17
Polling
There are 2 main ways for external I/O devices to communicate with the ucontroller Polling and Interrupt In polling, the ucontroller periodically query input devices to see if data is available
The ucontroller first stops processing its main program and enters the polling routine The I/O device is then queried for data. If there is data, the service routine is executed When the device has been serviced, the ucontroller resumes its main program
18
Polling
Polling can effectively assign priorities among input devices by querying the highest priority device first
19
Exercise
Given that 3 devices (A, B and C) are connected to RB0, RB1 and RB2, write a program to poll the devices. If any of the devices is activated, call the XYZ subroutine. The priority for the devices are A, B then C.
20
Interrupts
For the interrupt method, I/O devices issue a signal (interrupt) to the ucontroller when the device needs to be serviced The interrupt request can come at any time during the execution of a program The interrupt forces a call to the service subroutine which is usually referred to as an interrupt service routine On interrupt, the processor saves the return address on the stack and program control is redirected to the interrupt service routine
21
Interrupts
An interrupt in its simplest form is like a hardware triggered subroutine.
Main Program 16F84 INT
Interrupt
retfie
22
25
26
Sources of Interrupts
PIC16F84 has 4 sources of interrupt:
External interrupt RB0/INT pin Timer overflow interrupt Port B interrupt on change (pins RB7:RB4) EEPROM write complete interrupt
These interrupts are disabled/enabled through the interrupt control register (INTCON)
27
Any of these may be used alone, or several sources may be enabled, depending on your application. At any time in the program, the user may turn any of the sources off by clearing these mask bits;
BCF INTCON, INTE BCF INTCON, RBIE BCF INTCON, TOIE
76
30
31
Interrupt Procedure
When an interrupt occurs:
GIE bit is cleared to disable any further interrupt. Return address is pushed onto the stack. Program counter is loaded with 0x0004 Once in the interrupt service routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits The GIE bit is again set on execution of the return from interrupts (RETFIE)
32
Interrupt Procedure
The occurrence of an interrupt event sets the flag bit (if GIE and the mask bit are set) It is the user's responsibility to clear the flag bit within the service routine
BCF INTCON, INTF ; clear external INT flag BCF INTCON, RBIF ; clear RB4 - RB7 interrupt flag BCF INTCON, TOIF ; clear timeout flag
Failure to clear the flag bit is interpreted by the processor as "a previous interrupt occurred, but the user has yet to service the interrupt". Thus, although an interrupt event may occur, GIE may be enabled and a mask bit may be set, the actual interrupt will not occur unless the corresponding flag bit is clear
33
Interrupt Example
79
34
INT Interrupts
The INT interrupt is an external interrupt on RB0/INT pin It is edge triggered: either rising if INTEDG bit (OPTION<6>) is set or falling if INTEDG is clear When a valid edge appears on the RB0/INT pin the INTF bit (INTCON<1>) is set This interrupt can be disabled by clearing control bit INTE (INTCON<4>) The INT interrupt can wake the processor from SLEEP only if the INTE bit was set prior to going into SLEEP.
35
Exercise
Write a program to count the number of positive transitions on input RB0/INT. Display the last two bits of the count on 2 LEDS connected to RB6 and RB7.
36
Example: Cont.
INT_SERV: INCF COUNTER, F BTFSS COUNTER, 0 BCF PORTB, 6 BTFSC COUNTER, 0 BSF PORTB, 6 BTFSS COUNTER, 1 BCF PORTB, 7 BTFSC COUNTER, 1 BSF PORTB, 7 BCF INTCON, INTF ; clear the appropriate flag RETFIE ; this also set global interrupt enable END
37
TMR0 Interrupts
The TMR0 interrupt is triggered by an overflow (0xff to 0x00) in TMR0 The overflow sets the flag bit TOIF (INTCON<2>) The interrupt can be enabled/disabled by setting/clearing enable bit TOIE (INTCON<5>).
38
Exercise
Using interrupt, write a program to sent 0xFF to Port B after approximately 1s. Assume that you have a 1KHz clock at RA4/TOCKI pin.
39
PORT RB Interrupts
PORT RB Interrupt is triggered by input changes on portb<7:4> The interrupt sets the flag bit RBIF (INTCON<0>) The interrupt can be enabled/disabled by setting/clearing enable bit RBIE (INTCON<3>). This interrupt is useful for a keypad interface.
40