C7
C7
1.Đo thời gian: Timer được sử dụng để đo thời gian trong các ứng dụng như đo tốc độ, đo thời gian phản hồi,
đo thời gian chờ, đo thời gian giữa các sự kiện, vv.
2.Điều khiển tần số: Timer được sử dụng để tạo ra các tín hiệu xung với tần số cố định hoặc biến đổi, được
sử dụng để điều khiển các thiết bị ngoại vi như động cơ, bóng đèn, loa, vv.
3.Giao tiếp: Timer được sử dụng để tạo ra các tín hiệu xung để truyền thông dữ liệu giữa các thiết bị, như
trong các giao thức như UART, SPI, I2C, vv.
4.Hiển thị: Timer được sử dụng để quét các đèn LED để hiển thị các số hoặc ký tự trên các màn hình LED,
LCD, vv.
5.Kiểm tra độ chính xác: Timer được sử dụng để kiểm tra độ chính xác của các thạch anh hoặc các nguồn
tần số khác.
6.Lập lịch: Timer được sử dụng để thiết lập lịch cho các ứng dụng như đồng hồ, báo thức, vv.
7.Bảo vệ: Timer được sử dụng để giám sát các hoạt động của hệ thống và bảo vệ chúng khỏi các lỗi như quá
dòng, quá tải.
Counter mode and Timer mode
• There are counter registers in microcontrollers to count an
event or generate time delays.
• When we connect the external event source to the clock pin
of the counter register. This is counter mode.
• When we connect the oscillator to the clock pin of the
counter. This is timer mode
• one way to generate a time delay is to clear the counter at
the start time and wait until the counter reaches a certain
number.
Counter mode and Timer mode
• In the microcontrollers, there is a flag for each of the
counters. The flag is set when the counter overflows, and
is cleared by software.
• The second method to generate a time delay is to load
the counter register and wait until the counter overflows
and the flag is set.
• In ATmega32,ATmega16, there are three timers: Timer0,
Timer1, and Timer2. TimerO and Timer2 are 8-bit, while
Timer1 is 16-bit.
• In AVR, for each of the timers, there is a TCNTn
(timer/counter) register. In ATmega32 we have TCNTO,
TCNT1, and TCNT2.
• The TCNTn register is a counter. Upon reset, the TCNTn
contains zero. It counts up with each pulse. The contents
of the timers/ counters can be accessed using the
TCNTn. You can load/read a value into the TCNTn register
Basic Registers of Timers
• Each timer has a TOVn
(Timer Overflow) flag, When a
timer overflows, its TOVn flag is
set.
• Each timer also has the TCCRn
(timer/counter control register)
register for setting modes(timer
or counter) of operation.
• Each timer also has an OCRn
(Output Compare Register). The
content of the OCRn is
compared with the content of
the TCNTn. When they are
equal the OCFn (Output
Compare Flag) flag is set.
Timer0 Programming
• Timer0 is 8-bit in ATmega32; thus, TCNT0 (timer/ counter
register) is 8-bit.
1 1 Fast PWM
0 1 0 CLK / 8
0 1 1 CLK / 64
1 0 0 CLK / 256
1 0 1 CLK / 1024
Timer 2
TOV2 Timer2 Overflow flag bit
OCF2Timer2 output compare flag bit
ASSR (Asynchronous Status Register)
ASSR Register
AS2 TCN2UB OCR2UB TCR2UB
void blinkLED(){
digitalWrite(led, !digitalRead(led));
}
void loop() {
// code khác có thể chạy ở đây mà không bị chặn bởi delay()
}
#include <TimerOne.h>
The #define sensorPin 2
volatile unsigned long pulseCount;
Program void setup(){
Code Serial.begin(9600);
pinMode(sensorPin, INPUT);
(Counter) Timer1.initialize(100000); // 100ms
Timer1.attachInterrupt(pulseCounter);
}
void pulseCounter(){
pulseCount++;
}
void loop(){
Serial.println(pulseCount);
pulseCount = 0;
delay(100);
}
The #include <TimerOne.h>
// Khai báo chân tín hiệu đếm xung void loop() {
Program const byte sensorPin = 2; // In ra số xung đếm được
Serial.println(pulseCount);
Code // Biến đếm xung
volatile unsigned long pulseCount; // Reset biến đếm
(Counter) void setup() {
pulseCount = 0;
Serial.begin(9600); // Delay 1s
delay(1000);
// Đặt chân tín hiệu là INPUT }
pinMode(sensorPin, INPUT);
// Hàm xử lý ngắt
void pulseCounter(){
pulseCount++;
}
The Outputs
Summary on Operation of Timer1
Timer0 and Timer2 also
Can be obtained using
Output
(PWM Wave of discrete frequency)
Timer Register Resets : Normal
Input
6. Input Capture in ICP1 pin, (Time Period & DC)
Generation of PWM having Variable Frequency
• Previously we discussed about fixed frequency
PWM
• It was fixed frequency because the Timer register
Resets at TOP value
• In order to obtain variable frequency PWM we
need to have a choice of WGM where Timer
Register Resents at ICR1 register value.
• Let us follow the diagram of the next slide.
Value increases
Value in in TCNT0 Register
ICR1
Your Set
Values in
OCR0
Time Period, T1
ARDUINO CODE TIMER
#include <TimerOne.h>
void setup() {
Serial.begin(9600); // Khởi tạo cổng Serial để in kết quả
Timer1.initialize(1000000); // Thiết lập timer0 với chu kỳ 1 giây
Timer1.attachInterrupt(timer_callback); // Bật ngắt khi timer0 đếm đến overflow
}
void loop() {
// Không có gì để làm trong hàm loop()
}
void timer_callback() {
count++; // Tăng biến đếm khi có ngắt xảy ra
Serial.println("Timer interrupt #" + String(count));
1 // In kết quả
}
div 5.6
div
ARDUINO CODE TIMER
// Khai báo chân kết nối encoder void loop() {
#define ENC_A 2 // Tính toán thời gian từ lần đọc trước đó đến hiện tại
#define ENC_B 3 unsigned long deltaTime = millis() - lastTime;
// Biến lưu trữ số lượng xung // Nếu thời gian đọc encoder đã đủ 1 giây
volatile int count = 0; if (deltaTime >= 1000) {
// Tính toán tốc độ dựa trên số lượng xung đọc được và thời
// Biến lưu trữ thời gian đọc encoder trước đó gian đã trôi qua
unsigned long lastTime = 0; speed = (float)count / deltaTime * 1000;
// Biến lưu trữ tốc độ hiện tại // Reset số lượng xung và thời gian đọc encoder trước đó
volatile float speed = 0; count = 0;
lastTime = millis();
void setup() {
// Khai báo chân kết nối encoder là INPUT_PULLUP // In ra tốc độ hiện tại trên Serial Monitor
pinMode(ENC_A, INPUT_PULLUP); Serial.print("Speed: ");
pinMode(ENC_B, INPUT_PULLUP); Serial.print(speed);
1 Serial.println(" RPM");
// Bật interrupt cho chân A của encoder div 5.6}
attachInterrupt(digitalPinToInterrupt(ENC_A), }
readEncoder, CHANGE); div
}
void readEncoder() {
// Đọc trạng thái của chân A và chân B của encoder
int a = digitalRead(ENC_A);
int b = digitalRead(ENC_B);
analogWrite(pin, number);
The #include <TimerOne.h>
void loop() {
// không cần code ở đây
}