Vous êtes sur la page 1sur 28

Loops and Time Delays

ECET 209 – Lecture 8


Introduction to Microcontrollers
Example

• Flash the LEDs on PORTC 10 times at a


rate of 2Hz

– Flash LEDs => Turn on then off


– 2 Hz => freq = 1/time => time = 1/freq
– Do it 10 times

ECET 209 Purdue University 2


Flowchart
set count to zero

increment count

No
Count = 10? Turn LEDs On

Yes
Delay 250 mSec

Turn LEDs Off

Delay 250 mSec

ECET 209 Purdue University 3


Flowchart
count = 0; set count to zero
count++;
increment count
}
while (count < 10)
No
Count = 10? Turn LEDs On PORTC = 0x00;
{
Yes
Delay 250 mSec ???

Turn LEDs Off


PORTC = 0xFF;

Delay 250 mSec

???
ECET 209 Purdue University 4
Delays

• To use the “Standard” delay functions

– Include the delay.h header file

– Simply specify the length of the delay

ECET 209 Purdue University 5


#include <delay.h>

delay_ms( 1 ); // wait 1 mSec

This is an unsigned int value


ECET 209 Purdue University 6
Flowchart
count = 0; set count to zero
count++;
increment count
}
while (count < 10)
No
Count = 10? Turn LEDs On PORTC = 0x00;
{
Yes
Delay 250 mSec delay_ms(250);

Turn LEDs Off


PORTC = 0xFF;

Delay 250 mSec


delay_ms(250);
ECET 209 Purdue University 7
count = 0;
while (count < 10) // flash 10 times
{
PORTC = 0x00; // leds on
delay_ms(250); // delay – 2 Hz
PORTC = 0xFF; // leds off
delay_ms(250);
count++;
}

ECET 209 Purdue University 8


Flowchart
count = 0; set count to zero
count++;
increment count
}
while (count < 10)
No
Count = 10? Turn LEDs On PORTC = 0x00;
{
Yes
Delay 250 mSec delay_ms(250);

Turn LEDs Off


PORTC = 0xFF;

Delay 250 mSec


delay_ms(250);
ECET 209 Purdue University 9
Flowchart
set count to zero for (count = 0; count < 10; count++)

increment count
}
No
Count = 10? Turn LEDs On PORTC = 0x00;
{
Yes
Delay 250 mSec delay_ms(250);

Turn LEDs Off


PORTC = 0xFF;

Delay 250 mSec


delay_ms(250);
ECET 209 Purdue University 10
for (count = 0; count < 10; count++) // flash 10 times
{
PORTC = 0x00; // leds on
delay_ms(250); // delay – 2 Hz
PORTC = 0xFF; // leds off
delay_ms(250);
}

ECET 209 Purdue University 11


Problem

• Wait for any Push Button on PORTA to be


pressed…

ECET 209 Purdue University 12


Waiting for a Pushbutton

Read the value on


the port

Is a No
pushbutton
pressed?

ECET 209 Purdue University 13


Flowchart to Code

while
Logic
No
High on
PORTA?

Yes

ECET 209 Purdue University 14


Waiting
while (PINA == 0)

Logic {
No
High on
PORTA?

Yes

ECET 209 Purdue University 15


The Code
while (PINA == 0);

while (PINA == 0)
{
};

while (PINA == 0)
{
}
ECET 209 Purdue University 16
The Code
while (PINA == 0);

while (PINA == 0)
{
};

while (PINA == 0)
{
}
ECET 209 Purdue University 17
While Loops

• Use them to “Wait” for things

• Used to repeat things while a certain


condition is True or becomes false

• Only perform the associated block of code


in the event that the condition is True
ECET 209 Purdue University 18
Another Example

• Create a program that will count the time


(in mSec) that a pushbutton is pressed.

– Wait for a pushbutton to be pressed


– Count the press
– Delay for a millisecond
– Repeat until the button is released

ECET 209 Purdue University 19


Wait for Button
to be Pressed

Clear Counter

Delay

Increment Counter

Wait
for button to be
released

Display the
Results

ECET 209 Purdue University 20


while( PINA == 0)
{
} No
button
pressed?

Yes

counter = 0; set count to zero

delay 1mSec

counter++; increment count

button No
released?

Yes
ECET 209 Purdue University 21
while( PINA == 0)
{
} No
button
pressed?

Yes

counter = 0; set count to zero

do
delay 1mSec

counter++; increment count

button No
while( PINA != 0) released?

Yes
ECET 209 Purdue University 22
while( PINA == 0)
{
} No
button
pressed?

Yes
counter = 0;
set count to zero

do

delay_ms( 1 ); delay 1mSec

counter++; increment count

button No
released?
while ( PINA != 0 );
Yes
ECET 209 Purdue University 23
while( PINA == 0)
{
} No
button
pressed?

Yes
counter = 0;
set count to zero

do
{
delay_ms( 1 ); delay 1mSec

counter++; increment count

}
button No
released?
while ( PINA != 0 );
Yes
ECET 209 Purdue University 24
Got Everything??
while( PINA == 0)
{
} No
button
pressed?

Yes
counter = 0;
set count to zero

do
{
delay_ms( 1 ); delay 1mSec

counter++; increment count

}
button No
released?
while ( PINA != 0 );
Yes
ECET 209 Purdue University 25
Got Everything??
while( PINA == 0)
{
} button No
pressed?

Yes
counter = 0;
set count to zero

do
{
delay 1mSec
delay_ms( 1 );

counter++; increment count

}
button No
released?
while ( PINA != 0 );
Yes
ECET 209 Purdue University 26
while( 1 )

while( PINA == 0) {
{
} No
button
pressed?

Yes
counter = 0;
set count to zero

do
{
delay_ms( 1 ); delay 1mSec

counter++; increment count

}
button No
released?
while ( PINA != 0 );
Yes
ECET 209 Purdue University 27
}
#include <delay.h>
while(1)
{
while ( PINA == 0) // wait for a PB
{
}
counter = 0; // clear the counter
do
{
delay_ms (1); // delay
counter++;
}
while ( PINA != 0 ); // wait for release
PORTC = ~counter; // display the results
}
ECET 209 Purdue University 28

Vous aimerez peut-être aussi