Escolar Documentos
Profissional Documentos
Cultura Documentos
http://class.ece.iastate.edu/cpre288
http://class.ece.iastate.edu/cpre288
Announcements
Homework 5 is due Friday Exam 2 is Monday, June 18th
http://class.ece.iastate.edu/cpre288
INPUT CAPTURE
http://class.ece.iastate.edu/cpre288
Input Capture
Capture the times of events
Generally, any input that can be treated as a series of events, where the precise measure of event times is important
http://class.ece.iastate.edu/cpre288
TCNTx: Timer/Counter ICRx: Input Capture Reg ICPx: Input Capture Pin x is 1 or 3 for timer/counter 1 and timer/counter 3
CPU
Input Capture
An event is a transition of binary signal Example: How many events make up the following waveform?
http://class.ece.iastate.edu/cpre288
Input Capture
An input digitalized and then times captured
Example: The input is understood as events occurring at the following times: 220, 221, 223, 226, and 227 with initial state as low
http://class.ece.iastate.edu/cpre288
Application: Speedometer
How to detect the speed of a treadmill?
L 2r
Microcontroller
http://class.ece.iastate.edu/cpre288
10
http://class.ece.iastate.edu/cpre288
11
0. 6 ms
1.2 ms
0. 6 ms
m 2.4 s
mo Re
te
Handh Infrare
The examples here will rely on the protocol that universal remotes use to control SONY http://class.ece.iastate.edu/cpre288 12 puls protocol strobes the IR thirteen times with roughly a ha lf-millisecond rest between each
2.4ms
0.6ms
1.2ms
0.6ms
http://class.ece.iastate.edu/cpre288
13
Time value (clock count) is captured first then read by the CPU
TCNT
Interrupt To CPU
ICR
Edge Detector
Noise Canceller
http://class.ece.iastate.edu/cpre288
14
Interrupt
http://class.ece.iastate.edu/cpre288
15
http://class.ece.iastate.edu/cpre288
16
Sonar Principle
Sound Speed in Lab Temperature: About 340m/s Pulse width proportional to round-trip distance
17
Sonar Principle
Assume 62.5KHz Input Capture clock 1ms <=> 62.5 clocks <=> 34cm
Time Diff.
2ms
4ms
125
250
0.34m
0.68m
http://class.ece.iastate.edu/cpre288
18
ATmega128 16-bit Timer/Counter as Input Capture Unit ATMega128 has two, multi-purpose 16-bit timer/counter units
One input capture unit (IC) Three independent output compare units (OC) Pulse width modulation output (PWM) Frequency generator And other features
http://class.ece.iastate.edu/cpre288
19
TCNTx: Timer/Counter ICRx: Input Capture Reg ICPx: Input Capture Pin x is 1 or 3 for timer/counter 1 and timer/counter 3
CPU
20
http://class.ece.iastate.edu/cpre288
21
}
Notes:
Use Interrupt to process input capture events Read captured time from ICRx (x is 1 or 3)
http://class.ece.iastate.edu/cpre288
22
http://class.ece.iastate.edu/cpre288
23
PD4 = 0
Send trigger
Disable input Capture (or disable IC interrupt, but must make sure to clear IC flag, before reenabling IC interrupt)
Catch rising edge (store ICR1 in a var) Enable input Capture (or reenable IC interrupt)
Remember only one pin (i.e PD4) used to communicate with the PING))) sensor
http://class.ece.iastate.edu/cpre288
24
http://class.ece.iastate.edu/cpre288
25
http://class.ece.iastate.edu/cpre288
26
COM: Compare Output Mode We dont care COM bits at this moment set them to zero in lab 7 WGM: Waveform Generator Mode To select Timer/Counter function. Four bits in total (WGM33 and WGM32 in TCCR3B) To use Input Capture: WGM33 = 0, WGM32 = 0, WGM31 = 0, WGM30 = 0
http://class.ece.iastate.edu/cpre288
27
ICNC3: Input Capture Noise Canceller, requires four-cycle duration for an event; use it in lab 7 ICES3: Input Capture Edge Select Which edge will trigger the capture? 0 for falling edge, 1 for rising edge WGM32, WGM32: See previous slide
http://class.ece.iastate.edu/cpre288
28
CS3x: Clock Select bits Table in ATmega128 User Guide, page 137
http://class.ece.iastate.edu/cpre288
29
30
Write 0s to those bits in lab 7 or dont write it; output compare is not used We will see those bits later
http://class.ece.iastate.edu/cpre288
31
TICIE1: Timer/Counter 1, Input Capture Interrupt Enable Write 1 to it to use interrupt TOIE1: Timer/Counter1, Overflow Interrupt Enable If set to 1, interrupt is raised when Timer1/Counter 1 value (TCCN1 value) is overflowed
Note: Use a sufficient large prescaler value to avoid overflow in lab 7
The other bits are for output compare we will see them again
http://class.ece.iastate.edu/cpre288
32
ETIMASK is for Timer/Counter 3 TICIE3: Timer/Counter 3, Input Capture Interrupt Enable Write 1 to it to use interrupt TOIE3: Timer/Counter 3, Overflow Interrupt Enable If set to 1, interrupt is raised when Timer1/Counter 3 value (TCCN3 value) is overflowed
http://class.ece.iastate.edu/cpre288
33
Port D pin 4 (PD4) Its Timer1/Counter1s IC pin, and connects to the input/output pin of the PING sensor
http://class.ece.iastate.edu/cpre288
34
IC Programming Example
volatile enum {LOW, HIGH, DONE} state; volatile unsigned rising_time; // start time of the return pulse volatile unsigned falling_time; // end time of the return pulse /* start and read the ping sensor for once, return distance in mm */ unsigned ping_read() { } /* ping sensor related to ISR */ ISR (TIMER1_CAPT_vect) { }
Note 1: This code does not work for Lab 7 as it is. Note 2: Does not follow timing example of slide 29.
http://class.ece.iastate.edu/cpre288
35
/* send out a pulse on PD4 */ void send_pulse() { DDRD |= 0x10; // set PD4 as output PORTD |= 0x10; // set PD4 to high wait_ms(1); // wait PORTD &= 0xEF; // set PD4 to low DDRD &= 0xEF; // set PD4 as input } /* convert time in clock counts to single-trip distance in mm */ unsigned time2dist(unsigned time) { }
http://class.ece.iastate.edu/cpre288
36
http://class.ece.iastate.edu/cpre288
37