Professional Documents
Culture Documents
Pedestrian Crossing Traffic Light System Tasks 4 To 6
Pedestrian Crossing Traffic Light System Tasks 4 To 6
Pedestrian Crossing Traffic Light System Tasks 4 To 6
5) Timers
Arduino’s microcontroller is an ATmega328 which has 3 timers, timer0 (8 bit), timer1 (16 bit)
and timer2 (8 bit), each of the timers has a counter that is incremented on each tick of the timer's
clock. The Arduino clock runs at 16MHz, this is the fastest speed that the timers can increment
their counters. For example Timer0 and timer2 are 8 bit timers, meaning they can store a
maximum counter value of 255.wich mean it would take 256/16,000,000 seconds (~16us) for the
counter to reach its maximum (overflow) and go back to zero. It is impossible for humans to
notice a blinking LED for example at that frequency so the solution is to use a pre-scale which
divides the pulses coming from the internal clock by a determined amount. This input is sent to
the control circuit which increments the TCNT register.
Let’s say we gonna use timer1, we can pre-scale the timer by settings the Clock Select bits
(CS12/CS11/CS10) in Timer/Counter1 Control Register B (TCCR1B) (from
ATmega48PA/88PA/168PA/328P datasheet)
Timer Prescaler
CPU CLOCK TCNT1
Divide by 1024=
16 MHZ The Counter
15625
TCNT1 is a 16-bit counter it has 65535 for maximum timer counter value and after we configured the
register TCCR1B, the counter will be incrementing with a frequency of 15625 HZ counting to 65535 at
this speed would take = 4.194 seconds, then 1 second will take =15622.16~15622 increments.
/* Oussama Lakhneche */
#define yellowLed 2
void setup() {
pinMode(yellowLed, OUTPUT);
void loop() {
digitalWrite(yellowLed, HIGH);
TCNT1 = 0;
}
6) Interrupt
For our case the main task is to wait for button to be pressed and we can make an interrupt that can be
launched once the state of the input has changed for example we choose pin 2 as an input (or an output is
doesn’t matter cause in the datasheet even if the pin is set as output it will trigger an interrupt) next step is
to setup EIMSK (external interrupt mask register). Then write the interrupt service routine .