You are on page 1of 20

HCS12 Timer Control

Why are Timer Functions Important?


• Many useful applications are difficult to implement without a dedicated timer function.
- Examples:
- Precise time delay creation and measurement
- Period and pulse width measurement
- Frequency measurement
- Event counting
- Arrival time comparison
- Periodic interrupt generation
- Waveform generation
These applications can be done by the timer’s circuit without the direct involvement of
the CPU
Start / Stop Counting
Timer Overflow Interrupt Enable Bit
Timer Interrupt Flag
Expt. 4a – Timer as a Counting Device
Aim – To activate the LEDs in the Project Board whenever the timer
count overflows.
Pin Assignment:
LEDs – Project Board – PortA
To activate the LEDs on the Project Board – Pin 4 in Port P must be
enabled
Program Template

void main(){
/* variable declarations */
/* Data Direction Register Declarations */
TSCR1 = ? /* Enable start couning */
TSCR2 = ? /* No interrupt and set the timer clock frequency as 2MHz */
Infinite loop{
TFLG2 = ? /* Set the timer overflow flag*/
check for Timer overflow condition
increment the count;
Pass the count to LEDS;
}
}
Expt :4b – Digital Clock
Aim – To implement a digital clock using the timer block of HCS12 and
display the time in LCD.
Program Template
void main(){
Initialize LCD and SPI functions
variable declarations
Interface LEDs on PortB by configuring DDR
Initially turn off the LED
TSCR1 =?
TSCR2 = ?
TFLG2 = ?
for (;;){
check for Timer Overflow Flag{ /*till bit 7 of TFLG2 becomes set
increment the counter
TFLG2 = ? /* Clear the Flag
}
If(count == certain value){
increment seconds;
reset count
Toggle the statusof LED
}
if (seconds reached the maximum value){
increment minutes;
reset seconds
}
If(minutes reached the maximum value){
Increment hour;
Minutes zero;
}
Call the display_integer function /* to display hour
Move the cursor position
Call the LCD_send character function /* to display :
Call the display_integer function /* to display hour
Move the cursor position
Call the LCD_send character function /* to display :
Call the display_integer function /* to display seconds
}
}
Real Time Interrupt
• Used to: Keep track of time
• Used to: Initiate tasks on a periodic basis
• When a timeout occurs:
1. An interrupt is generated
2. The CPU vectors to location $FFF0 in order to load the Program
Counter (PC) with the address that points to the interrupt service
routine.
3. The CPU clears the interrupt request.
4. The CPU returns to the main program.
Real Time Interrupt Control Status Register (RTICTL)

An RTR[6:4] is a Real-Time Interrupt Prescale Rate Select, and an


RTR[3:0] is a Real-Time Interrupt Modulus Counter Select. The
maximum time-out can be set by writing the Real Time Rate bit field
with hex $7F. This will divide the input clock by 2^16 and then by 16.
RTI Frequency =
Bus Clock / RTICTL
Clock and Reset Generator Interrupt Enable
Register (CRGINT)
• In the Clock and Reset Generation Interrupt Enable (CRGINT) register,
the RTI has an interrupt enable (Bit 7), which is the Real-Time
Interrupt Enable (RTIE) bit.
• Setting this bit enables the RTI, Else disables it.
• In this case, “0” means that the interrupt is disabled, and “1” means
that the interrupt is enabled.
CRG FLG Register

• In the CRG Flag (CRGFLG) register, the RTI timer has a flag bit called
Real-Time Interrupt Flag (RTIF) that will set whenever the timer times
out.
• Whenever this flag is set and its interrupt enable bit is also set, the
RTI timer signals an interrupt request to the CPU. The CPU must clear
the RTIF by writing it with a logic of “1.”
• The RTIF bit is automatically set to “1” at the end of every RTI period.
This flag can only be cleared by writing a “1.”
Expt.4c – Real Time Interrupt
Aim – To toggle the status of buzzer continuously and to toggle the
status of Pin5 of PortB whenever a real time interrupt is generated for
every 2 seconds.
Program Template
#pragma CODE_SEG NON_BANKED
Declare variable declarations
void main(void){
Declare DDR to enable the LEDs on the Application Module and Buzzer
CRGINT = ?
RTICTL = ? /* maximum time-out
You activate the LED connected to Pin 5 on PortB
EnableInterrupts;
infiniteloop{
toggle the state of buzzer continuously
}
}
Call ISR
Interrupt (((0x10000 – Vrti/2)-1) void RTI_ISR(void){
increment the count;
check for 2 secs
Toggle the state of Pin5 of PortB
Reset the count;
}
Clear the RTIF bit in CRGFLG register
}
Define delay function

You might also like