CE351 lec10 Timer and play music notes
Watch on YouTube →
Overview
Yiyan Li reviews 16-bit Timer1 calculations at a 16 MHz clock, showing how prescalers, starting counts, and overflow determine elapsed time and counter values. He then explains a music-note program that uses active-low buttons, precomputed half-period counts, and Timer1 registers to toggle a speaker output, contrasting delay-based playback with a timer-based approach.
Key takeaways
- At 16 MHz with a 1:1 prescaler, a 16-bit Timer1 overflows every 4.096 ms, so longer intervals must account for one or more complete cycles.
- For an interval that begins at count x, the remaining counts to overflow are 65,536 minus x; multiplying the resulting time by the timer frequency converts it back to counts.
- A speaker note is generated by toggling its output every half-period; two half-periods form one full waveform period, and the frequency determines pitch.
- With an active-low pull-up button, pressing reads LOW, making a while-LOW loop a direct way to sustain a note only for the duration of the press.
- A larger prescaler reduces the counts needed for a fixed time interval, but overly coarse count resolution can make low-frequency notes inaccurate.
- Pull-up resistance affects both current and signal speed: higher resistance uses less current but charges parasitic capacitance more slowly, which can constrain bandwidth in circuits such as I²C.
Chapters
- A short quiz on timer calculations is scheduled for Wednesday, followed by an interrupts tutorial on Friday.
- Homework 5 is the focus during the following week's canceled Monday and Wednesday classes.
- Timer1 is a 16-bit counter, so its full range contains 2^16, or 65,536, counts.
- A 1:256 prescaler reduces a 16 MHz timer clock to 62.5 kHz, increasing the time required to complete a count range.
- For a nonzero starting value such as 0xADAB, calculate remaining counts as 65,536 minus the initial count.
- Convert between time and counts by tracking units: divide counts by counts per second for time, or multiply time by frequency for counts.
- At a 16 MHz clock with a 1:1 prescaler, a full 65,536-count Timer1 cycle takes 4.096 ms.
- A 5 ms interval exceeds one cycle; subtract the first 4.096 ms overflow and convert the remaining time into the counter's post-overflow value.
- From a starting count of 30,000, calculate rollover time using 65,536 minus 30,000, then divide by 16 million counts per second.
- For 100 seconds, divide total elapsed time by the 4.096 ms cycle period to find completed rollovers, then use the fractional cycle to find the remaining count.
- Students are asked to finish and submit the timer calculations practiced in class before moving to the music-note tutorial.
- The class pauses for several minutes; Li briefly discusses a social-media comparison of morning street activity in Los Angeles and China.
- Li frames programming as starting with the intended user behavior, then implementing the code to match a demonstration.
- The example has separate top and bottom buttons, each selecting a note that plays only while its button is held.
- The supplied code is a template, and students are expected to follow its programming pattern rather than submit an unrelated generated solution.
- Each note is produced by repeatedly toggling the speaker output at a frequency that determines its pitch.
- A note list and incrementing index select successive pitches; cycling logic returns the index to the first note after the last.
- A 50% duty cycle means the signal stays high for half of each period and low for the other half; PWM can also use values such as 20%.
- Holding a button keeps the output loop running, while releasing it stops playback.
- TCCR1A and TCCR1B are Timer1 control registers; setting TCCR1A to zero selects normal mode for this example.
- The CS12, CS11, and CS10 bits in TCCR1B select the Timer1 clock source and prescaler.
- Setting all three clock-select bits to zero disconnects the timer clock and stops Timer1; a nonzero prescaler setting starts it.
- Load the counter with 65,536 minus the desired count interval so it overflows after the required number of timer ticks.
- A spreadsheet supplies the Timer1 counts for each note's half-period, the interval between output toggles.
- The program toggles the speaker after each half-period, producing a complete waveform period from two timed intervals.
- Increasing the prescaler slows the timer and reduces the number of counts needed for a given interval.
- If a prescaler makes the count interval too small, low-pitch notes may have insufficient resolution and become inaccurate.
- The buttons use pull-ups, so pressing a button pulls its input low and playback logic checks for a LOW state.
- A 10 kΩ pull-up is a common choice, but values such as 2 kΩ, 5 kΩ, or 50 kΩ can be appropriate depending on the circuit.
- Higher resistance reduces current but charges parasitic capacitance more slowly, limiting signal bandwidth.
- I²C pull-up values may require calculation, with examples around 2 kΩ to 4 kΩ depending on the required frequency.
- The simpler implementation uses a microsecond delay, flips the speaker output, and repeats while the active-low button remains pressed.
- The delay value must match the note's half-period; changing it changes the audible pitch.
- After release, the code increments the note index, using the remainder operation to wrap through a seven-entry sequence.
- The demo assigns the top and bottom buttons to pins 8 and 9, respectively.
- The timer-based version enters a loop while the button is pressed, starts Timer1 by selecting a prescaler, and uses the counter to time output changes.
- Li highlights the counter setup and register-bit pattern as material to memorize for later quizzes, with detailed timer manipulation deferred to Wednesday.
Transcript excerpt
0:00 Before we get started, any questions, any concerns or anything? All good. Okay. So, this week, this is the week four already, which is pretty crazy. Uh, so this tutorial number two, we start on Friday about timers, time delays, uh, count counters. Um, the first uh tutorial about that will be finished on Wednesday this week. and then uh followed by a quiz on timers. So look at your inlex number three right so there are a lot of exercise on counter calculation timer calculations expect a short one short quiz on on timers on Wednesday okay and on Friday we'll start a new tutorial um it's about interrupts
0:48 and next week uh Monday Monday Wednesday I have to go to Denver for for a trip grant meeting uh and so we won't have any classes over there so you guys will just work on homework five we'll start a neutral show on on Friday. Um, so there are there are a lot of tasks over there. So keep working on that one. Um, and Friday next week we'll wrap it up and give you guys in class number five. So that's the plan for this next week. Okay. So if you take a look at the in class number three where we were at last time, we were at one of the problems at the bottom of the first page, right? here. Okay. So the major idea here is that um
1:38 you have all the units and you convert you just to write down the units of the uh the count number counts clearly and then you see if you're is asking for counts number counts or asking for a time then you can decide which operation do you need is a division or a multiplication. Okay so these are all
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Yiyan Li.