0% found this document useful (0 votes)
132 views18 pages

L12 Timer Functions PDF

This document discusses microcontroller timers and creating delays using timers. It introduces microcontroller timer configuration using prescalers and clock selection. It then shows an example of using Timer 1 to create a 1 millisecond delay. Finally, it discusses creating a variable delay function that allows specifying the desired delay time by calling a 1ms delay function in a loop.
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)
132 views18 pages

L12 Timer Functions PDF

This document discusses microcontroller timers and creating delays using timers. It introduces microcontroller timer configuration using prescalers and clock selection. It then shows an example of using Timer 1 to create a 1 millisecond delay. Finally, it discusses creating a variable delay function that allows specifying the desired delay time by calling a 1ms delay function in a loop.
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

Microcontroller Timers

ECET 209 – Lecture 12


Introduction to Microcontrollers
Clock Select

GND (no signal )


System Clock

CK/8

TCCR1B
System Clock
Prescale

CK/64
Timer/Counter
CK/256
Register
CK/1024

External Pin T1

CS10
CS11
CS12
ECET 209 Purdue University 2
Microcontroller Timer

ECET 209 Purdue University 3


Microcontroller Timer

ECET 209 Purdue University 4


Microcontroller Timer

ECET 209 Purdue University 5


Microcontroller Timer

ECET 209 Purdue University 6


Creating a 1mSec Delay

• To create a desired delay, we must calculate


the number of timer ticks it takes to
implement the delay.

• The equation is:

#ticks = desired delay / (fosc / prescale)-1

ECET 209 Purdue University 7


Load TCNT1 with
64,786 TCNT1 = 64786;

Select Clock / 8 TCCR1B = 2;

while ((TIFR & 0x04) == 0)


Wait for TOV1
to be set {
}

Stop the Timer


TCCR1B = 0;

Clear the Timer


Overflow Flag TIFR = TIFR | 0x04;

ECET 209 Purdue University 8


TCNT1 = 64786; // set for 250 counts
TCCR1B = 2; // select CLK / 8
while ( (TIFR & 0x04) == 0) // wait for flag
{
}
TCCR1B = 0; // stop the clock
TIFR = TIFR | 0x04; // clear the flag

ECET 209 Purdue University 9


Goal

• Our goal is to create our own timer delay


• Specifically, we are going to:
– Create a 1 millisecond time delay using Timer 1
– Later, we make it a variable 1mSec delay

ECET 209 Purdue University 10


Functions

• Functions are “building blocks”


– Perform complicated processes ( > 1 line of C )
– Eliminate redundant code
– Make code easier to read
– Allow for “libraries”
– Allow software to be modularized

ECET 209 Purdue University 11


delay_1ms Function

void delay_1ms ( void ); // function prototype

delay_1ms(); // delay for 1mSec

ECET 209 Purdue University 12


delay_1ms()
void delay_1ms(void)
{
TCNT1 = 64786; // set for 250 counts
TCCR1B = 2; // select CLK / 8
while ( (TIFR & 0x04) == 0) // wait for flag
{
}
TCCR1B = 0; // stop the clock
TIFR = TIFR | 0x04; // clear the flag
}

ECET 209 Purdue University 13


What if we needed a variable
1mSec delay?

ECET 209 Purdue University 14


Variable 1mSec delay

counter = 0
Set loop counter
to zero counter++
Increment loop
counter
for loop
Is
loop counter Yes
less than
desired?

No

Delay 1mSec

delay_1ms( )

ECET 209 Purdue University 15


Variable 1mSec delay
void delay(unsigned int time)
{
unsigned int cntr;
for (counter=0; counter < time;counter++)
{
delay_1ms( );
}
}

ECET 209 Purdue University 16


Example Functions

delay_ms( 4 );

void delay_ms ( unsigned int );

Function Prototype

ECET 209 Purdue University 17


Any Questions?

ECET 209 Purdue University 18

You might also like