This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 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 35 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 35 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 35 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 35 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 35 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 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Timer0 (Chapter 13.1)Reading/Writing Timer0 in 16-bit modePulse Width Measurement: Timer0Pulse Width Measurement: Timer0 mainCapture Module Time MeasurementMeasuring pushbutton pulse widthComputing Pulse Widthswdetov.c (configuration)swdet.c (main loop)swdetov.c (main loop)Timer1 ScalingImplementing a Clock/CalendarClock/Calendar TMR1Clock/Calendar TMR1 (cont)Intensity Based InfraredHow to block Ambient Light?Integrated IR ReceiverIR WaveformSpace-Width Encoding ExamplesLab #12: Decoding IR WaveformExample WaveformSpace-width Decoding ApproachBiphase EncodingExperiment 12 NotesWriting the Space-width Decode Codebiphase decoding approachNotes on irdet_biphase.cdo_ircap()do_ircap() cont.main() while loopV 0.7 1Timer0 (Chapter 13.1)TMR0H write/read triggered by write/read to TMR0LCan be used as either 8-bit or 16-bit timer. Picture shown is 16-bit mode.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 2Reading/Writing Timer0 in 16-bit modeunsigned int tmr0_tics;char *ptr;(1) ptr = (char *) &tmr0_tics; // ptr points to LSB of tmr0_tics(2) tmr0_tics = TMR0; // this works for read(3) *ptr = TMR0L; *(ptr+1) = TMR0H; // also works for read(4) *(ptr+1) = TMR0H, *ptr = TMR0L; // wrong order for read(5) TMR0 = tmr0_tics; // wrong order for write(6) TMR0H = *(ptr+1); TMR0L= *ptr; // correct order for write(7) TMR0H = (tmr0_tics)>>8; TMR0L= (tmr0_tics & 0xFF); //ok as wellWhen reading, read TMR0L first!When writing, write TMR0L last!The statement tmr0_tics = TMR0 works for read because TMR0L, TMR0H are stored in little endian order in the file registers, and TMR0L is read first, then TMR0H by the compiler generated code.V 0.7 3Pulse Width Measurement: Timer0Copyright Thomson/Delmar Learning 2005. All Rights Reserved.Inaccurate, as timer0 tics from falling edge interrupt to ISR code are not counted. Also, timer0 tics from rising edge interrupt to ISR code are incorrectly added to total. Also, Timer0 overflow not counted.V 0.7 4Pulse Width Measurement: Timer0 mainConfiguration code before loop is not shown.This works of for human activated pushbutton time measurement, but if more accurate measurements are needed, then use the Capture module.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 5Capture Module 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.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 6Measuring pushbutton pulse widthRC2/CCP1PIC18Fxx2Pulse Width1. Capture TMR1 value on falling edge (Tf) in CCPR12. Capture TMR1 value on rising edge (Tr) in CCPR13. Pulse width = Tr – Tf (Elapsed Timer1 Tics)Use interrupt to save timer values.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 7Computing Pulse Width© Thomson/Delmar LearningIn overflow case, the value can be greater > 16 bits so need to use a LONG type to hold TimerDelta value.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 8ISR 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 ticsCopyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 9Read 16-bit capture valueCopyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 10After pulse width is captured, the capture_flag semaphore is set and the Timer0 interrupt is disabled as the pulse width has been measured. Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 11swdetov.c (configuration)#define FOSCQ 29491200#define PRESCALE 2.0#define TMR1TIC 1.0/(FOSCQ/4.0)*PRESCALEdouble pulse_width_float;long pulse_width;main(void){serial_init(95,1); // 19200 in HSPLL mode// initialize timer 1// prescale by 2T1CKPS1 = 0; T1CKPS0 = 1;T1OSCEN = 0; // disable the oscillatorTMR1CS = 0; //use internal clock FOSC/4 T1SYNC = 0;// set CCP1 as inputbitset(TRISC,2);// enable interruptsIPEN = 0; PEIE = 1; GIE = 1;Initialize Timer1CCP1 used as input pin.V 0.7 12swdet.c (main loop)configure for falling edge, enable CCP1 interrupt, and timer1 interruptwait for captureprint pulse widthV 0.7 13swdetov.c (main loop)printf("(Timer1 version) Ready for button mashing!"); pcrlf();while(1) {// configure captureCCP1IE = 0; // disable when changing modesCCP1CON = 0x0; // turnoff before changingCCP1CON = 0x4; // capture every falling edgeCCP1IF = 0; // clear CCP1IF interupt flagCCP1IE = 1; // enable capture interruptTMR1IF = 0; // clear timer 1 interrupt flagTMR1IE = 1; // allow timer 1 interruptsTMR1ON = 1; // enable timer 1capture_flag = 0;while(!capture_flag); //wait for capturepulse_width_float = (delta * TMR1TIC)*1.0e6;pulse_width = (long) pulse_width_float;printf ("Switch pressed, timer ticks: %ld, pwidth: %ld (us)",delta,pulse_width);pcrlf();}}floating point computation to convert Timer tics to microseconds.compile with –lf flag to print long variablesV 0.7 14Timer1 ScalingPrecaling options are 1,2,4,8. Can also be clocked by an source independent of main oscillator.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 15Implementing a Clock/Calendar Use 32.768KHz crystal on T1OS0/T1OS1 pins, this crystal frequency good for accurate time keeping32.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.7 16Clock/Calendar TMR1External clock source for timer1Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 17Clock/Calendar TMR1 (cont)Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 18Intensity Based InfraredVddVout = Vdd, IR present (Rout > Vref)Vout = 0v, IR absent (Rout < Vref)+timetimeIncrease in ambient light raises DC biasVrefVrefvoltage voltageRout (dc volt)VoutVrefemitter receiverProblem: value for Vref changes depending on ambient light!V 0.7 19How to block Ambient Light?amplifierOpen/Close switch at particular frequencyDC voltage here depends only how fast transmitter is switchedTransmitterWaveformswitch opening/closingVdd~~capacitor blocks DCTransmitter ReceiverV 0.7 20Integrated IR ReceiverActual IR receiver a bit more complicatedAll of this is in here123OutGndVCCV 0.7 21IR WaveformWaveform produced by receiver when stimulated by a universal remote control


View Full Document

MSU ECE 3724 - Timer0 Chapter 13.1

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download Timer0 Chapter 13.1
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 Timer0 Chapter 13.1 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 Timer0 Chapter 13.1 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?