Professional Documents
Culture Documents
Real-Time Clock
The device is not a clock!
It does not tell time!
It has nothing to do with actual or real-time!
information correctly!
Example
If clock interval is 1 second, a counter within the computer can be set
equal to time, one could easily program a routine to give the time on a
12 or 24-hour basis
Line Clock
Simple form of Real-Time Clock
Use 60Hz from line to generate 1/60 second clock
Half-Wave rectifier
60hz
60 Hz Pulse
XTAL
OSC
Divide
by 10
Divide
by 10
Divide
by 10
Divide
by 10
10ms.
Divide
by 10
.1s 1s.
Multiplexer
Interrupt to CPU
Or connect to I/P
Programmable Timer/Counter
Clock
Overflow
output
Programmable Timer/Counter
Example:
8 bit Up Counter (0-255)
clock input =1MHz or 1us.
wanted to interrupt the CPU after 200us.
If one load the counter with (256-200) = 56,
after 200 clocks ----> output =1 on overflow!
Therefore cpu can be interrupted after 200us.
Programmable Timer/Counter
Typically
Timer:---> known clock input (Internal)
Counter: --> unknown clock (External)
TIMERs/COUNTERs
PIC16F877
Timer0
Timer1
Timer2
Timer0 Module
8-bit timer/counter
Readable and writable
8-bit software programmable prescaler
Internal or external clock select
Interrupt on overflow from FFh to 00h
Edge select for external clock
(Lab Assignment)
* Fast turning will set the Minutes
* Slow turning will set the seconds
Recall
Rotary Encoder: Example
Phase Difference
One cycle
ON/OFF
determines CCW/CW
CpE 112 : Klinkhachorn
Recall
INT_EXT Interrupt : Example
Main Program
.
.
.
.
enable_interrupts(int_EXT);
enable_interrupts(global);
.
.
.
Recall
INT_EXT Interrupt : Example
#int_ext
EXT_INT_ISR()
{
// one interrupt per cycle
// determine direction by reading the another bit
// do what is necessary
// exit
}
Note: CCS will reset INTE flag and re-enable GIE.
These do not applied to #int_Global!
(continued)
Assume
Rotary Encoder generates 32 pulse per revolution
One of the outputs of the encoder connects to
Rb0/INT
Maximum rate of turning is one turn in 0.5
second.
i.e. Generates interrupt up to 32*1/2 = 64 times a
second or every 1/64 = 15.625 milliseconds.
(continued)
Assume
Timer0 will be used to detect Fast/Slow turning
Set prescaler to divide input freq. by 256 (or input
period *256)
Use internal clock (4MHz/4 or 1us)
The TMR0 will increment every 1* 256 = 256us or about 1/4
of ms.
TMR0 will count through its 256 counts in 256*256 or about
66 ms.
(continued)
#int_ext
EXT_INT_ISR()
{
// one interrupt per cycle
// determine direction by reading the another bit
// clears INTF
// checks the T0IF flag to see if TMR0 has gone through 256
// counts since the last RB0/INT.
//
if so ----> increment/decrement second
//
else ----> increment/decrement minute
// clear TMR0 and T0IF
// exit
}
CpE 112 : Klinkhachorn
Timer1 Module
16-bit timer/counter (TMR1H,TMR1L)
Readable and writable (both)
Internal or external clock select
Interrupt on overflow from FFFFh to 0000h
Reset from CCP module trigger
Programmable Prescaler (1,2,4, and 8)
Sync and Asyn Counter mode
CpE 112 : Klinkhachorn
Timer1Block diagram
Timer1 Oscillator
Low power Oscillator rated upto 200kHz
Primary intended for a 32kHz
Timer1 - TMR1H:TMR1L
The register pair (TMR1H:TMR1L)
4MHz clock
maximum supply current is 4mA
Maximum supply current on sleep mode is 42uA
If CpU is asleep roughly 15.99 S out of 16.00 S
Then the average current is
42(15.99/16)+4000(.01/16) = 44.5 uA!
Timer2 Module
8-bit timer (TMR2)
8-bit period register (PR2)
Readable and writable (both)
Interrupt on TMR2 match of PR2
Programmable Prescaler (1,4, and 16)
Programmable postscaler (1 to 16)
Can be use as the PWM time-base for PWM mode of the
CCP module
SSP module optional use of TMR2 output to generate clock
shift
CpE 112 : Klinkhachorn
Timer2 - TMR2
The prescaler and postscaler counters are
CAPTURE/COMPARE/PWM MODULES
PIC16f877 has two Capture/Compare/PWM
can operate as a:
16-bit Capture register
16-bit Compare register
PWM master/slave Duty Cycle register
CCP MODULES
Both the CCP1 and CCP2 modules are identical in
CCP1 MODULE
Capture/Compare/PWM Register1
CCPxCON Registers
CCP1CON REGISTER/CCP2CON REGISTER (ADDRESS: 17h/1dh)
CAPTURE MODE
In Capture mode, CCPR1H:CCPR1L captures the 16-bit
CAPTURE MODE
Start, read
counts from
CCPRx
(Init_Count)
T=?
.
If (Period_Measu_done)
repot_period;
.
.
loop
CCPx ISR
.
.
// If first interrupt {
// save_CCPxR into Init_Count;
// exit ;}
// else {
// calculate the period;
// set Period_Measu_done flag;
// disable CCPxIE;
// exit; }
.
.
Compare Mode
In Compare mode, the 16-bit CCPR1 register value is
CCP1M3:CCP1M0 (CCP1CON<3:0>)
At the same time, interrupt flag bit CCP1IF is set.
Compare Mode
CCPxCON Registers
CCP1CON REGISTER/CCP2CON REGISTER (ADDRESS: 17h/1dh)
T=?
65536
CCP2 operates in
Compare mode
(software interrupt),
interrupts every 65536
clocks. Update
CYCLES
500 clocks
500 clocks
On interrupt,
toggle bit 0 of CCPxCON
add 500 to CCPRx
reset CCPxIF
* Assume
- PIC run at 4 MHz or
1uS instruction cycle
- Timer1 is on and the
input is from the
internal clock
PWM Period
The PWM period is specified by writing to the PR2
register
The PWM period can be calculated using the following
formula:
PWM period = [(PR2) + 1] 4 TOSC (TMR2 prescale value)
PWM Resolution
Maximum PWM resolution (bits) for a