Application of Interrupt and Timer : Measurement of Motor Speed.

Slides:



Advertisements
Similar presentations
More fun with Timer/Counters
Advertisements

C Examples 1.
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.
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.
Lecture 9. - Synchronous Devices require a timing signal. Clock generated Interval Timer Microprocessor Interval Timer Clk PCLK = MHz PCLK (for.
ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)
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
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
Robotics Research Laboratory Louisiana State University.
Robotics Research Laboratory Louisiana State University.
FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller
C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
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.
Robotics Research Laboratory Louisiana State University.
AVR Programming: Interrupts September 17, What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.
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.
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
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.
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
Real Time Interrupts Section Real-Time Interrupt (RTI) Most operating systems (OS) require an interrupt every T seconds by the RTI RTI 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)
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.
Chap.6.3 UART.
전자공학과 유승민 FND 과제. 1. 과제 내용 A~F 까지 십진수로 나타내기 이 진 수이 진 수 십진수 gfedcba A b C d E F
16-Bit Timer/Counter 1 and 3
Timers and Scheduled Interrupts
Servos Elementary Robotics with Servos
Outline Introduction to Arduino UNO Programming environment setup GPIO
V.V.P. ENGINEERING COLLEGE,RAJKOT
BITS EDU. CAMPUS , VARNAMA
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
AVR Addressing Modes Subject: Microcontoller & Interfacing
Pulse Width Modulation (PWM) Motor Feedback - Shaft Encoder
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.
8-bit Timer/Counter0 with PWM
ATmega103 Timer0 and Interrupts
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
UART Protocol Chapter 11 Sepehr Naimi
DC motor and PWM.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
ADC and DAC Programming in AVR
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:

Application of Interrupt and Timer : Measurement of Motor Speed

incoming Lab. Rotary Encoder A ( rotary) encoder is an electro-mechanical device that converts the angular position of a shaft or axle to digital code or digital pulse absolute/ incremental

incoming Lab. DC motor

incoming Lab. Encoder Incremental Encoder 13 pulses/round/channel 1 pulse = 360/13 도 (motor), 360/50/13 도 (wheel) Motor Speed  rpm (round per minute) –rad/sec = rpm*2pi/60  102 rpm (wheel) = 5100 rpm (motor) direction  A/B channel 의 위상을 비교 A Ch. B Ch. A Ch. B Ch.

incoming Lab. DC Motor RB-35GM 제어전압 : DC 12V 감속비 [Reduction ration] : 1/50 정격토크 [Rated torque](Kg-cm): 2.0 정격회전수 [Rated speed](rpm):102 Rated torque 60 g-cm, 5100 rpm, current 470 mA 이하, 3.14 W No load speed 6200 rpm, No load current 120 mA 이하

incoming Lab. Measurement of motor speed (I) pulse 간의 시간 측정 Example)

incoming Lab. Measurement of motor speed (I) pulse 간의 시간 측정 예 ) f clk = 1 Mhz, N(prescale) = 100  f timer = f clk /N=10kHz, T timer = 100uSec  timer_diff = 100 =>  10 mSec timer_diff

incoming Lab. Measurement of motor speed (I) External Interrupt INT 5 : Left motor A channel (PE5) INT 7 : Right motor A channel (PE7) external interrupt 간 timer 비교 EICRB = 0x88; EIMSK = 0xA0; A Ch. tcnt1tcnt2 timer_diff = tcnt2 – tcnt1

incoming Lab. 바퀴구동체 도면

incoming Lab. External interrupt Setting EIMSK : external Interrupt MaSK register EICRB

incoming Lab. Measurement of motor speed (I) timer for external interrupt TCNT0 : 8 bit timer/counter TCNT1 : Motor Speed Control TCNT2, TCNT3 : same prescaler with TCNT1 normal mode no timer interrupt f clk = Hz & prescale : 256  f timer = /256 =  T timer = 1/28800 = uSec  예상 ) 102 rpm => uSec => 26 (= timer_diff ) TCCR0 = 0x06;

incoming Lab. Measurement of motor speed (I) TCCRn : Timer/Counter Control Register 1 1 0

