0% found this document useful (0 votes)
3 views

ESL Exp 4

The document outlines three experiments involving timer functions in an HCS12 microcontroller, including activating LEDs on overflow, implementing a digital clock, and using real-time interrupts to toggle a buzzer and LED. Each experiment includes software and hardware requirements, theoretical background, program code, and an algorithm. The results confirm the successful implementation of each task as intended.

Uploaded by

Nandhini P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ESL Exp 4

The document outlines three experiments involving timer functions in an HCS12 microcontroller, including activating LEDs on overflow, implementing a digital clock, and using real-time interrupts to toggle a buzzer and LED. Each experiment includes software and hardware requirements, theoretical background, program code, and an algorithm. The results confirm the successful implementation of each task as intended.

Uploaded by

Nandhini P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Ex.

No: 4a Date:

TIMER AS A COUNTING DEVICE

Aim : To activate the LEDs in the Project Board whenever the timer count overflows.

Software Requirements:

Freescale code warrior development studio version 5.

Hardware Requirements:

i. Personal Computer
ii. MC9S12 freescale application module
iii. Interface card
iv. Project Board PBMCUSLK

Theory:

Many useful applications are difficult to implement without a dedicated timer function.

Some of the examples of timer is as follows:-

- 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.

4-1
Program:
#include <hidef.h>
#include “derivative.h”
void delay (void){
int i,j;
for(i=0;i<100;i++){
for(j=0;j<100;j++){
}
}
}
void main(void){
unsigned char count;
DDRA=0XFF;
DDRP=0X10;
TSCR1=0X80;
TSCR2=0X01;
for(;;){
TFLG2=0X80;
while(!(TFLG2 & 0X80)){
count=count+0X01;
PORTA=count;
delay();
}
}
}

4-2
Algorithm:

1. Start
2. Declare PORTA as output.
3. Declare the 4th pin of PORTP as output.
4. Assign the control word for TSCR1 as 0X80.
5. Assign the control word for TSCR2 as 0X01.
6. Inside the infinite for loop assign the control word for TFLG2 as 0X80.
7. While TFLG2 is not set, increment the count and assign the count value to PORTA.
8. Call the delay function.
9. Stop

TSCR1 is used for setting and clearing the Timer Enable bit (TEN) bit7 of TSCR1 which when
enabled starts and stop counting of the TCNT register. It does not enable/disable interrupts but
when the counter stops counting, the interrupts stop automatically because it will not roll from
FFFF to 0000.

4-3
4-4
Timer System Control Register 2 (TSCR2)

TOI enables/disables interrupts, but it does not start / stop counting. When interrupt is disabled,
the timer counts but an interrupt is not generated when it rolls from FFFF to 0000.

Timer Interrupt Flag Register (TFLG2)

Timer Overflow flag (TOF) will be set whenever the timer count overflows from FFFF to 0000
whenever the timer makes interrupt.

LEDs on the Project Board

The PBMCUSLK provides 8, green LED’s, for use as output indicators. Each LED is
configured for active-high operation. Each LED is individually driven by an ACT buffer
allowing either 5V or 3.3V input levels. Four LED’s are connected directly to the
MCU_PORTA connector Pins 0 to 3. To activate the LEDs on the Project Board – Pin 4 in
Port P must be enabled.

Result:

Thus, the LEDs in the Project Board are activated whenever the timer count overflows.

4-5
4-6
Ex. No: 4b Date:

DIGITAL CLOCK

Aim :

To implement a digital clock using the timer block of HCS12 and display the time in
LCD.

Software Requirements:

Freescale code warrior development studio version 5.

Hardware Requirements:

i. Personal Computer
ii. MC9S12 freescale application module
iii. Interface card
iv. Project Board PBMCUSLK

Theory:

A digital clock displays the time using numbers and it has many applications like cars,
railway stations, houses, offices, etc. in order to provide accurate time and date. In this type of
applications, normally we use RTC (Real Time Clock) ICs to display the time and date
accurately. The circuit displays the time on LCD. For this clock, we can set the time at any
instant. Here, the clock can work in either 24 hour mode or 12 hour mode.

