Time Measurement Capture Mode of the Capture/Compare/PWM module is used for time measurement. TMR1 or TMR3 16-bit value transferred to 16-bit capture.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

More fun with Timer/Counters
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
Chung-Ta King National Tsing Hua University
ECT 357 Ch 16 Output Control. Today’s Quote: The measure of a man is not how great his faith is bt how great his love is. The measure of a man is not.
Introduction of Holtek HT-46 series MCU
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.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Taking timing further Chapter Nine 9.1 – 9.8 Dr. Gheith Abandah1.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
T IMERS - 2. O UTPUT U NIT Each capture/compare block contains an output unit. The output unit is used to generate output signals such as PWM signals.
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.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Today’s Lecture List the Timers of PIC18 and their associated registers Describe the various modes of the PIC18 timers Program the PIC18 timers in Assembly.
V 0.21 Timers A timer on a  C is simply a counter The input clock frequency to a timer can be prescaled so that it is some fraction of the system clock.
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
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)
ECE 425 Timer 1 Timer Functions Two main modes: – Input capture Used for timing external events – Match (Output compare) Allows use as a simple function.
ARM Timers.
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
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.
Lecture Set 9 MCS-51 Serial Port.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller
Microprocessoren lesson 6. PWM /motor control/servo.
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
The Silicon Laboratories C8051F020
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
Microcontrollers session. What is Microcontroller? Microcontroller is composed of microprocessor,ram, flash memory,EEPROM and some modules like timers,
Special Features. Device Configuration bits Revision Device Configuration bits Revision On-chip Power-on Reset (POR) Revision On-chip Power-on Reset (POR)
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
TIMERS.
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
Application Case Study Christmas Lights Controller
Chapter 9 PIC18 Timer Programming in Assembly
Assembly Language * * * * * * * 8051 Timer
Timer and Interrupts.
Prof. Chung-Ta King Department of Computer Science
Microprocessor Systems Design I
AVR Addressing Modes Subject: Microcontoller & Interfacing
Microprocessors Timers
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
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.
ECE 3430 – Intro to Microcomputer Systems
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
Pugazhendhi.M 11/12/2018 TIMERS 11/12/2018 CCET CCET.
ECE 354 Computer Systems Lab II
8051 Timers / Counters It has two timers Timer 0 and Timer 1.
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Figure CCP1 Control Register
CS4101 Introduction to Embedded Systems Lab 2: Basic IO and Timer
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Prof. Chung-Ta King Department of Computer Science
Timers Chapter 2.
Presentation transcript:

Time Measurement Capture Mode of the Capture/Compare/PWM module is used for time measurement. TMR1 or TMR3 16-bit value transferred to 16-bit capture register on edge detect. Rising or falling edge detect, with interrupt flag set. V 0.1

Measuring pushbutton pulse width PIC18 RC2/CCP1 Pulse Width Capture TMR1 value on falling edge (Tf) in CCPR1 Capture TMR1 value on rising edge (Tr) in CCPR1 Pulse width = Tr – Tf (Elapsed Timer1 Tics) Use interrupt to save timer values. V 0.1

Computing Pulse Width © Charles River Media In overflow case, the value can be greater > 16 bits so need to use a LONG type to hold TimerDelta value. V 0.1

ISR for capturing pulse width. tmr1_ov variable keeps track of timer1 overflows. After falling edge, reconfigure for rising edge capture. After rising edge, compute delta timer tics V 0.1

V 0.1

After pulse width is captured, the capture_flag semaphore is set and the V 0.1

swdetov.c (configuration) #define FOSCQ 29491200 #define PRESCALE 2.0 #define TMR1TIC 1.0/(FOSCQ/4.0)*PRESCALE double pulse_width_float; long pulse_width; main(void){ serial_init(95,1); // 19200 in HSPLL mode ptr = (char *) &this_capture; // initialize timer 1 // prescale by 2 T1CKPS1 = 0; T1CKPS0 = 1; T1OSCEN = 0; // disable the oscillator TMR1CS = 0; //use internal clock FOSC/4 T1SYNC = 0; // set CCP1 as input bitset(TRISC,2); // enable interrupts IPEN = 0; PEIE = 1; GIE = 1; Initialize Timer1 CCP1 used as input pin. V 0.1

