CounterTimerPWM Counter counter is a device which stores










































- Slides: 42
Counter/Timer/PWM
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 count 범위 : 0~2 N-1 D C B A 0 0 0 0 1 1 0 0 1 0 2 0 0 1 1 3 0 1 0 0 4 0 1 5 0 1 1 0 6 incoming Lab.
Timer timer is a specialized type of clock synchronous counter with fixed frequency clock Tclk = 1/fclk Timer의 최소단위 : Tclk D C B A 0 0 0 0 0 1 1 Tclk 0 0 1 0 2 2 Tclk 0 0 1 1 3 3 Tclk 0 1 0 0 4 4 Tclk 0 1 5 5 Tclk 0 1 1 0 6 6 Tclk 최대 측정 시간 : (2 N-1)Tclk incoming Lab.
Example counter : how many tablet ? timer : how long ? incoming Lab.
Counter/Timer in Atmega 128 8 -bit Timer/Counter TCNT 0, TCNT 2 Counting 범위 : 0~255 16 -bit Timer/Counter TCNT 1 H & TCNT 1 L, TCNT 3 H & TCNT 3 L Counting 범위 : 0~216 – 1 = 65535 incoming Lab.
8 -bit Timer/Counter Block Diagram incoming Lab.
Basic Operation clk Counter/timer (TCNTn) prescaler fclk 8 bit / 16 bit ftimer = fclk/N Tclk = 1/fclk Ttimer = N/fclk = 1 Mhz, N(prescale) = 100 Tclk = 1 u. Sec ftimer = fclk/N = 10 k. Hz Ttimer = 100 u. Sec incoming Lab.
Basic Operation & Timer Interrupt normal mode : overflow interrupt clear time on compare match (CTC) mode : output compare interrupt incoming Lab.
8 bit Timer/Counter Register 관련 Register TCCRn : Timer/Counter Control Register 예) TCCR 0 = 0 x 05 Ø ftimer = fclk /128 = 7372800/128 = 57600 Hz Ø Ttimer = 1/57600 incoming Lab.
8 bit Timer/Counter Register 관련 Register TCNTn : Timer/Counter Register Overflow Interrupt 예) TCNT 0 = 0 x 27 Ø 구간 = 0 x. FF-0 x 27 = 0 x. D 8 = 216 Ø 216 * Ttimer 마다 인터럽트 발생이 가능함 incoming Lab.
8 bit Timer/Counter Register 관련 Register OCRn : Output Compare Register Output Compare Interrupt 예) OCR 0 = 0 x. A 2 Ø 구간 = 0 x. A 2 = 162 Ø 162 * Ttimer 마다 인터럽트 발생이 가능함 incoming Lab.
8 bit Timer/Counter Register 관련 Register TIMSK : Timer/Counter Interrupt Mask Register TOIE 0 bit : Overflow Interrupt Enable OCIE 0 bit : Output Compare Interrupt Enable ETIMSK : Extended Timer/Counter Interrupt Mask Register incoming Lab.
16 -bit Timer/Counter Block Diagram incoming Lab.
16 bit Timer/Counter Register 관련 Register Timer/Counter Control Register TCCRn. A TCCRn. B TCCRn. C incoming Lab.
16 bit Timer/Counter Register 관련 Register Clock Selection Bit : CSn 0 ~ CSn 2 in TCCRn. B incoming Lab.
16 bit Timer/Counter Register 관련 Register TCNTn. H & TCNTn. L incoming Lab.
16 bit Timer/Counter Register 관련 Register Output Compare Register OCRn. AH & OCRn. AH OCRn. BH & OCRn. BH OCRn. CH & OCRn. CH incoming Lab.
Timer Interrupt Example Mode : normal mode (overflow interrupt) Interrupt 간격 : 500 m. Sec fclk = 7372800 Hz Ø 0. 5초 : 3686400 Ø 16 bit counter = 216 – 1 = 65535 prescale : 1024 Ø 3686400/1024 = 3600 => 0 x. E 10 Ø 0 x. FFFF-0 x. E 10 = 0 x. F 1 EF TCCR 1 A = 0 x 00; TCCR 1 B = 0 x 05; TCCR 1 C = 0 x 00; TCNT 1 H = 0 x. F 1; TCNT 1 L = 0 x. EF; TIMSK = 0 x 04; incoming Lab.
Timer Interrupt Example #include <avr/io. h> #include <avr/interrupt. h> int main(){ DDRD = 0 x 18; TCCR 1 A = 0 x 00; TCNT 1 H = 0 x. F 1; TCCR 1 B = 0 x 05; TCNT 1 L = 0 x. EF; TCCR 1 C = 0 x 00; TIMSK = 0 x 04; sei(); while(1); return 0; } incoming Lab.
Timer Interrupt Service Routine ISR(TIMER 1_OVF_vect) { static char cnt = 0; // static 은 이전 값을 유지 함 cnt++; cnt %= 2; if( cnt == 0 ) PORTD &= 0 x. EF; else PORTD |= 0 x 10; TCNT 1 H = 0 x. F 1; TCNT 1 L = 0 x. EF; } incoming Lab.
연습 다양한 시간으로 lamp 구동 1초 간격으로 left motor 전진 right motor 전진 left motor 정지 left motor 후진 right motor 정지 right motor 후진 left motor 정지 right motor 정지 이하 반복 incoming Lab.
Timer Interrupt Example (II) Mode : clear time on compare match (CTC) mode (output compare interrupt) Interrupt 간격 : 250 m. Sec fclk = 7372800 Hz Ø 0. 25초 : 1843200 Ø 16 bit counter = 216 – 1 = 65535 prescale : 1024 Ø 1843200 /1024 = 1800 => 0 x 0708 TCCR 1 A = 0 x 00; TCCR 1 B = 0 x 0 D; TCCR 1 C = 0 x 00; OCR 1 A =0 x 0708; (OCR 1 AH = 0 x 07; OCR 1 AL = 0 x 08; ) TIMSK = 0 x 10; incoming Lab.
CTC mode (output compare interrupt) Which counter ? TCNT 0, TCNT 1, TCNT 2, TCNT 3 ? Timer/Counter Control Register TCCRn. A / TCCRn. B / TCCRn. C = ? Output Compare Register OCRn. A / OCRn. B / OCRn. C = ? Timer/Counter Interrupt Mask Register TIMSK ? incoming Lab.
CTC mode (output compare interrupt) Timer/Counter Control Register 0 TCCRn. A TCCRn. B 0 Timer/counter mode prescale TCCRn. C 0 1 1 0 1 incoming Lab.
CTC mode (output compare interrupt) Timer/counter Mode of Operation incoming Lab.
CTC mode (output compare interrupt) Output Compare Register OCRn. A / OCRn. B / OCRn. C = ? TIMSK : Timer/Counter Interrupt Mask Register incoming Lab.
Timer Interrupt Example #include <avr/io. h> #include <avr/interrupt. h> int main(){ DDRD = 0 x 18; TCCR 1 A = 0 x 00; OCR 1 A = 0 x 0708 TCCR 1 B = 0 x 0 D; TCCR 1 C = 0 x 00; TIMSK = 0 x 10; sei(); while(1); return 0; } incoming Lab.
Timer Interrupt Service Routine ISR(TIMER 1_COMPA_vect) { static char cnt = 0; // static 은 이전 값을 유지 함 cnt++; cnt %= 2; if( cnt == 0 ) PORTD &= 0 x. EF; else PORTD |= 0 x 10; } 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에 배정 incoming Lab.
Timer/Counter의 Input & Output TCNT 0 : 8 bit Counter No input Output : OC 0 (PB 4) TCNT 1 : 16 bit Counter Input : T 1 (PD 6) Output : OC 1 A(PB 5), OC 1 B (PB 6), OC 1 C(PB 7) TCNT 2 : 8 bit Counter Input : T 2(PD 7) Output : OC 2 (PB 7) TCNT 3 : 16 bit Counter Input : T 3 (PE 6) Output : OC 3 A(PE 3), OC 3 B (PE 4), OC 3 C(PE 5) incoming Lab.
Timer/Count Mode normal mode : overflow interrupt clear time on compare match (CTC) mode : output compare interrupt Fast PWM Mode incoming Lab.
Timer/Count Mode Phase Correct PWM Mode 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 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 incoming Lab.
PWM in ATmega 128 Fast PWM mode/Phase Correct PWM Mode TCNT 0 Ø OC 0 (PB 4) TCNT 1 Ø OC 1 A(PB 5), OC 1 B (PB 6), OC 1 C(PB 7) TCNT 2 Ø OC 2 (PB 7) TCNT 3 : 16 bit Counter Ø OC 3 A(PE 3), OC 3 B (PE 4), OC 3 C(PE 5) OC 1 B (PB 6) : Left Motor Enable OC 1 C (PB 7) : Right Motor Enable TCNT 1 => PWM으로 사용 incoming Lab.
PWM Example PWM mode : Fast PWM 10 bit mode PWM frequency motor와 motor driver의 주파수 특성 : ? fclk = 7372800 Hz N(prescale) = 8 TOP = 210 -1 incoming Lab.
PWM Example incoming Lab.
PWM Example Timer/Counter Control Register TCCRn. A TCCRn. B 1 0 1 1 Timer/counter mode prescale TCCRn. C 0 1 0 incoming Lab.
PWM Example incoming Lab.
PWM Example #include <avr/io. h> int main(){ DDRB = 0 x. CF; PORTB = 0 x 06; TCCR 1 A = 0 x 2 B; OCR 1 B = 0 x 00; OCR 1 C = 0 x 00; TCCR 1 B = 0 x 0 A; TCCR 1 C = 0 x 00; /* 원하는 속도를 기입 0 x 000 ~ 0 x 3 FF */ while(1); return 0; } incoming Lab.