DOC PREVIEW
UNCC ECGR 4101 - Timers and Event Counters

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Timers and Event CountersIn These Notes . . .Timer/Counter IntroductionTimer A Block DiagramHigh-Level Timer A Block DiagramTimer A Mode RegisterTimer A “Data” RegisterCount Start RegisterCounter ModeTAiMR in Event Counting ModeUp/Down FlagTrigger Select RegisterExample – Setting-up Event ModeExample – Using Event ModeTimer Mode – Measure Elapsed TimeTAiMR in Timer ModeExample – Setting-up Timer ModeExample – Using Timer ModeExtending Time Range in SoftwareExample – Timer & Event ModeWhat Registers Need to be Set?Slide 22Slide 23Pulse-Width ModulationSlide 25Timer A PWM DescriptionTimer A Mode Register for PWMOne-Shot Flag13-1Embedded SystemsTimers and Event CountersLecture 13Embedded Systems 13-2In These Notes . . . We learn the basics of the Timer/Counter peripheral–Called timers by RenesasWe examine how to set up the timers for different operation:–Timer mode–Event counting mode–Pulse Width Modulation (PWM) mode–One-shot timer modeWe then examine how to use a microcontroller in these modesEmbedded Systems 13-3Timer/Counter IntroductionCommon peripheral for microcontrollersBased on pre-settable binary counter, enhanced with configurability–Count value can be read and written by MCU–Count direction can often be set to up or down–Counter’s clock source can be selected•Counter mode: count pulses which indicate events (e.g. odometer pulses)•Timer mode: clock source is periodic, so counter value is proportional to elapsed time (e.g. stopwatch)–Counter’s overflow/underflow action can be selected•Generate interrupt•Reload counter with special value and continue counting•Toggle hardware output signal•Stop!EventsClockCurrent CountReload ValuePresettable Binary Counter ÷2 or RSPWMInterruptReloadorEmbedded Systems 13-4Timer A Block DiagramEach timer has one input, which is selectable from several different sources.Embedded Systems 13-5High-Level Timer A Block DiagramTimer A devices will be the most frequently usedFlexible – can be cascaded to create larger timers (i.e. 32 bits long)Embedded Systems 13-6Timer A Mode RegisterTo use the timer, you must set up how you wish to use it (i.e. via TA0MR). After that, the mode register has different settings depending on bits 1 and 0.Embedded Systems 13-7Timer A “Data” RegisterEmbedded Systems 13-8Count Start RegisterOnce the timer has been loaded with a value, start it counting.Embedded Systems 13-9Counter ModeCount pulses representing eventsOdometer example–Measure total distance your car has traveled–Events are wheel rotations, measured with magnetic sensors (dirt-proof!)–Distance traveled = counter value * 100.53”•Assume 16” tire radius. Tire circumference = 2r = 100.53”–Will limited range of 16 bit counter be a problem?•100.53” * 216-1 = 1247.78 miles–Yes. So need to extend range in software. •Enable overflow interrupt for the timer•Create an ISR to count overflowsEmbedded Systems 13-10TAiMR in Event Counting ModeEmbedded Systems 13-11Up/Down FlagThe default is that the timer will count down.Embedded Systems 13-12Trigger Select RegisterYou can set the trigger pulse of Timers A1 to A4Embedded Systems 13-13Example – Setting-up Event Mode#define TIME_CONFIG 0x01 /* 00000001 val to load into timer mode reg ||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||____ MR0: NO PULSE OUTPUT |||||_____ MR1: COUNT FALLING EDGES ||||_______MR2: USE UP/DOWN FLAG |||_______ MR3: = 0 IN EVENT COUNTER MODE ||________ TCK0: RELOAD TYPE |__________TCK1: BIT NOT USED */ #define CNTR_IPL 0x03 // TA2 priority interrupt level#define LED p7_2 // LED port on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 boardvoid init() { ta2 = 100; //e.g for an automated packaging line, 100 items per case // the following procedure for writing an Interrupt Priority Level follows // that as described in the M16C data sheets under 'Interrupts' _asm (" fclr i") ; // turn off interrupts before modifying IPL ta2ic |= CNTR_IPL; // use read-modify-write instruction to write IPL ta2mr = TIME_CONFIG; _asm (" fset i"); ta2s = 1; //start counting }Embedded Systems 13-14Example – Using Event Mode#pragma INTERRUPT /B TimerA2Int void TimerA2Int(void) { int delaycntr; delaycntr = 0; count++; //e.g for an automated packaging line, cnts # of cases LED = 1; while( delaycntr <0xffff) //software delay for flashing LEDdelaycntr++; LED = 0; }// initializes variables and LED port. Then does nothing but // wait for TA2 interrupts. void main (void) { int temp; count = 0; LED_PORT_DIRECTION = OUTPUT; init(); while (1);}Embedded Systems 13-15Use a fixed-frequency signal fbase as a time-baseTo measure elapsed time – automatically instead of measuring twiddle (debug) bits on an oscilloscope–Clear the timer (or read its value and subtract it out later)–Let time go by…–Read timer value (possibly subtract out start time)Examplevoid Compute_Cosine(float angle){unsigned t_start, t_stop, t_cosine;t_start = timer_current_count;compute,calculate,approximate,interpolate,complete.t_stop = timer_current_count;t_cosine = t_stop – t_start;}Gate function –Can use external signal (applied to TAiIN) to enable/disable counting–Can select counting during either high or low portion–Use MR1 and MR2 to configure–Convenient way to measure duty cycleTimer Mode – Measure Elapsed TimeWARNING:THIS CODE WILL NOT COMPILEEmbedded Systems 13-16TAiMR in Timer ModeEmbedded Systems 13-17Example – Setting-up Timer Mode#define TIME_CONFIG 0x40 /* 01000000 value to load into timer mode register ||||||||_ TMOD0,TMOD1: TIMER MODE SELECTED ||||||____ MR0: NO PULSE OUTPUT |||||_____ MR1,MR2: GATE FUNCTION NOT SELECTED |||_______ MR3: SET TO 0 IN TIMER MODE ||________ TCK0,TCK1: F DIVIDED BY 8 SELECTED */ #define CNTR_IPL 0x03 // TA0 priority interrupt level#define LED p7_2 // LED4 is connected to p7_2 on the MSV1632/62 boardvoid init() { ta0 = 15000; // 24meg xtal, div by 8, times 15000 counts-> 5msec interrupts.// the following procedure for writing an Interrupt Priority Level follows // that as described in the M16C data sheets under 'Interrupts' // Note: ta0ic is the interrupt control register, memory location x0055 _asm (" fclr i") ; //turn off interrupts before modifying IPL ta0ic |= CNTR_IPL; // use


View Full Document

UNCC ECGR 4101 - Timers and Event Counters

Documents in this Course
Load more
Download Timers and Event Counters
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Timers and Event Counters and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Timers and Event Counters 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?