swdet.c (main loop) configure for falling edge, enable CCP1 interrupt, and timer1 interrupt wait for capture print pulse width V 0.1

swdet.c (main loop) First (falling) edge capture for (;;) { // loop forever err_flag = 0; CCP1CON = 0; //turn off when changing modes CCP1CON = 0x4; // capture every falling edge edge_type = 0; // looking for falling edge capture_flag = 0; TMR1IF = 0; // clear timer 1 interrupt flag TMR1IE = 1; // allow timer 1 interrupts TMR1ON = 1; // enable timer 1 while(!capture_flag) { asm("clrwdt"); //wait forever for button press } capture_flag = 0; CCP1CON = 0; /* turn off when changing modes */ CCP1CON = 0x5; /* capture every rising edge */ edge_type = 1; /* looking for rising edge */ while(!capture_flag) { asm("clrwdt"); // wait for rising edge } First (falling) edge capture Second (rising) edge capture V 0.1

swdet.c (main loop, cont.) // continued from code on previous slide if (err_flag) { // multiple overflows printf("**Timer 1 overflowed more than once! **"); pcrlf(); } else { // scale to microseconds pulse_width_float = (delta * ONE_TIC)*1.0e6; pulse_width = (long) pulse_width_float; printf ("Switch pressed, timer ticks: %ld, pwidth: %ld (us)", delta,pulse_width); pcrlf(); } } based on Fosc, pre-scale // pre=8, crystal = 7.3728 MHz HSPLL #define ONE_TIC 1.08492e-6 V 0.1

interrupt service Timer1 overflow no overflow during button press no TMR1IF? CCP1IF? yes CCP1F = 0; Get 16-bit capture value (CCPR1H:CCPR1L) overflow++; clear TMR1IF 1 (rising) 0 (falling) edge_type? last_capture = timer value overflow = -1 -1 Timer1 overflow overflow? delta_time = (0 – last_capture) + this_capture; delta = this_capture – last_capture no overflow during button press disable TMR1 interrupt return from interrupt V 0.1

Timer1 Overflow Timer1 is free running, captured at any time 0x0000 Second capture (B) First capture (A) First capture (A) Second capture (B) 0xFFFF 0xFFFF no overflow case (B > A) delta = B - A overflow case delta = (0 – A) + B Note that B can occur after ‘A’, so value can be > 0xFFFF, so need a ‘long’ for delta value!!! V 0.1

Interrupt Service pack 2 bytes into one integer. First edge volatile unsigned int last_capture, this_capture; volatile long delta; volatile signed char tmr1_ov, err_flag; timer_isr(void){ if (TMR1IF) { tmr1_ov++; // increment timer1 overflow TMR1IF = 0; } if (CCP1IF) { CCP1IF = 0; //clear capture interrupt flag this_capture = (CCPR1H << 8) | CCPR1L ; if (edge_type == 0) { last_capture = this_capture; tmr1_ov = -1; // clear timer 1 overflow count } else { pack 2 bytes into one integer. First edge V 0.1

Interrupt Service (cont.) } else { if (tmr1_ov == -1) { // no overflow delta = this_capture - last_capture ; } else { // overflowed once delta = 0 - last_capture; delta = delta + this_capture; } } /* disable timer1 interrupt */ TMR1ON = 0; // disable timer 1 TMR1IE = 0; // disable timer 1 interrupts TMR1IF = 0; // clear flag just in case } capture_flag = 1; } } no overflow case overflow case 2nd edge V 0.1

Timer1 Scaling Precaling options are 1,2,4,8. However, can be clocked by an source independent of main oscillator. V 0.1

Implementing a Clock/Calendar Use 32.768KHz crystal on T1OS0/T1OS1 pins, this crystal frequency good for accurate time keeping 32.768KHz = 32768 Hz  when 16-bit counter rolls over, then exactly 2 seconds  when counter = 0x8000, exactly 1 sec No error in time keeping, good for long term clock/calendar function. V 0.1

Intensity Based Infrared Vdd Rout (dc volt) + Vout Vref emitter receiver Vout = Vdd, IR present (Rout > Vref) Vout = 0v, IR absent (Rout < Vref) Increase in ambient light raises DC bias voltage Vref Problem: value for Vref changes depending on ambient light! time voltage Vref time V 0.1

How to block Ambient Light? amplifier Vdd DC voltage here depends only how fast transmitter is switched ~ ~ capacitor blocks DC Transmitter Receiver Open/Close switch at particular frequency switch opening/closing Transmitter Waveform V 0.1

Integrated IR Receiver Actual IR receiver a bit more complicated All of this is in here 3 2 V 0.1 Out 1 Gnd

IR Waveform Waveform produced by receiver when stimulated by a universal remote control depends on function and manufacturer. # of bits depends on IR remote function Stop Start ‘0’ ‘1’ Length of start period, ’0’ period, ‘1’ period will vary with function. ‘0’, ‘1’ waveforms may be inverted. Called space-width modulation. V 0.1

Space-Width Encoding Examples REC-80 code, Panasonic: ‘0’ period = 3T, ‘1’ period = 4T T 2T T 3T T 2T Bit Time Bit Value ‘0’ ‘1’ ‘0’ Sony code: ‘0’ period = 2T, ‘1’ period= 3T Bit Time T T 2T T T T Bit Value ‘0’ ‘1’ ‘0’ From: “A Primer on Remote Control Technology”, Innotech Systems, Inc. V 0.1

Lab #12: Decoding IR Waveform You will be assigned a function on the Universal Remote. Use the scope and determine the periods of ‘start’, ‘0’, and ‘1’ (assume a ‘0’ the short period, a ‘1’ is the long period). Using ‘swdet.c’ as a starting point, decode the first two bytes of a frame and print these two bytes out to the screen via Hyperterm. Choose a particular prescale for Timer1 to give you enough fidelity on Timer1 to distinguish ‘1’,’0’ periods. Your particular function may have LOTs of bits in a frame, just decode the first two bytes (16 bits). If your function does NOT have at least 16 bits, then get the TA to assign you a different IR remote function (or find one, and show this to the TA). V 0.1

Example Waveform assume LSB first Second Byte First Byte ‘0’ ‘0’ ‘0’ ‘1’ ‘0’ ‘1’ ‘1’ ‘1’ ‘0’ ‘1’ ‘0’ ‘0’ ‘1’ ‘1’ ‘0’ ‘0’ assume LSB first Second Byte First Byte 11101000 = 0xE8 00110010 = 0x32 Print first two bytes of frame to screen. V 0.1

Biphase Encoding In a previous example, ‘1’ and ‘0’ were distinguished by having different periods. Some Remote function/manufacturer use biphase encoding; ‘1’ and ‘0’ have same period, but use different transition in middle of the period (low-to-high or high-to-low). ‘0’ ‘0’ ‘0’ ‘0’ ‘1’ ‘1’ ‘1’ ‘1’ transition low-to-high is a ‘1’ transition high-to-low is a ‘0’ V 0.1

Experiment 12 Notes Ensure that your assigned IR Remote function does NOT use biphase encoding (must use space-width encoding). Simply measure the time between falling edges – the first period will be the start period, the periods after that will be either ‘1’ or ‘0’. Compute the number of timer tics for a ‘1’ or ‘0’, and compare what is measured. Use some slack, if you compute 2000 timer ticks for a ‘0’, and 4000 for a ‘1’, then distinguish a ‘1’ as being greater than 3000 tics. You must compute an appropriate prescale for timer1 so that your timer tics will have necessary fidelity to distinguish between ‘0’ and ‘1’. V 0.1