HCS12 Timer Control
HCS12 Timer Control
HCS12 Timer Control
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)
• 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