3.9 Creating Timer
3.9 Creating Timer
3.9 Creating Timer
Cre e r
ating a Tim
Summary:
In this course, you will learn how to make timers for all kinds of occasions. In the process of learning
how to make it, you will learn about MCU interrupts, key debouncing, dynamic scanning of digital displays
and other related knowledge.
Component Required:
(1) x Elegoo Uno R3
(3) x key
(5) x 220_resistance
(1) x 10k_resistance
(1) x 74HC595 IC
Component Introduction
There are three buttons in this experiment,
"minute" button:
Every times you press it, the time you set will add one minute. Once you press the key and hold it
down, the time will add quickly.
"second" button:
Every times you press it, the time you set will add one second. Once you press the key and hold it
down, the time will add quickly.
"function" button:
After finishing setting the time, you should press this button. When you press it, you will see the 4
Digit 7-Segment Display begin to count down.If you press it again during counting down, the time
you set will be cleared. When the countdown time ends, the buzzer sounds and the LED lights flicker.
At this time, if you press the function key again, the sound will turn off and the LED will stop flickering
and the time you set will be cleared too.
Wiring diagram
Because there are too many devices connected, the driving ability of the IO port is not enough. So
we use high-level driving mode for the A0 port connected with the led, and add 10K pull-up
resistance to help the IO port output current. Normally, we use NPN 8050 transistor to drive the
load of about 5V.
Watchdog interrupt:
Although no watchdog interruption is used in this
course, we also need to understand the watchdog
interruption.
The watchdog timer is set to one second and the setup contains a delay of two seconds therefore
the watchdog will trigger.
In this case, you need to delete it before running the program.
Wdt_enable (WDTO_1S);
Serial. println ("watchDog_interrupt triggered");preceding "//"
After uploading the program, you will see:
#include "avr/wdt.h"
void setup()
{
wdt_enable(WDTO_1S);
Serial.println("watchDog_interrupt triggered");
}
void button_interrupt()
{
Serial.print("we begin to count:");
Serial.println(number++);
delay(2000);
wdt_reset(); //we should feed the dog avoiding it reseted.
}
When the mechanical contact is opened and closed, because of the elastic effect of the contact,
the switch will not close stably, it will bounce for a short time. The same thing happens when the switch
is opened. This bouncing is eliminated in this lesson.
void timer2_interrupt()
After wiring, please open the program in the code
{
folder- demo1 and click UPLOAD to upload the
Serial.println("timer2_interrupt triggered");
program.
}
Before running this program, make sure that
the < PinChangelnt > library and < MsTimer2 >
library is installed or reinstalled if necessary.
Otherwise, your code will not be compiled.
For more information about loading library files,
see Lesson 5 in part 1.
Timer2 interrupt:
Run the code you will see:
This means that the code that is executed sequentially in loop() will be interrupted by timer interrupt.
The contents of interrupt function will be executed first. Only after the interrupt function has been
executed, will the code run return to loop() function.
In this case, we set the "FALLING " interrupt when the key is pressed.
When you press the button, you will see the following:
So milliseconds are used to determine if the key has been pressed twice or not.
The time we use to discriminate between bouncing or 2 genuine presses is 200 milliseconds.
Greater than 200 ms determines that it is not switch bounce.
Demo3:
After wiring, please open the program in the code folder- demo3 and click UPLOAD to
upload the program.
After wiring, please open the program in the code folder- MyTimer and click UPLOAD
to upload the program.