Lab 08 MPMC
Lab 08 MPMC
Name:
Reg. No:
Date of
Experiment:
Internal Timer: As an internal timer the unit, ticks on the oscillator frequency. The
oscillator frequency can be directly feed to the timer or it can be pre-scaled. In this
mode it used generate precise delays. Or as precise time counting machine.
External Counter: In this mode the unit is used to count events on a specific external
pin on a MCU.
Pulse width Modulation (PWM) Generator: PWM is used in speed control of motors
and various other applications.
Input Capture Mode: Input capture mode is used to measure time period and pulse
width of an external frequency.
Arduino UNO has 3 timers and these times count at some frequency derived from the
16MHz system clock:
Timer0 is an 8-bit timer:
It is used by millis(), delay() and analogWrite() on pins 5 and 6.
Timer1 is a 16-bit timer:
It is used by analogWrite() functions on pins 9 and 10;
It is also used for driving servos using the Servo library so you can’t use
analogWrite with these pins when using the Servo library.
Timer2 is an 8-bit timer.
It is used by analogWrite() functions on pins 3 and 11 and the Tone() library
Clock Divisor: configured to alter the frequency and various counting modes
Timers can be configured to generate interrupts when they overflow or reach a
specific count
Important Registers and Flags Associated with Timers:
Each timer has following registers associated with it, we can change the Timer behavior
through the timer register:
TCNTn (Timer/Counter Reg): Upon reset, it has zero value and counts with
each timer clock. We can load/read a value into the TCNT0 register
7 6 5 4 3 2 1 0
WGM WGM TCCR0
COM0A1 COM0A0 COM0B1 COM0B0 - -
01 00 A
R/W R/W R/W R/W R R R/W R/W
0 0 0 0 0 0 0 0
7 6 5 4 3 2 1 0
WGM CS TCCR0
FOC0A FOC0B - - CS01 CS00
02 02 B
W W R R R/W R/ R/W R/W
W
0 0 0 0 0 0 0 0
Timer1
Timer 1 is 16-bit timer and following is the register’s details
7 6 5 4 3 2 1 0
COM1A COM1A COM1B COM1B WGM1 WGM1 TCCR1
- -
1 0 1 0 1 0 A
R/W R/W R/W R/W R R R/W R/W
Lab 8: Counter Mode Programming with Arduino Page 52
0 0 0 0 0 0 0 0
7 6 5 4 3 2 1 0
WGM1 CS1 TCCR1
ICNC1 ICES1 - WGM13 CS11 CS10
2 2 B
R/W R/W R R R/W R/W R/W R/W
0 0 0 0 0 0 0 0
Schematic Diagram:
void setup() {
Serial.begin(9600);
pinMode(5,INPUT_PULLUP); // Set Pin No.5 (T1 Pin) as input
}
void loop(){
TCNT1 = 0x0000; // Start counting from 0
TCCR1A = 0x00; // 16-bit counter, Normal Mode
TCCR1B = 0x06; // Start Counting at
// Falling Edge using T1 pin
// For Rising Edge put 0x07
delay(1000); // delay of One Second
TCCR1B = 0x00; // Stop Counting
Serial.print("Input Frequency = ");
Serial.print(TCNT1);
Serial.print(" Hz ");
Serial.print("Time Period = ");
float tp = 1000.0F / TCNT1;
Serial.print(tp);
Serial.println(" ms");
}
Lab Task:
Heart pulses of a patient, in the form of square wave are reaching at Pin T1 (PD5)
(Arduino PIN No. 5) of Arduino UNO Board. Write a program to measure the current pulse
rate per minute of that patient after each 20 seconds and send this answer via serial port
to Computer.