Download presentation
Presentation is loading. Please wait.
Published byAldous Hood Modified over 8 years ago
1
Counter/Timer/PWM
2
incoming Lab. Counter counter is a device which stores the number of times a particular event or process has occurred synchronous/asynchronous pulse up counter/down Counter DCBA 00000 00011 00102 00113 01004 01015 01106 count 범위 : 0~2 N -1
3
incoming Lab. Timer timer is a specialized type of clock synchronous counter with fixed frequency clock T clk = 1/f clk DCBA 00000 0 00011 T clk 00102 2T clk 00113 3T clk 01004 4T clk 01015 5T clk 01106 6T clk Timer 의 최소단위 : T clk 최대 측정 시간 : (2 N -1)T clk
4
incoming Lab. Example counter : how many tablet ? timer : how long ?
5
incoming Lab. Counter/Timer in Atmega128 8-bit Timer/Counter TCNT0, TCNT2 Counting 범위 : 0~255 16-bit Timer/Counter TCNT1H & TCNT1L, TCNT3H & TCNT3L Counting 범위 : 0~2 16 – 1 = 65535
6
incoming Lab. 8-bit Timer/Counter Block Diagram
7
incoming Lab. Basic Operation prescaler Counter/timer (TCNTn) clk f clk T clk = 1/f clk 8 bit / 16 bit f timer = f clk /N T timer = N/f clk f clk = 1 Mhz, N(prescale) = 100 T clk = 1 uSec f timer = f clk /N = 10 kHz T timer = 100 uSec
8
incoming Lab. Basic Operation & Timer Interrupt normal mode : overflow interrupt clear time on compare match (CTC) mode : output compare interrupt
9
incoming Lab. 8 bit Timer/Counter Register 관련 Register TCCRn : Timer/Counter Control Register 예 ) TCCR0 = 0x05 f timer = f clk /128 = 7372800/128 = 57600 Hz T timer = 1/57600
10
incoming Lab. 8 bit Timer/Counter Register 관련 Register TCNTn : Timer/Counter Register Overflow Interrupt 예 ) TCNT0 = 0x27 구간 = 0xFF-0x27 = 0xD8 = 216 216 * T timer 마다 인터럽트 발생이 가능함
11
incoming Lab. 8 bit Timer/Counter Register 관련 Register OCRn : Output Compare Register Output Compare Interrupt 예 ) OCR0 = 0xA2 구간 = 0xA2 = 162 162 * T timer 마다 인터럽트 발생이 가능함
12
incoming Lab. 8 bit Timer/Counter Register 관련 Register TIMSK : Timer/Counter Interrupt Mask Register TOIE0 bit : Overflow Interrupt Enable OCIE0 bit : Output Compare Interrupt Enable ETIMSK : Extended Timer/Counter Interrupt Mask Register
13
incoming Lab. 16-bit Timer/Counter Block Diagram
14
incoming Lab. 16 bit Timer/Counter Register 관련 Register Timer/Counter Control Register TCCRnA TCCRnB TCCRnC
15
incoming Lab. 16 bit Timer/Counter Register 관련 Register Clock Selection Bit : CSn0 ~ CSn2 in TCCRnB
16
incoming Lab. 16 bit Timer/Counter Register 관련 Register TCNTnH & TCNTnL
17
incoming Lab. 16 bit Timer/Counter Register 관련 Register Output Compare Register OCRnAH & OCRnAH OCRnBH & OCRnBH OCRnCH & OCRnCH
18
incoming Lab. Timer Interrupt Example Mode : normal mode (overflow interrupt) Interrupt 간격 : 500mSec f clk = 7372800 Hz 0.5 초 : 3686400 16bit counter = 2 16 – 1 = 65535 prescale : 1024 3686400/1024 = 3600 => 0xE10 0xFFFF-0xE10 = 0xF1EF TCCR1A = 0x00; TCCR1B = 0x05; TCCR1C = 0x00; TCNT1H = 0xF1; TCNT1L = 0xEF; TIMSK = 0x04;
19
incoming Lab. Timer Interrupt Example #include int main(){ DDRD = 0x18; TCCR1A = 0x00; TCCR1B = 0x05; TCCR1C = 0x00; TCNT1H = 0xF1; TCNT1L = 0xEF; TIMSK = 0x04; sei(); while(1); return 0; }
20
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; }
21
incoming Lab. 연습 다양한 시간으로 lamp 구동 1 초 간격으로 left motor 전진 right motor 전진 left motor 정지 left motor 후진 right motor 정지 right motor 후진 left motor 정지 right motor 정지 이하 반복
22
incoming Lab. Timer Interrupt Example (II) Mode : clear time on compare match (CTC) mode (output compare interrupt) Interrupt 간격 : 250mSec f clk = 7372800 Hz 0.25 초 : 1843200 16bit counter = 2 16 – 1 = 65535 prescale : 1024 1843200 /1024 = 1800 => 0x0708 TCCR1A = 0x00; TCCR1B = 0x0D; TCCR1C = 0x00; OCR1A =0x0708; (OCR1AH = 0x07; OCR1AL = 0x08; ) TIMSK = 0x10;
23
incoming Lab. CTC mode (output compare interrupt) Which counter ? TCNT0, TCNT1, TCNT2, TCNT3 ? Timer/Counter Control Register TCCRnA / TCCRnB / TCCRnC = ? Output Compare Register OCRnA / OCRnB / OCRnC = ? Timer/Counter Interrupt Mask Register TIMSK ?
24
incoming Lab. CTC mode (output compare interrupt) Timer/Counter Control Register TCCRnA TCCRnB TCCRnC prescale Timer/counter mode 0 0 1 1 0 1
25
incoming Lab. CTC mode (output compare interrupt) Timer/counter Mode of Operation
26
incoming Lab. CTC mode (output compare interrupt) Output Compare Register OCRnA / OCRnB / OCRnC = ? TIMSK : Timer/Counter Interrupt Mask Register
27
incoming Lab. Timer Interrupt Example #include int main(){ DDRD = 0x18; TCCR1A = 0x00; TCCR1B = 0x0D; TCCR1C = 0x00; OCR1A = 0x0708 TIMSK = 0x10; sei(); while(1); return 0; }
28
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; }
29
incoming Lab. Timer/Counter 의 Input & Output Input Counter : asynchronous pulse Timer : external clock Output Waveform PWM : Pulse Width Modulation External Input/Output I/O Port 에 배정
30
incoming Lab. Timer/Counter 의 Input & Output TCNT0 : 8bit Counter No input Output : OC0 (PB4) TCNT1 : 16bit Counter Input : T1 (PD6) Output : OC1A(PB5), OC1B (PB6), OC1C(PB7) TCNT2 : 8bit Counter Input : T2(PD7) Output : OC2 (PB7) TCNT3 : 16bit Counter Input : T3 (PE6) Output : OC3A(PE3), OC3B (PE4), OC3C(PE5)
31
incoming Lab. Timer/Count Mode normal mode : overflow interrupt clear time on compare match (CTC) mode : output compare interrupt Fast PWM Mode
32
incoming Lab. Timer/Count Mode Phase Correct PWM Mode
33
incoming Lab. DC 모터의 가감속 제어 DC 모터의 속도제어 DC 모터의 전달함수 정상 상태 속도는 입력 전압에 비례
34
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
35
incoming Lab. Pulse Width Modulation (PWM, 펄스폭 변조 ) Why PWM ? difficult to amplify a voltage with appropriate power compact and low cost means for applying adjustable power for many devices Principle of PWM 전달되는 power 는 duty cycle 에 비례 PWM frequency
36
incoming Lab. PWM in ATmega128 Fast PWM mode/Phase Correct PWM Mode TCNT0 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 Enable OC1C (PB7) : Right Motor Enable TCNT1 => PWM 으로 사용
37
incoming Lab. PWM Example PWM mode : Fast PWM 10 bit mode PWM frequency motor 와 motor driver 의 주파수 특성 : ? f clk = 7372800 Hz N(prescale) = 8 TOP = 2 10 -1
38
incoming Lab. PWM Example
39
incoming Lab. PWM Example Timer/Counter Control Register TCCRnA TCCRnB TCCRnC prescale Timer/counter mode 1 0 1 0 1 0 1 0
40
incoming Lab. PWM Example
41
incoming Lab. PWM Example #include int main(){ DDRB = 0xCF; PORTB = 0x06; TCCR1A = 0x2B; TCCR1B = 0x0A; TCCR1C = 0x00; OCR1B = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ OCR1C = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ while(1); return 0; }
42
incoming Lab. 실습 과제 좌우 모터에 대해 저속 / 중속 / 고속을 정의 전진 / 후진 / 정지를 정의 Left/Right Switch 를 이용하여 Motor Disable (Interrupt) USART 통신을 이용하여 제어 (Interrupt)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.