تایمر در میکروکنترلرهای AVR. تایمرها در میکروکنترلرهای AVR تایمر در حقیقت یک شمارنده است که پالس ورودی را می شمارد. در صورتی که پالس ورودی کنتور از پالس.

Slides:



Advertisements
Similar presentations
More fun with Timer/Counters
Advertisements

COMP3221: Microprocessors and Embedded Systems Lecture 16: Interrupts II Lecturer: Hui Wu Session 2, 2005.
معاونت درمان امور مامایی اردیبهشت 90. برای ثبت اطلاعات در برنامه نرم افزاری نظام مراقبت مرگ پریناتال ابتدا لازم است برنامه نرم افزار info-path وپرنیان.
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.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Pulse Width Modulation modes
فایل پردازی در C File based Programming in C. انواع فایل متنی –سرعت بالا –حجم کمتر –امکان دسترسی تصادفی –حفظ امنیت داده ها دودویی (باینری) –امکان باز.
The Use of Microcontrollers
ARM Session , Spring Copyright © 2012 Mohammad Moallemi.
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.
Timer/counter Chapter 12
1 Network Address Translation (NAT). 2 Private Network شبکه خصوصی شبکه ای است که بطور مستقیم به اینترنت متصل نیست در یک شبکه خصوصی آدرس های IP به دلخواه.
A Few Words From Dilbert
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
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.
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.
نوشتن كد برنامه در AVR STUDIO. شروع برنامه قسمت داده :.DSEG.ORG 0X60.DEF TEMP,R16.EQU LENGTH,10.SET KEY_PORT,PORTC.STRING:.BYTE 5.
Input / Output (Peripheral) Interfacing
فصل سوم. 2 Phases of C++ Programs: 1.Edit 2.Preprocess 3.Compile 4.Link 5.Load 6.Execute Program is created in the editor and stored on disk. Preprocessor.
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.
فصل 4 پرتهای ورودی و خروجی. برای هر پورت سه ثبات 8 بیتی در منطقه رجیسترهای I/O وجود دارد که بعنوان مثال برای پورت A این ثبات ها عبارتند از:
Timers and Interrupts Mark Neil - Microprocessor Course.
Timers and Scheduled Interrupts
Stepper Motor Configuration
PORT(I/O) Configuration
Seven Segment Configuration Lecturer: Reza Arjmandi Autumn 2015 Lecture 5: Introduction 7Segment Configuration.
برای هر پورت سه آدرس در حافظهSRAM یا سه ثبات ورودی خروجی I/O 8 بیتی تعریف میشود که عبارتند از: 1- یک ثبات برای نوشتن و خواندن داده به نام PORTX (Port.
충북인력개발원 전자과 공학박사 강원찬 3. Timer & Counter &PWM. uP AVR (Atmega128) PWM1=OC1A(PB5) PWM2=OC1B(PB6) T0=T1(PD6) T1=T2(PD7)
Interrupt Control Unit Lecturer: Reza Arjmandi Autumn 2015 Lecture 10, 11: Introduction Interrupt Concept, interrupt control unit and External Interrupt.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
Lecturer: Reza Arjmandi Autumn 2015
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
Timer Counter 1 Lecturer: Reza Arjmandi Autumn 2015 Lecture 14, 15: Introduction Timer/Counter1 Registers, initialization.
Analog to Digital Converter(ADC) Lecturer: Reza Arjmandi Autumn 2015 Lecture 18: Introduction ADC Unit and initialization.
آشنايي با سيستم اعداد.
Timers and Scheduled Interrupts
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.
Session 7,8.
[c.
COMP2121: Microprocessors and Interfacing
Timer/Counter Modified from Dr. Lam Phung’s Slides.
8-bit Timer/Counter0 with PWM
ATmega103 Timer0 and Interrupts
Session 6.
هیدروگراف(Hydrograph) تهیه : دکتر محمد مهدی احمدی
بنام خدا زبان برنامه نویسی C (21814( Lecture 12 Selected Topics
SY800 router mode [AD-14-TB ].
ساختمان داده‌ها الگوریتمهای کوتاهترین مسیر
پيکره بندی تايمر / کانترها
مدار منطقی Logic Circuits
کوئیز از جلسه قبل) کارخانه ای در حال خرید قطعه‌ای برای یکی از ماشین‌آلات خود می باشد اگر نرخ بهره 10% برای محاسبات فرض شود، دو مدل از قطعه ماشین در دسترس.
DC motor and PWM.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
دینامیک سیستمهای قدرت مدرس: دکتر مهدی بانژاد
بسم الله الرحمن الرحیم هرس درخت تصمیم Dr.vahidipour Zahra bayat
فصل ششم مدارهای ترتیبی.
Uniprocessor Scheduling
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
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:

تایمر در میکروکنترلرهای AVR

تایمرها در میکروکنترلرهای AVR تایمر در حقیقت یک شمارنده است که پالس ورودی را می شمارد. در صورتی که پالس ورودی کنتور از پالس ساعت میکروکنترلر تامین شود تایمر نام دارد چون در این صورت زمان پالس ورودی کنتور مشخص است لذا عدد شمارش شده در کنتور متناسب با زمان است به عبارت دیگر کنتور زمان را اندازه گیری میکند.

وقفه در میکروکنترلرهای AVR

تایمر 0 در Atmega 8

ثبات تایمر TCNT0(Timer/Counter 0)

ثبات کنترل TCCR0 (Timer/Counter 0 Control Register)

ثبات پرچم وقفه تایمر TIFR (T/C Interrupt Flag Register) بیت 0- بیت سرریز تایمر 0 TOV0(T/C0 Overflow Flag ) زمانی که کنتور پر شد و مقدار آن از به 0 رسید آن وقت بیت سرریز تایمر 0 یعنی TOV0 برابر 1 میگردد و باعث یک وقفه میشود که در صورت اجرای روتین وقفه این بیت به طور اتوماتیک 0 میگردد. البته با دستورات میکروکنترلر می توان در این بیت 1 نوشت تا این بیت Clear گردد.

ثبات فعال کردن وقفه تایمرها TIMSK(T/C Interrupt Mask Register) بیت 0- بیت فعال کردن وقفه تایمر 0 TOIE0(T/C0 Overflow Interrupt Enable ) اگربا دستورات میکروکنترلر این بیت را 1 کنیم وقفه سرریز تایمر 0 فعال میشود که در صورت وجود سر ریز یعنی با 1 شدن بیت سرریز TOV0 در ثبات پرچم TIFR روتین سرویس وقفه اجرا میشود.

دستوراتی بنویسید که پالس ساعت تایمر 0 برابر پالس ساعت میکروکنترلر ATmega8 شود و در ضمن وقفه نیز فعال گردد. حل: ldi r16, 1<<CS00 out TCCR0, r16 ldi r16, 1<<TOIE0 out TIMSK, r16 sei

ساختار برنامه اسمبلی با سابروتین و روتین وقفه ISR ; nemuneh3.asm ;************************************************* ;A Section ;AVR number :atmega8 ;File name:nemuneh3.asm ;Description:This is a sample assembly with ; subroutine & Interrupt ;************************************************* ;B Section.include "m8def.inc" ;(1)This header file ; ; is needed for all avr ; ; atmega8 assembly language ;Interrupt vectors table.org0x00 ;Reset address rjmp Reset ;(2)Jump to reset ;.org 0x009 rjmp ISR ;(3) Interrupt vector,jump ; ;to interrupt service routin for timer 0 ;overflow ;************************************************* ;C Section ;*******Main program entry point on reset reset: ;*******Initialization of the stack pointer ldi r16,high(RAMEND);(4) out SPL,r16;(5) ldi r16,low(RAMEND);(6) out SPH,r16;(7)

;*******Input output ports initialization ;PORTB initialization (if it is used) ;PORTC initialization (if it is used) ;PORTD initialization (if it is used) ;*******Initialize others I/O devices like: ; timer0, timer1, timer2, watchdog timer, ; external interrupt,ADC, Analog comparator... ; (if they are used). ;********golbal interrupt enable ; ;************************************************* ;D Section ;*****loopforever loopforever: ;Insert instruction here rjmp loopforever;(8) ;************************************************* ;E Section ;*******Write subroutine or proccedrure here SUB: ;Insert instructions RET;(9) ;*******Interrupt service routine ISR: ;Insert instructions here RETI;(10) ;*************************************************

مثال : برنامه ای به زبان C در محیط CodeVision بنویسید که هر بار تایمر 0 پر شد تقاضای وقفه کند و در روتین سرویس وقفه بیت 0 پورت B را معکوس کند. در ضمن فرکانس تایمر برابر 1/1024 فرکانس میکروکنترلر است.

#include //(1) #include //(2) // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { PORTB.0=!PORTB.0;//(3) } void main(void) { // Port B initialization, as ouput DDRB=0xFF; //(5) PORTB=0x00; //(6) // Timer/Counter 0 initialization // Clock value: kHz 1MHZ/1024 TCCR0=0x05; b //(7) TCNT0=0x00; //(8) // Timer0 interrupt enable TIMSK=0x01; //(9) // Global enable interrupts #asm("sei") //(10) while (1) //(11) { }; }

پالس PWM ( Pulse Width Modulation ) برای کنترل موتورها و سیستم های سوئچینگ مورد استفاده قرار میگیرد.

روشهای تولید پالس PWM الف: روش Fast PWM

ب: روش Phase Correct PWM

تایمر 0 در میکروکنترلرهای AVR Atmega 16, 32

حالتهای مختلف تایمر 0 (atmeg 16, 32): حالت معمولی حالت Fast PWM حالت Phase Correct PWM حالت CTC حالت معمولی : ساده ترین حالت تایمر 0 حالت معمولی و پیش فرض آن است که تایمر 0 با هر پالس از مقدار اولیه 0 به طور صعودی می شمارد تا به مقدار Max یا TOP یعنی 0xFF برسد و با پالس بعدی تایمر از ماکزیمم به Bottom یعنی به 0 میرود که در این صورت بیت پرچم سر ریز TOV0 در ثبات پرچم TIFR برابر 1 میگردد و تایمر مجددا از 0 به طور صعودی می شمارد و بالا می رود. مثال: برنامه ای به زبان اسمبلی بنویسید که هر بار تایمر 0 پر شد با استفاده از وقفه سر ریز، بیت 0 پورت B معکوس شود.

;************************************************** ;A Section ;* Number : atmega16 ;* File Name : "timer0-interrupt-atmega16.asm" ;* Title : Setup and Use Timer0 ;************************************************** ;B Section.include "m16def.inc" ; Interrupt Vectore Table.org0x00;1- Reset-Address `rjmpReset.org0x012;2- Timer overflow vector rjmpISR_TOV0 ;************************************************** ;C Section ; *** Begin of Program Execution *** Reset: ; Initialization of the Stack Pointer ldir16,low(RAMEND) ;3- initialize outSPL,r16 ;4- SPL ldir16,high(RAMEND) ;5- initialize out SPH,r16 ;6- SPH ;initialization of timer 0 ;initialize timer0 ldi r16,1<<CS00;7-timer clock = out TCCR0,r16 ;8-system clock

ldi r16,1<<TOIE0 ;9- enable timer out TIMSK,r16 ;10-interrupt overfow0 ; Set Port B as output ser r16 ;11- r16=0FFh out DDRB,r16;12- DDRB=0FF sei ;13- Global Interrupts enabled ;************************************************** ;D Section ; Endlessloop loopforever: rjmploopforever;14- wait for interrupt ;************************************************** ;E Section ;Interrupt service routine ISR_TOV0, ; is executed each time that overflow occured, ; that is TOV0 become 1, complement PB0 each ; time ISR_TOV0: ldi r19,1 ;15- r19=1 in r16,PORTB ;16- r16= PORTB eor r16,r19 ;17- complement ;change PB0 in 256 clock (for timer0 overflow) ;plus,time of executing of ISR. out PORTB,r16 ;18-PORTB=r16 reti ;19- return from interrupt

مثال : برنامه ای به زبان C در محیط CodeVision بنویسید که در لبه بالارونده هر پالس ورودی در T0 (PB0) تایمر 0، یک شماره بیندازد ودر هر لحظه خروجی تایمر 0، TCNT0 را به پورت D ارسال کند.

#include //(0) void main(void) { // Port B initialization, as input PORTB=0xFF;//(1) DDRB=0x00; //(2) // Port D initialization, as output PORTD=0x00;//(3) DDRD=0xFF; //(4) // Timer/Counter 0 initialization // Clock source: T0 pin Rising Edge // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x07; //(5) TCNT0=0x00; //(6) while (1) //(7) { PORTD=TCNT0;//(8) }; }

تولید پالس PWM در تایمر 0:

تولید پالس PWM به صورت Fast PWM در تایمر 0:

) فرکانس پالس میکروکنترلر)/ (256* N ) = پرید PWM *100( OCR0/255 ) %= Duty Cycle

ثبات پرچم وقفه تایمر 0 TIFR : بیت 0: بیت پرچم سرریز TOV0 : بیت 1: بیت پرچم تساوی Compare Match : OCF0

ثبات فعال کردن وقفه در تایمرها TIMSK : بیت 0: فعال کردن وقفه در صورت سر ریز تایمر 0 TOIE0 : بیت 1: فعال کردن وقفه در صورت اعلام تساوی Compare Match به نام OCIE0

تولید پالس pwm به صورت Phase Correct در تایمر 0: ) 510* N )/ ( فرکانس پالس میکروکنترلر ) = فرکانس PWM

تولید پالس ساعت تایمر، از طریق تقسیم کننده فرکانس با Prescaler

دیاگرام زمانی تایمر 0

مثال : برنامه ای به زبان اسمبلی بنویسید که با تایمر 0 ، پالس PWM در خروجی OC0 و با روش Fast PWM تولید کند. مقدار اولیه OCR0=0x80 و فرکانس تایمر 0، برابر فرکانس میکروکنترلر است.

;********************************************************* ;A Section ;Testing fastpwm Modes in atmega16.include "m16def.inc" ;Interrupt vector tables.org 0x00;Reset_address rjmpReset;1-jump reset ;********************************************************* ;B Section ; begin of main program reset: ;initialize fast pwm, non_inverted,clock = system clock ldi r16,(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00) ;2-fast out TCCR0,r16;3-pwm ;set compare value or duty cycle ratio of output pulse OC0 ldi r16,0x80;4-OCR0= out OCR0,r16;5-0x80 ;set pB3 or OC0 as output ser r16;6-DDRB= out DDRB,r16 ;7-0xFF ;********************************************************* ;C Section ;endlessloop for generating output pulse on OC0 loopforever: rjmp loopforever;8-do it again

مثال : برنامه ای به زبان C بنویسید که تایمر0 ، پالس PWM در خروجی OC0 و با روش Fast PWM تولید کند. مقدار اولیه OCR0=0x80 و فرکانس تایمر 0، برابر فرکانس میکروکنترلر است.

/**************************************************** * A Section Chip type : ATmega16 Program type : Application Clock frequency : MHz ***************************************************** / //B Section #include //(0) void main(void) { // Input/Output Ports initialization // Port B initialization

PORTB=0x00;//(1) DDRB=0x08; //(2) // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: kHz // Mode: Fast PWM top=FFh // OC0 output: Non-Inverted PWM TCCR0=0x69; //(3) TCNT0=0x00; //(4) OCR0=0x80; //(5) while (1) //(6) { }; } ) 256* N )/ ( فرکانس پالس میکروکنترلر ) = فرکانس PWM *100( OCR0/255 ) %= زمانی که پالس PWM برابر 1 میباشد

مثال : برنامه ای به زبان C بنویسید که با تایمر0 ، پالس PWM در خروجی OC0 و با روش Phase Correct تولید کند. مقدار اولیه OCR0=0x80 و فرکانس تایمر 0، برابر فرکانس میکروکنترلر است.

#include //(0) void main(void) { PORTB=0x00;//(1) DDRB=0x08; //(2) // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: kHz // Mode: Phase correct PWM top=FFh // OC0 output: Non-Inverted PWM TCCR0=0x61;//(3) TCNT0=0x00; //(4) OCR0=0x80; //(5) while (1)//(6) { }; } ) 510* N )/ ( فرکانس پالس میکروکنترلر ) = فرکانس PWM

تولید پالس با روش CTC در تایمر0 CTC: Clear Timer on Compare Match

دیاگرام زمانی تایمر 0 در حالت CTC

مثال : برنامه ای به زبان C بنویسید که تایمر0 ، در حالت CTC پالس مربعی در خروجی OC0 تولید کند. مقدار اولیه OCR0=0x80 می باشد. با استفاده از وقفه Compare Match مقدار ثبات OCR0 را برابر مقدار پورت D قرار دهید تا فرکانس پالس خروجی متناسب با آن تغییر نماید.

#include //(1) unsigned char temp; //(2) // Timer 0 output compare interrupt service routine interrupt [TIM0_COMP] void timer0_comp_isr(void) { OCR0=temp; //(3) } void main(void) { // Input/Output Ports initialization // Port B initialization

PORTB=0x00;//(4) DDRB=0x08; //(5) // Port D initialization PORTD=0xFF;//(6) DDRD=0x00; //(7) // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: kHz // Mode: CTC top=OCR0 // OC0 output: Toggle on compare match TCCR0=0x1D;//(8) TCNT0=0x00; //(9) OCR0=0x80; //(10) // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x02;//(11) // Global enable interrupts #asm("sei") //(12) while (1) { temp=PIND;//(13) }; }