4-7
Program:

#include <hidef.h>
#include "derivative.h"
void delay(void) {
int i,j;
for(i=0;i<100;i++) {
for(j=0;j<100;j++) {
}}}
void main(void) {
unsigned int count=0,sec=0,min=0,hour=0;
SPI_Initialize();
LCD_Initialize();
DDRB=0XFF;
PORTB=0XFF;
TSCR1=0X80;
TSCR2=0X01;
for(;;) {
TFLG2=0X80;
while(!(TFLG2 & 0X80)) {
count=count+1;
if(count==30) {
sec=sec+1;
count=0;
PORTB=0X00;
delay();
PORTB=0XFF;
}
if(sec==60) {
min=min+1;
sec=0;
}
if(min==60) {
hour=hour+1;
min=0;
}
Display_integer(1,0,hour);
LCD_CursorPos(1,2);
LCD_SendCharacter(':');
Display_integer(1,3,min);
LCD_CursorPos(1,5);
LCD_SendCharacter(':');
Display_integer(1,6,sec);
}}}

4-8
Algorithm:
1. Start
2. Declare the variables count,sec,min,hour and initialize them to zero.
3. Call SPI and LCD initialize function.
4. Declare PORTB as output port.
5. Assign the control word for TSCR1 as 0X80.
6. Assign the control word for TSCR2 as 0X01.
7. Inside the infinite for loop assign the control word for TFLG2 as 0X80.
8. When MSB of TFLG2 is not set, do the following.
9. Increment the count.
10. If count reaches 30, increment the seconds and reset count to 0.
11. If seconds reaches 60, increment the minutes and reset seconds to 0.
12. If minute reaches 60, increment the hour and reset minutes to 0.
13. Display the Hours, minutes and seconds in LCD by moving the cursor position respectively.
14. Stop.

Result:

Thus, digital clock is implemented using the timer block of HCS12 and the time is displayed
in LCD.

4-9
4-10
Ex. No: 4c Date:

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.

Software Requirements:

Freescale code warrior development studio version 5.

Hardware Requirements:

i. Personal Computer
ii. MC9S12 freescale application module
iii. Interface card
iv. Project Board PBMCUSLK

Theory:

Real time interrupts are used to keep track of time and also to initiate tasks on a periodic basis.

Whenever 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.

4-11
Program:

#include <hidef.h> /* common defines and macros */


#include "derivative.h" /* derivative-specific definitions */
#pragma CODE_SEG NON_BANKED
unsigned int count =0;
void delay(void) {
int i,j;
for(i=0;i<1000;i++) {
for(j=0;j<1000;j++) {
}
}
}
void main(void) {
DDRB=0XFF;
DDRT=0XFF;
PORTB=0XFF;
RTICTL=0X7F;
CRGINT=0X80;
EnableInterrupts;
for(;;) {
PTT=0X01;
delay();
PTT=0X00;
delay();
}
}
interrupt(((0X10000-Vrti)/2)-1)void RTI_ISR(void) {

count=count+1;
if(count==8) {
PORTB=0XD0;
count=0;
} else {

PORTB=0XFF;
}
CRGFLG=0X80;

4-12
RTI Frequency = Bus Clock / RTICTL

Algorithm:

1. Start
2. Declare a variable called count and initialize to 0.
3. Make PORTB as output port.
4. Make PORTT as output port.
5. Define the control word for RTICTL as 0X7F.
6. Define the control word for CRGINT as 0X80 to enable the interrupt.
7. Toggle the buzzer forever.
8. Call interrupt service routine and perform the following.
9. Increment the value of count.
10. If count reaches 8, glow the LED associated with pin 5 of PORTB.
11. Reset count to 0.
12. Clear the flag by defining CRGFLG=0X80.
13. Stop.

4-13
4-14
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.”

Result:

Thus, the status of buzzer is toggled continuously and Pin5 of PortB is glown whenever
a real time interrupt is generated for every 2 seconds.

4-15

You might also like