Sismik 6 - Timer
Sismik 6 - Timer
Sismik 6 - Timer
Figure 7.1: An example analog signal and a pulse width modulated representation
4
DEMODULATION
• Converting the square-wave signal to the desired analog
value is referred to as demodulation, and requires a
demodulator circuit to use the pulse widths as a measure
for the signal amplitude
• Consider the simple low-pass filter (LPF) shown in Fig.
7.2.
MODULATION
• The remaining component to outputting an analog signal
from a digital microcontroller is to generate the
appropriate PWM waveform, a process referred to as
modulation; i.e, an analog message signal modulates the
square-wave carrier by altering its duty cycle
• Most microcontrollers provide at least one port that has
timer sub-circuitry capable of generating PWM signals on
a port pin
• Typically, one just needs to configure the square-wave
frequency and desired duty cycle via a couple of registers.
• When enabled, the port pin will output a PWM signal that
can be demodulated in order to provide an approximation
to an analog signal.
10
• Note, in the init() function within the Wiring library, all three
timers are pre-initialized with some specific functionality in
mind. In particular, the often used delay() function relies
on a timer 0 interrupt, and so we will avoid using it for the
time being.
11
ARDUINO PWM
• Pulse Width Modulation, or PWM, is a technique for
getting analog results with digital means. Digital control is
used to create a square wave, a signal switched between
on and off. This on-off pattern can simulate voltages in
between full on (5 Volts) and off (0 Volts) by changing the
portion of the time the signal spends on versus the time
that the signal spends off. The duration of "on time" is
called the pulse width. To get varying analog values, you
change, or modulate, that pulse width. If you repeat this
on-off pattern fast enough with an LED for example, the
result is as if the signal is a steady voltage between 0 and
5v controlling the brightness of the LED.
14
Aplikasi PWM
PWM has several uses:
• Dimming an LED
• Providing an analog output; if the digital output is filtered,
it will provide an analog voltage between 0% and 100% .
• Generating audio signals.
• Providing variable speed control for motors.
• Generating a modulated signal, for example to drive an
infrared LED for a remote control.
18
rearranging the equation above, you can solve for the compare
match register value that will give your desired interrupt
frequency:
• Timer Registers
• Several registers are used to control each timer. The
Timer/Counter Control Registers TCCRnA and TCCRnB
hold the main control bits for the timer. (Note that
TCCRnA and TCCRnB do not correspond to the outputs A
and B.) These registers hold several groups of bits:
29
• The bits are slightly different for each timer, so consult the
datasheet for details. Timer 1 is a 16-bit timer and has
additional modes. Timer 2 has different prescaler values.
30
Manfaat Timer
Perhatikan program berikut
classic example is turning a relay on for 10 minutes. The 'delay'-way looks like
this:
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(pin, HIGH);
delay(10 * 60 * 1000);
digitalWrite(pin, LOW);
}
void loop()
{
}
31
Timer t;
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
}
void loop()
{
t.update(); //This will service all the events associated with the timer.
}
33
• #include "Timer.h"
•
• Timer t;
• int pin = 13;
• void setup()
• {
• Serial.begin(9600);
• pinMode(pin, OUTPUT);
• t.oscillate(pin, 100, LOW); // Toggle pin 13 selama 100 ms dg nilai awal LOW
• //This will cause the LED to toggle state every 100 milliseconds.
• t.every(1000, takeReading);//Jalankan takeReading setiap 1 s
• }
• void loop()
• {
• t.update(); //This will service all the events associated with the timer
• }
• void takeReading()
• {
• Serial.println(analogRead(0));
• }
35
• int update()
• Must be called from 'loop'. This will service all the events
associated with the timer
38
Menghentikan kejadian
#include "Timer.h"
Timer t;
int ledEvent;
void setup()
{
Serial.begin(9600);
int tickEvent = t.every(2000, doSomething);
Serial.print("2 second tick started id=");
Serial.println(tickEvent);
pinMode(13, OUTPUT);
ledEvent = t.oscillate(13, 50, HIGH);
Serial.print("LED event started id=");
Serial.println(ledEvent);
void loop()
{
t.update();
}
void doSomething()
{
Serial.print("2 second tick: millis()=");
Serial.println(millis());
}
void doAfter()
{
Serial.println("stop the led event");
t.stop(ledEvent);
t.oscillate(13, 500, HIGH, 5); //Toggle ke pin 13 selama 500ms dimulai dg HIGH
//sebanyak 5 x
}
40
Timer Frekuensi
Untuk menghitung frekuensi timer dengan frekuensi yang
ditentukan dapat dilakukan dengan cara sebagai berikut.
Menentukan frekuensi pembagi, misal 2 Hz
• Mengetahui frekuensi kerja Arduino, yakni 16 MHz
• Mengetahui prescaler timer, 256 untuk 8 bit, 65536 untuk 16
bit
• Membagi nilai frekuensi kerja Arduino (16 MHz) dengan
prescaler (256), hingga diperoleh hasil 62500
• Membagi hasil tersebut (62500) dengan frekuensi ditentukan (2
Hz), hingga diperoleh hasil 31250
• Memastikan bahwa hasil tersebut dapat digunakan dengan
membandingkannya nilai maximum prescaler, bila nilai tersebut
lebih kecil (dalam hal ini 31250 lebih kecil dari 65536), maka
hasilnya tersebut valid, bila tidak valid gunakan nilai prescaler
yang lebih besar
compare match register = [ 16,000,000Hz/
(prescaler * desired interrupt frequency) ] - 1
42
Timer 0
Registers
44
Timer 1
Registers
45
Timer 2
Registers
46
Setting bit
TCCR0A
TCCR0B
47
Setting bit
TCCR1A
TCCR1B
48