Counter/Timer/PWM
incoming Lab.incoming Lab.
Counter
counter is a device which stores the number of times a particular event or process has occurred
synchronous/asynchronous pulseup counter/down Counter
D C B A
0 0 0 0 0
0 0 0 1 1
0 0 1 0 2
0 0 1 1 3
0 1 0 0 4
0 1 0 1 5
0 1 1 0 6count 범위 : 0~2N-1
incoming Lab.incoming Lab.
Timer
timer is a specialized type of clocksynchronous counter with fixed frequency clock
Tclk = 1/fclk D C B A
0 0 0 0 0 0
0 0 0 1 1 Tclk
0 0 1 0 2 2Tclk
0 0 1 1 3 3Tclk
0 1 0 0 4 4Tclk
0 1 0 1 5 5Tclk
0 1 1 0 6 6TclkTimer 의 최소단위 : Tclk
최대 측정 시간 : (2N-1)Tclk
incoming Lab.incoming Lab.
Example
counter : how many tablet ? timer : how long ?
incoming Lab.incoming Lab.
Counter/Timer in Atmega128
8-bit Timer/CounterTCNT0, TCNT2Counting 범위 : 0~255
16-bit Timer/CounterTCNT1H & TCNT1L, TCNT3H & TCNT3LCounting 범위 : 0~216 – 1 = 65535
incoming Lab.incoming Lab.
8-bit Timer/Counter Block Diagram
incoming Lab.incoming Lab.
Basic Operation
prescaler
Counter/timer (TCNTn)
clk
fclk
Tclk = 1/fclk
8 bit / 16 bit
ftimer = fclk/N
Ttimer = N/fclk
fclk = 1 Mhz, N(prescale) = 100Tclk = 1 uSec
ftimer = fclk/N = 10 kHz
Ttimer = 100 uSec
incoming Lab.incoming Lab.
Basic Operation & Timer Interrupt
normal mode : overflow interrupt
clear time on compare match (CTC) mode : output compare interrupt
숫자세기구간
0 TCNTn
255
인터럽트 발생위치
(초기값)
숫자세기구간
0
OCRn
255
인터럽트 발생위치
(한계값)
clkf
NT )-0xFF( 초기값
clkf
NOCRnT
incoming Lab.incoming Lab.
8 bit Timer/Counter Register 관련 Register
TCCRn : Timer/Counter Control Register
예 ) TCCR0 = 0x05 ftimer = fclk /128 = 7372800/128 = 57600 Hz
Ttimer = 1/57600
incoming Lab.incoming Lab.
8 bit Timer/Counter Register 관련 Register
TCNTn : Timer/Counter Register
Overflow Interrupt예 ) TCNT0 = 0x27
구간 = 0xFF-0x27 = 0xD8 = 216 216 * Ttimer 마다 인터럽트 발생이 가능함
숫자세기구간
0 TCNTn
255
인터럽트 발생위치
(초기값)
incoming Lab.incoming Lab.
8 bit Timer/Counter Register 관련 Register
OCRn : Output Compare Register
Output Compare Interrupt예 ) OCR0 = 0xA2
구간 = 0xA2 = 162 162 * Ttimer 마다 인터럽트 발생이 가능함
숫자세기구간
0
OCRn
255
인터럽트 발생위치
(한계값)
incoming Lab.incoming Lab.
8 bit Timer/Counter Register 관련 Register
TIMSK : Timer/Counter Interrupt Mask Register
TOIE0 bit : Overflow Interrupt EnableOCIE0 bit : Output Compare Interrupt Enable
ETIMSK : Extended Timer/Counter Interrupt Mask Register
incoming Lab.incoming Lab.
16-bit Timer/Counter Block Diagram
incoming Lab.incoming Lab.
16 bit Timer/Counter Register 관련 Register
Timer/Counter Control RegisterTCCRnA
TCCRnB
TCCRnC
incoming Lab.incoming Lab.
16 bit Timer/Counter Register 관련 Register
Clock Selection Bit : CSn0 ~ CSn2 in TCCRnB
incoming Lab.incoming Lab.
16 bit Timer/Counter Register 관련 Register
TCNTnH & TCNTnL
숫자세기구간
0 TCNTn
ㅗㅗ
0xFFFF
인터럽트 발생위치
(초기값)
incoming Lab.incoming Lab.
16 bit Timer/Counter Register 관련 Register
Output Compare RegisterOCRnAH & OCRnAH OCRnBH & OCRnBHOCRnCH & OCRnCH
숫자세기구간
0
OCRn
0xFFFF
인터럽트 발생위치
(한계값)
incoming Lab.incoming Lab.
Timer Interrupt Example
Mode : normal mode (overflow interrupt)Interrupt 간격 : 500mSec
fclk = 7372800 Hz 0.5 초 : 3686400 16bit counter = 216 – 1 = 65535
prescale : 1024 3686400/1024 = 3600 => 0xE10 0xFFFF-0xE10 = 0xF1EF
TCCR1A = 0x00; TCCR1B = 0x05; TCCR1C = 0x00;TCNT1H = 0xF1; TCNT1L = 0xEF;TIMSK = 0x04;
incoming Lab.incoming Lab.
Timer Interrupt Example
#include <avr/io.h>#include <avr/interrupt.h>
int main(){DDRD = 0x18;
TCCR1A = 0x00; TCCR1B = 0x05; TCCR1C = 0x00;
TCNT1H = 0xF1; TCNT1L = 0xEF;
TIMSK = 0x04;sei();
while(1); return 0;}
incoming Lab.incoming Lab.
Timer Interrupt Service Routine
ISR(TIMER1_OVF_vect){ static char cnt = 0; // static 은 이전 값을 유지 함
cnt++; cnt %= 2;
if( cnt == 0 ) PORTD &= 0xEF; else PORTD |= 0x10;
TCNT1H = 0xF1; TCNT1L = 0xEF;}
incoming Lab.incoming Lab.
연습
다양한 시간으로 lamp 구동1 초 간격으로
left motor 전진 right motor 전진left motor 정지left motor 후진right motor 정지right motor 후진left motor 정지right motor 정지이하 반복
incoming Lab.incoming Lab.
Timer Interrupt Example (II)
Mode : clear time on compare match (CTC) mode (output compare interrupt)Interrupt 간격 : 250mSec
fclk = 7372800 Hz 0.25 초 : 1843200 16bit counter = 216 – 1 = 65535
prescale : 1024 1843200 /1024 = 1800 => 0x0708
TCCR1A = 0x00; TCCR1B = 0x0D; TCCR1C = 0x00;OCR1A =0x0708; (OCR1AH = 0x07; OCR1AL = 0x08; )TIMSK = 0x10;
incoming Lab.incoming Lab.
CTC mode (output compare interrupt)
Which counter ?TCNT0, TCNT1, TCNT2, TCNT3 ?
Timer/Counter Control RegisterTCCRnA / TCCRnB / TCCRnC = ?
Output Compare RegisterOCRnA / OCRnB / OCRnC = ?
Timer/Counter Interrupt Mask RegisterTIMSK ?
숫자세기구간
0
OCRn
0xFFFF
인터럽트 발생위치
(한계값)
incoming Lab.incoming Lab.
CTC mode (output compare interrupt)
Timer/Counter Control RegisterTCCRnA
TCCRnB
TCCRnC
prescale
Timer/counter mode
0 0
0 1 1 0 1
incoming Lab.incoming Lab.
CTC mode (output compare interrupt)
Timer/counter Mode of Operation
incoming Lab.incoming Lab.
CTC mode (output compare interrupt)
Output Compare RegisterOCRnA / OCRnB / OCRnC = ?
TIMSK : Timer/Counter Interrupt Mask Register
incoming Lab.incoming Lab.
Timer Interrupt Example
#include <avr/io.h>#include <avr/interrupt.h>
int main(){DDRD = 0x18;
TCCR1A = 0x00; TCCR1B = 0x0D; TCCR1C = 0x00;
OCR1A = 0x0708
TIMSK = 0x10;sei();
while(1); return 0;}
incoming Lab.incoming Lab.
Timer Interrupt Service Routine
ISR(TIMER1_COMPA_vect){ static char cnt = 0; // static 은 이전 값을 유지 함
cnt++; cnt %= 2;
if( cnt == 0 ) PORTD &= 0xEF; else PORTD |= 0x10;
}
incoming Lab.incoming Lab.
Timer/Counter 의 Input & Output
InputCounter : asynchronous pulseTimer : external clock
OutputWaveform PWM : Pulse Width Modulation
External Input/OutputI/O Port 에 배정
incoming Lab.incoming Lab.
Timer/Counter 의 Input & Output
TCNT0 : 8bit CounterNo inputOutput : OC0 (PB4)
TCNT1 : 16bit CounterInput : T1 (PD6)Output : OC1A(PB5), OC1B (PB6), OC1C(PB7)
TCNT2 : 8bit CounterInput : T2(PD7)Output : OC2 (PB7)
TCNT3 : 16bit CounterInput : T3 (PE6)Output : OC3A(PE3), OC3B (PE4), OC3C(PE5)
incoming Lab.incoming Lab.
Timer/Count Mode
normal mode : overflow interruptclear time on compare match (CTC) mode : output compare interruptFast PWM Mode
숫자세기구간
0
OCRn
255
인터럽트 발생위치
(한계값)
incoming Lab.incoming Lab.
Timer/Count Mode
Phase Correct PWM Mode
숫자세기구간
0
OCRn
255
인터럽트 발생위치
(한계값)
인터럽트 발생위치
incoming Lab.incoming Lab.
DC 모터의 가감속 제어
DC 모터의 속도제어DC 모터의 전달함수
정상 상태 속도는 입력 전압에 비례
Ks
K
sV
s
a
gain DC1)(
)(
+V
DC모터
0V
Vce가높다+
-
incoming Lab.incoming Lab.
Pulse Width Modulation (PWM, 펄스폭 변조 )
commonly used technique for controlling power to an electrical device
power delivery, voltage regulation, telecommunication, …
신호 크기에 비례하는 pulse width or duty cycle
(%)duty 100TtW
0
0duty
V max25.0
%25duty
VV
max5.0
%50duty
VV
max75.0
%75duty
VV
max
%100duty
VV
incoming Lab.incoming Lab.
Pulse Width Modulation (PWM, 펄스폭 변조 )
Why PWM ?difficult to amplify a voltage with appropriate powercompact and low cost means for applying adjustable power for many devices
Principle of PWM전달되는 power 는 duty cycle 에 비례
PWM frequency
2max
2
max0
2max0
2 1)(
1
VT
tV
VT
tdtV
Tdttv
TV
Wrms
WtT
rms
W
Tf pwm /1
incoming Lab.incoming Lab.
PWM in ATmega128
Fast PWM mode/Phase Correct PWM ModeTCNT0
OC0 (PB4)TCNT1
OC1A(PB5), OC1B (PB6), OC1C(PB7)TCNT2
OC2 (PB7)TCNT3 : 16bit Counter
OC3A(PE3), OC3B (PE4), OC3C(PE5)
OC1B (PB6) : Left Motor EnableOC1C (PB7) : Right Motor EnableTCNT1 => PWM 으로 사용
incoming Lab.incoming Lab.
PWM Example
PWM mode : Fast PWM 10 bit modePWM frequency
motor 와 motor driver 의 주파수 특성 : ?fclk = 7372800 Hz
N(prescale) = 8 TOP = 210-1
숫자세기구간
0
OCRn
255
인터럽트 발생위치
(한계값)
HzTOPN
ff clkpwm 900
)1(
incoming Lab.incoming Lab.
PWM Example
incoming Lab.incoming Lab.
PWM Example
Timer/Counter Control Register
TCCRnA
TCCRnB
TCCRnC
prescale
Timer/counter mode
1 1
0 1 0 1 0
1 0 1 0
incoming Lab.incoming Lab.
PWM Example
incoming Lab.incoming Lab.
PWM Example
#include <avr/io.h>
int main(){
DDRB = 0xCF; PORTB = 0x06;
TCCR1A = 0x2B; TCCR1B = 0x0A; TCCR1C = 0x00;
OCR1B = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */OCR1C = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */
while(1);return 0;
}
incoming Lab.incoming Lab.
실습 과제
좌우 모터에 대해저속 /중속 /고속을 정의전진 /후진 /정지를 정의Left/Right Switch 를 이용하여 Motor Disable (Interrupt)USART 통신을 이용하여 제어 (Interrupt)