CE351 lec17 PWM pins, TOP, BOTTOM, Mode 14
Overview
Yiyan Li explains active-low PWM control for LEDs, including how `analogWrite(pin, 255)` turns an LED off and how clearing a compare-output control bit disconnects PWM. The lecture reviews an 8-bit Timer2 preload calculation for a 10 ms interval, then applies 8-bit RGB PWM values and a 10 ms interrupt routine to fade through six colors in sequence.
Key takeaways
- For the active-low LEDs discussed, `analogWrite(pin, 255)` turns a channel off and `analogWrite(pin, 0)` turns it fully on.
- With a 16 MHz clock and a Timer2 prescaler of 1024, a 10 ms interval is approximately 156 timer counts, so an 8-bit overflow timer can start near 100.
- The RGB sequence yellow, red, fuchsia, blue, turquoise, and green can be encoded as channel values using 0 for on and 255 for off.
- Adding 255 to an 8-bit channel value is equivalent to subtracting 1 because the arithmetic wraps modulo 256.
- A 10 ms interrupt and one-unit channel steps provide gradual color transitions; changing either the interrupt period or step size changes the transition effect.
Chapters
- The LED outputs are active-low: a pin value of 0 turns an LED on, while 255 turns it off.
- PWM can be disconnected by clearing its compare-output control bit, returning the pin to normal port operation.
- For an 8-bit Timer2 at 16 MHz with a 1024 prescaler, a 10 ms interval requires about 156 timer counts; preload the counter with 256 minus that count.
- The Timer2 compare-output configuration selects the corresponding OC2A output pin; the compare mode determines when that pin is set or cleared.
- Each RGB channel uses an 8-bit PWM value, with 0 meaning fully on and 255 meaning off.
- Represent red as (0, 255, 255), green as (255, 0, 255), blue as (255, 255, 0), and yellow as (0, 0, 255).
- The color sequence is yellow, red, fuchsia, blue, turquoise, and green, with each color represented by red, green, and blue channel values.
- The template stores initial colors and per-channel deltas in arrays, allowing one shared index to select the current color and its transition behavior.
- After each transition, advance the color index; after all six colors, return to the initial color.
- An interrupt every 10 ms adjusts each RGB channel by its configured delta, gradually fading the current color toward the next.
- The routine can use addition for every channel update: adding 1 increments a value, while adding 255 wraps around and acts like subtracting 1.
- A step size of 1 and a 10 ms interrupt interval produce a gradual transition; larger steps or a different interrupt frequency change the fade's smoothness and speed.
- The lecture closes by noting that the next in-class problem is postponed to Friday and that the quiz will cover timer and PWM calculations.
Transcript excerpt
0:07 active lower, active high. You know this these are dials, right? LED light emitting dials. The dials got a high voltage on the left and um so for these are the signal pins from the mic controllers. If these pins are high, there's no voltage drop. So the LEDs are off. So when they're low, there's voltage drop and so turn on the LEDs. So they're actually low, right? So when low, you get them to be turned on. And um to simply to simply turn them turn them off, you should give uh uh 255 to the pen using alite. Definitely you can
0:56 you can write you can you can turn off like for example here there's another way to turn it off which is uh here see that's another trick to turn it off. Remember we just turn this bit on right turn this bit on it starts doing this job. If you flip it back to zero, what's going to happen? See, there's a mode 00 normal port operation PM disconnected. So, if you want to turn off PM, you can just directly flip this bit to zero, which is fine. But I think I either way. Okay, you can you can do that or you can you can just do an analog right analog right to the pin and give a 255 to it.
1:52 Questions on that? So you know this function right analog right of that whatever pin, right? And you give a 255 to it. You can turn it off or you could flip that um comp 3 B1 to zero. How to do that though? How to do that? How to flip that bit to zero.
2:57 Will that work? How does this work? Wait. Um, 3B1. Could this clear this fit there? It does. Right. So, what's happening is
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
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.