Counter/Timer/PWM. incoming Lab. Counter counter is a device which stores the number of times a particular event or process has occurred synchronous/asynchronous.

Slides:



Advertisements
Similar presentations
More fun with Timer/Counters
Advertisements

3 Phase Wave Generation 3 Phase waveform generation using Timer/Counter1 and 3 on atmega128.
Timers in Hardware ECE152. Overview Why are timers important – Watchdog – Task switching – Accurate time of day Can use polling or interrupts.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
ATtiny2313 Timers/Counters CS-423 Dick Steflik. What Do You Use Timers For? Timing of events (internal or external)‏ Scheduling Events Measuring the width.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Pulse Width Modulation modes
Arduino DC motor driver L298 H-Bridge Arduino GUI vs Atmel Studio
Robotics Research Laboratory Louisiana State University.
The Use of Microcontrollers
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.
Timer/counter Chapter 12
Lab 1 - Microcontrollers Complete the program template below being sure to select the correct parameters to meet the requirements (see the Microcontroller.
A Few Words From Dilbert
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
1 Lab 5: Controls and feedback. 2 Lab 5: Control and Feedback This embedded system uses the Photo sensor to detect the light intensity of the environment.
Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
AVR Programming: Timers October 8, What is a timer? A register that keeps track of a current value This value is updated at a certain frequency.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Interrupt. incoming Lab. Interrupt an asynchronous signal indicating the need for attention hardware interrupt/software interrupt call “interrupt service.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
The Cortex-M3 Embedded Systems: LM3S9B96 Microcontroller – Pulse Width Modulator (PWM) Refer to Chapter 21 in the reference book “Stellaris® LM3S9B96 Microcontroller.
Application of Interrupt and Timer : Measurement of Motor Speed.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Timers and Interrupts Mark Neil - Microprocessor Course.
Timers and Scheduled Interrupts
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
Application of Interrupt and Timer : Motor Position Control.
The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi.
충북인력개발원 전자과 공학박사 강원찬 3. Timer & Counter &PWM. uP AVR (Atmega128) PWM1=OC1A(PB5) PWM2=OC1B(PB6) T0=T1(PD6) T1=T2(PD7)
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
1 68HC11 Timer. 2 68HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output compare.
전자공학과 유승민 FND 과제. 1. 과제 내용 A~F 까지 십진수로 나타내기 이 진 수이 진 수 십진수 gfedcba A b C d E F
16-Bit Timer/Counter 1 and 3
Timers and Scheduled Interrupts
Embedded Systems Programming Interrupts and Handlers
Why are Timer Functions Important?
Servos Elementary Robotics with Servos
Outline Introduction to Arduino UNO Programming environment setup GPIO
V.V.P. ENGINEERING COLLEGE,RAJKOT
BITS EDU. CAMPUS , VARNAMA
AVR Addressing Modes Subject: Microcontoller & Interfacing
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Timer/Counter Modified from Dr. Lam Phung’s Slides.
16-bit Timer/Counter1 Features
8-bit Timer/Counter0 with PWM
ATmega103 Timer0 and Interrupts
Atmel ARM Timer Programming
DC motor and PWM.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
Timers/Counters.
Wave Generation and Input Capturing
Interrupt Chapter 10.
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

Counter/Timer/PWM

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 count 범위 : 0~2 N -1

incoming Lab. Timer timer is a specialized type of clock synchronous counter with fixed frequency clock T clk = 1/f clk DCBA T clk T clk T clk T clk T clk T clk Timer 의 최소단위 : T clk 최대 측정 시간 : (2 N -1)T clk

incoming Lab. Example counter : how many tablet ? timer : how long ?

incoming Lab. Counter/Timer in Atmega128 8-bit Timer/Counter TCNT0, TCNT2 Counting 범위 : 0~ bit Timer/Counter TCNT1H & TCNT1L, TCNT3H & TCNT3L Counting 범위 : 0~2 16 – 1 = 65535

incoming Lab. 8-bit Timer/Counter Block Diagram

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

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 예 ) TCCR0 = 0x05  f timer = f clk /128 = /128 = Hz  T timer = 1/57600

incoming Lab. 8 bit Timer/Counter Register 관련 Register TCNTn : Timer/Counter Register Overflow Interrupt 예 ) TCNT0 = 0x27  구간 = 0xFF-0x27 = 0xD8 = 216  216 * T timer 마다 인터럽트 발생이 가능함

incoming Lab. 8 bit Timer/Counter Register 관련 Register OCRn : Output Compare Register Output Compare Interrupt 예 ) OCR0 = 0xA2  구간 = 0xA2 = 162  162 * T timer 마다 인터럽트 발생이 가능함

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

incoming Lab. 16-bit Timer/Counter Block Diagram

incoming Lab. 16 bit Timer/Counter Register 관련 Register Timer/Counter Control Register TCCRnA TCCRnB TCCRnC

incoming Lab. 16 bit Timer/Counter Register 관련 Register Clock Selection Bit : CSn0 ~ CSn2 in TCCRnB

incoming Lab. 16 bit Timer/Counter Register 관련 Register TCNTnH & TCNTnL

incoming Lab. 16 bit Timer/Counter Register 관련 Register Output Compare Register OCRnAH & OCRnAH OCRnBH & OCRnBH OCRnCH & OCRnCH

incoming Lab. Timer Interrupt Example Mode : normal mode (overflow interrupt) Interrupt 간격 : 500mSec f clk = Hz  0.5 초 :  16bit counter = 2 16 – 1 = prescale : 1024  /1024 = 3600 => 0xE10  0xFFFF-0xE10 = 0xF1EF TCCR1A = 0x00; TCCR1B = 0x05; TCCR1C = 0x00; TCNT1H = 0xF1; TCNT1L = 0xEF; TIMSK = 0x04;

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; }

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. 연습 다양한 시간으로 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 간격 : 250mSec f clk = Hz  0.25 초 :  16bit counter = 2 16 – 1 = prescale : 1024  /1024 = 1800 => 0x0708 TCCR1A = 0x00; TCCR1B = 0x0D; TCCR1C = 0x00; OCR1A =0x0708; (OCR1AH = 0x07; OCR1AL = 0x08; ) TIMSK = 0x10;

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 ?

incoming Lab. CTC mode (output compare interrupt) Timer/Counter Control Register TCCRnA TCCRnB TCCRnC prescale Timer/counter mode

incoming Lab. CTC mode (output compare interrupt) Timer/counter Mode of Operation

incoming Lab. CTC mode (output compare interrupt) Output Compare Register OCRnA / OCRnB / OCRnC = ? TIMSK : Timer/Counter Interrupt Mask Register

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; }

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. 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 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)

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. DC 모터의 가감속 제어 DC 모터의 속도제어 DC 모터의 전달함수 정상 상태 속도는 입력 전압에 비례

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 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 으로 사용

incoming Lab. PWM Example PWM mode : Fast PWM 10 bit mode PWM frequency motor 와 motor driver 의 주파수 특성 : ? f clk = Hz N(prescale) = 8 TOP =

incoming Lab. PWM Example

incoming Lab. PWM Example Timer/Counter Control Register TCCRnA TCCRnB TCCRnC prescale Timer/counter mode

incoming Lab. PWM Example

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; }

incoming Lab. 실습 과제 좌우 모터에 대해 저속 / 중속 / 고속을 정의 전진 / 후진 / 정지를 정의 Left/Right Switch 를 이용하여 Motor Disable (Interrupt) USART 통신을 이용하여 제어 (Interrupt)