incoming Lab. Interrupt Service Routine (INT5, Left Motor) ISP(INT5_vect) { static unsigned char timer_cur = 0, timer_prev = 0; timer_cur = TCNT0; Left_timer_diff = timer_cur – timer_prev; timer_prev; = timer_cur; if ( (PINE & 0x10) == 0 ) Dir_L = 0; // forward else Dir_L = 1; // backward }

incoming Lab. Speed Measurement Program (I) #include void txd_char(unsigned char data){ while( (UCSR0A & 0x20) == 0 ); UDR0 = data; } unsigned char Left_timer_diff, Right_timer_diff; unsigned char Dir_L, Dir_R; int main(){ unsigned char i, time_diff; unsigned char text[] = "\r Left : ";

incoming Lab. Speed Measurement Program (I) DDRB = 0xCF; DDRD = 0x18; DDRE = 0x01; PORTB = 0x01; /* 원하는 방향을 기입 */ EICRB = 0x08; // EICRB = 0x88; EIMSK = 0x20; // EIMSK = 0xA0; TCCR1A = 0x2B; TCCR1B = 0x0A; TCCR1C = 0x00; OCR1B = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ // OCR1C = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ TCCR0 = 0x06; EIMSK = 0x20; /* Left interrupt 만 enable */ sei();

incoming Lab. Speed Measurement Program (I) while(1){ i=0; while(text[i] != '\0') txd_char(text[i++]); if( Dir_L == 1 ) txd_char('+'); // 부호 출력 else txd_char('-'); time_diff = Left_timer_diff; // 임시 저장 txd_char(time_diff/100 + '0'); // 백의 자리 출력 txd_char((time_diff/10)%10 + '0'); // 십의 자리 출력 txd_char(time_diff%10 + '0'); // 일의 자리 출력 txd_char('\r'); return 0; }

incoming Lab. 연습 Right Motor 에 대해 속도 측정 code 완성 Left/Right motor 에 대해 다양한 속도에 대한 속도측정 RPM 단위로 출력 (UART)

incoming Lab. Measurement of motor speed (I)

incoming Lab. Interrupt Service Routine (INT5, Left Motor) ISR(INT5_vect) { static timer_cur = 0, timer_prev = 0; timer_cur = TCNT0; if (timer_cur >= timer_prev) Left_timer_diff = timer_cur – timer_prev; else Left_timer_diff = 255- timer_prev+timer_cur+1; timer_prev; = timer_cur; if ( (PINE & 0x20) == 0 ) Dir_L = 0; // forward else Dir_L = 1; // backward }

incoming Lab. Interrupt Service Routine (INT5, Left Motor) ISR(INT5_vect) { Left_timer_diff = TCNT0; TCNT0 = 0; if ( (PINE & 0x20) == 0 ) Dir_L = 0; // forward else Dir_L = 1; // backward }

incoming Lab. Measurement of motor speed (II) 일정한 시간 간격 동안 pulse 의 개수 측정 Example)

incoming Lab. Measurement of motor speed (II) clear time on compare match (CTC) mode (output compare interrupt) TCNT0 : 8 bit timer/counter f clk = Hz & prescale : 1024  f timer = /1024 = 7200  T timer = 1/7200  OCR0 = 144 => 144*T timer = 1/50 = 20 mSec 마다 interrupt 발 생  예상 ) 102 rpm => uSec => 22 개 pulse TCCR0 = 0x0F; TIMSK = 0x02;

incoming Lab. Measurement of motor speed (II) TCCRn : Timer/Counter Control Register TIMSK : Timer/Counter Interrupt Mask Register 1 1

incoming Lab. Interrupt Service Routine (INT5, Left Motor) ISP(INT5_vect) { Pulse_L++; if ( (PINE & 0x20) == 0 ) Dir_L = 0; // forward else Dir_L = 1; // backward }

incoming Lab. Interrupt Service Routine (Timer0) ISR(TIMER0_COMP_vect) { static unsigned char no_timer = 0; no_timer++; if ( no_timer >= 10 ) { // 20*10 = 200 mSec no_timer = 0; No_Pulse_L = Pulse_L; No_Pulse_R = Pulse_R; Pulse_L = Pulse_R = 0; }

incoming Lab. Interrupt Service Routine (Timer0) – 20mSec ISR(TIMER0_COMP_vect) { No_Pulse_L = Pulse_L; No_Pulse_R = Pulse_R; Pulse_L = Pulse_R = 0; }

incoming Lab. Speed Measurement Program (II) #include void txd_char(unsigned char data){ while( (UCSR0A & 0x20) == 0 ); UDR0 = data; } unsigned char Pulse_L, Pulse_R, No_Pulse_L, No_Pulse_R; unsigned char Dir_L, Dir_R; int main(){ unsigned char cmd, i=0, no_pulse_l; unsigned char text[] = "\r Left : ";

incoming Lab. Speed Measurement Program (II) /* UART0 초기화 추가 */ DDRB = 0xCF; DDRD = 0x18; DDRE = 0x01; PORTB = 0x01; /* 원하는 방향을 기입 */ EICRB = 0x08; // EICRB = 0x88; EIMSK = 0x20; // EIMSK = 0xA0; TCCR1A = 0x2B; TCCR1B = 0x0A; TCCR1C = 0x00; OCR1B = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ // OCR1C = 0x00; /* 원하는 속도를 기입 0x000 ~ 0x3FF */ OCR0 = 144; TCCR0 = 0x0F; /* CTC mode prescale = 1024 */ TIMSK = 0x02; /* Timer Interrupt */ sei();

incoming Lab. Speed Measurement Program (II) while(1){ i=0; while(text[i] != '\0') txd_char(text[i++]); if( Dir_L == 1 ) txd_char('+'); // 부호 출력 else txd_char('-'); no_pulse_l = No_Pulse_L; // 임시 저장 txd_char(no_pulse_l /100 + '0'); // 백의 자리 출력 txd_char((no_pulse_l /10)%10 + '0'); // 십의 자리 출력 txd_char(no_pulse_l %10 + '0'); // 일의 자리 출력 txd_char('\r'); return 0; }

incoming Lab. 연습 Right Motor 에 대해 속도 측정 code 완성 Left/Right motor 에 대해 다양한 속도에 대한 속도측정 RPM 단위로 출력 (UART)