EECS 373 - Fall 2026 - Lecture 9: "Timers: Capture & Compare" — Transcript
Full transcript
- 0:00Okay, we finally are ready to go. I've
- 0:04solved all the world's AV problems.
- 0:06We're ready. All right, so well, they're
- 0:10not released. They'll be released right
- 0:11after class. We'll talk about grades.
- 0:15[laughter]
- 0:16Homework uh four is due on Monday.
- 0:20And then we're going to do slide
- 0:21ideation slides will be due next
- 0:23Thursday. We'll talk about that and then
- 0:25team formation is next week. So for the
- 0:28ideation slides, we'll release the
- 0:29details tonight. Basically, your job is
- 0:31to create just three slides. Uh
- 0:34individual slides will have a upload
- 0:36folder for you and it's supposed to be
- 0:38like not a lot of words. Like there's
- 0:40got to be an image, less than 100 words,
- 0:43something that describes your particular
- 0:45project. And if you want, you can go and
- 0:48put a little star up there and that will
- 0:50like key to me that you want to present
- 0:52this particular thing. But the point is
- 0:54that you have to start thinking about
- 0:56what projects you want to work on. And
- 0:57if this is a forcing function to make
- 0:59you say, "Oh, this is the sort of thing
- 1:01I'd like to do." And then you can also
- 1:02look through other people's projects and
- 1:04be like, "Okay, I see uh other people
- 1:07are at least aligned with my vision
- 1:08here." Uh that's too loud. Let's see
- 1:14better.
- 1:16Okay.
- 1:18And then we'll serve pizza. It'll be
- 1:21there. uh endow one of the rooms we took
- 1:24the exam in. And then uh the next I
- 1:27think that date's right, the next
- 1:28Tuesday, you need to register your team.
- 1:30So we'll have a form online. You'll just
- 1:32say this is our team. This is our team
- 1:34name. This is what we're going to do. Uh
- 1:36if you're having trouble uh finding a
- 1:38teammate, you guys should meet with me
- 1:40after class on that uh previous Monday.
- 1:47Okay. Uh and then uh after so I'm just
- 1:51also laying out what the project is
- 1:53going to be. Uh the first next step is
- 1:55after the uh team formation then you
- 1:57have to do a project proposal. I don't
- 2:00think these are online yet in which you
- 2:02will basically there's a description for
- 2:04the project. You will write a brief
- 2:05statement that is really me forcing you
- 2:08to think through what is this project
- 2:10going to be? What components do you
- 2:11need? What's its goal? And then there'll
- 2:14be one-on-one meetings with me or James
- 2:18uh to go over your project and mostly
- 2:20give you feasibility feedback
- 2:23um and help you think about like okay
- 2:25here are the parts we have get you guys
- 2:27started on your projects. So any
- 2:30questions about those parts?
- 2:37Okay. So background of timers. Uh, and
- 2:40then later on we're going to talk about
- 2:42cycic and Ashley this semester. We're
- 2:43not going to talk about virtual timers
- 2:45anymore in this course because 473
- 2:47doesn't use them anymore in the same way
- 2:48that they did. All right. So, the best
- 2:52source of information is there is a
- 2:53separate manual just for the capture
- 2:56compare timers. This is more
- 2:58specifically for your device. And you
- 3:00can more or less be at the point now
- 3:02where you could read through this
- 3:03manual. It's long, sure, but um for the
- 3:08uh unit that you're using, the capture
- 3:09and compare unit 4, all the information
- 3:12you need is there. I'm going to try and
- 3:13give you the background so that it's
- 3:15easy to interpret this
- 3:18and then your labs are going to have you
- 3:20actually exercise the details of this
- 3:22unit. All right, so let's back up for a
- 3:25second. So, we have timers and we think
- 3:27about timekeeping in general. We have
- 3:29things like stopwatches and alarm clocks
- 3:33and world zone time zones and all those
- 3:36sorts of things. So there's lots of
- 3:37these passage of time things that we
- 3:40have to keep track of stopwatches. Um
- 3:43but then that's kind of like our
- 3:44userfacing point of view. But then
- 3:46understanding and manipulating time in
- 3:48the embedded systems world sometimes you
- 3:50have to make uh give information to
- 3:52users in their world. But most of it is
- 3:55controlling things. And I think the
- 3:57conceptual idea is that computers uh
- 4:00when interfacing with the world are bad
- 4:02at almost everything like they don't
- 4:05understand analog. They only understand
- 4:07voltage at best, right? But the one
- 4:10advantage they have is they are really
- 4:11good at controlling time, right? So that
- 4:13you have a clock, it goes extremely fast
- 4:16and you can manipulate that. So a lot of
- 4:19these devices that we're going to
- 4:21interact with are controlled through
- 4:23some bit of time. So we talked about
- 4:25servers servos before and that's you're
- 4:27doing a little pulse that moves the
- 4:29servo to one location. You're encoding
- 4:31that position as a function of pulse
- 4:34timing.
- 4:35Uh we'll talk I think in lab you'll do
- 4:38uh DC motors and those are using
- 4:41pulsewidth modulation to control the
- 4:43speed of motors effectively how much
- 4:46current is going into it and LEDs. So
- 4:49all these things are really about
- 4:50controlling time because that's the only
- 4:52thing a computer or microcontroller is
- 4:54good at.
- 4:56So really that's what we're trying to
- 4:57get you guys the ability to do. And this
- 4:59word pulse with have you all heard
- 5:01pulsewith modulation before? Probably
- 5:03not one some people. Yes. Okay. So
- 5:06pulsewidth modulation is kind of the
- 5:07goal of where we're going to be at the
- 5:09end of this lecture. And it's really
- 5:11just well we'll talk about it but it's
- 5:13really just pulse with modulation is
- 5:15controlling the width of pulses to
- 5:17control uh timing information.
- 5:20All right.
- 5:22Uh so okay uh what am I trying to say
- 5:25here? All right. I kind of repeated all
- 5:26this but here these are the things you
- 5:29can do with it. Um so how does a
- 5:32computer measure time? So typically
- 5:33there's an event that goes off right?
- 5:35You have some main thread, you have an
- 5:37interrupt that's controlling time, but
- 5:39that's what it does when it has time. So
- 5:42my question to you is uh what is the
- 5:45electrical method that electronics use
- 5:48or the method electronics use to measure
- 5:50the passage of time? It's probably
- 5:52useful to think about how humans do it.
- 5:54So we look at the natural world. There
- 5:56has to be something in the natural world
- 5:58for us to use for timekeeping. So rise
- 6:00and fall of the sun, spring mass const
- 6:03constants for uh clocks. And so my
- 6:06question to you is what is the physical
- 6:08principle that electronics use to
- 6:11measure the passage of time?
- 6:14All right, take a take a minute to chat.
- 6:35Yeah.
- 6:49Heat. Heat.
- 6:59Heat. Heat.
- 7:33All right. What do we think? How do
- 7:35computers measure the passage of time?
- 7:41generates
- 7:44>> a magic. It's true. There are magic
- 7:46crystals. I know we read around a lot of
- 7:48fantasy books when we were kids, but
- 7:49there are magic crystals, but that's not
- 7:52it's very that is the object. But what's
- 7:54the mechanism that we're that we're
- 7:56using? Yeah.
- 7:57>> Electricity.
- 7:58>> What's that?
- 7:59>> Electricity.
- 8:01>> Yes. So, uh there's different
- 8:03mechanisms, but really it's there is a
- 8:06pislectric material in that crystal, the
- 8:08quartz crystal, and it's vibration. We
- 8:10are looking at the pro the propagation
- 8:13of a physical wave through that piece of
- 8:15crystal going back and forth and it's
- 8:17the pisoelectric effect which goes from
- 8:19electricity which the computer at least
- 8:21can measure pulsing that crystal you
- 8:23we're looking at the delay in time as a
- 8:26wave goes across that crystal structure
- 8:28and it just happens that our species
- 8:29figured out how to make that crystal
- 8:31very accurate and repeatable from one
- 8:33device to another device. But these
- 8:35things seem magical, right? But it
- 8:37really is just this just a time delay of
- 8:40a physical wave oscillating across a
- 8:42piece of uh crystal. I still think
- 8:44that's amazing. Um yeah. Okay. So,
- 8:49anatomy of a timer. So, we start with
- 8:52some crystal. All right. There's some
- 8:54clock divider. We're not really going to
- 8:55get into too much detail about this, but
- 8:57the analog parts of it, but basically
- 8:59this crystal here along with some
- 9:01digital internal circuitry will turn it
- 9:03into a square wave. Then we're going to
- 9:05have a pre-scaler and a counter. So
- 9:06these are the things that we're more or
- 9:08less going to learn about today. Capture
- 9:10and compare components. And this is all
- 9:13basically at the assembly level where
- 9:16this mod or these are the RTL level I
- 9:18should say where the module here is um
- 9:21controlling the underlying function and
- 9:23then we get up to the sub layer where we
- 9:25have assembly and then the abstract
- 9:27layer and then the examples we talked
- 9:29about before with you know from the
- 9:31iPhone that's up at the application
- 9:33layer and there are ways at least
- 9:35mentioned them so that you guys are
- 9:36aware to virtualize your timer so that
- 9:38you can have one timer that can do um
- 9:41you know any number a scalable amount of
- 9:43time with some um caveats.
- 9:48Okay. So, yeah. So, here we're starting
- 9:50with the lowest physics going all the
- 9:52way up into uh different applications.
- 9:54So, we'll start off with the crystal as
- 9:56you guys pointed out. Um
- 9:59there are different types of crystals to
- 10:01be aware of. So, these are typically
- 10:03quartz. Um they're uh low frequency. You
- 10:07can see some with leads. Those are
- 10:08called throughhole. And most modern ones
- 10:10are going to be um uh surface mount.
- 10:13They're rarely are they ever in the C
- 10:16the uh microcontroller.
- 10:19I can't think of a time when I've ever
- 10:21seen that. Although I have seen them
- 10:22integrated with real-time clocks. Um
- 10:25usually outside the device. And here you
- 10:28can see that this is for your XM XMC
- 10:324770 your Infinia chip. So there's
- 10:34external inputs here for your crystal.
- 10:37Um, and these are happen to be really
- 10:39good at lower frequencies. So, a common
- 10:41one you're going to be in the range of
- 10:43like 10 kHz up to maybe 100 kilhertz or
- 10:46so. And a very common one is 32.768
- 10:50kHz. And we'll see why in a little bit.
- 10:52But your device has a specific module
- 10:55just to interface with this external low
- 10:58frequency clock.
- 11:02And then the other type would be higher
- 11:04frequency uh resonators. So these are
- 11:06like ceramic uh saw uh crystals. Um they
- 11:11work on slightly different principle uh
- 11:13surface acoustic waves than the uh
- 11:16quartz crystals we talked about earlier.
- 11:17And they were really in the 10 meghertz
- 11:20up to 100 meghertz range. That's
- 11:22typically what these things can be made
- 11:25to. So there is kind of like this this
- 11:27gap this between like a 100 kilhertz and
- 11:31or sorry 1 megahertz and 10 kHz or so or
- 11:34or 10 meghertz or so where it's hard to
- 11:36find crystals or oscillators but um so
- 11:40that's why the the community generally
- 11:42has these two types and your device has
- 11:45an input for that. So now we have a way
- 11:47to we can control the clock how fast
- 11:50your um processor is going, how fast the
- 11:53advanced peripheral bus is going and
- 11:55your timers and your peripherals u by
- 11:57choosing the correct clock frequency.
- 12:01Any questions?
- 12:04Okay.
- 12:06And then uh it's not you don't have one
- 12:08on your device, but it's good to know
- 12:10just embedded systems that there are
- 12:11many microcontrollers that have
- 12:12digitally controlled oscillators, which
- 12:14is basically something like a relaxation
- 12:16oscillator, a little or ring oscillator,
- 12:18a little thing on chip where they can
- 12:20have registers and you can dynamically
- 12:22control the speed of your processor,
- 12:23which is really nice for low power
- 12:25things. So you have to do something very
- 12:26quickly because you have a timing
- 12:28constraint, you can go into high power
- 12:29mode, otherwise you can go down to uh a
- 12:32lower power mode if you if you needed
- 12:34to. Um there still is that kind of speed
- 12:36energy trade-off. Um but there's certain
- 12:39reasons why you don't want to draw too
- 12:41much current out all the time,
- 12:42especially with batteries uh to keep
- 12:45them uh operating for a long longest
- 12:48time. You don't actually want to pull
- 12:49them at their peak current. So there's
- 12:51good reasons to control how fast your
- 12:54oscillator goes for power reasons. Also,
- 12:56if there's different protocol reasons,
- 12:57you might want to change your oscillator
- 12:59speed. So that is available on many
- 13:01microcontrollers, but not on yours. uh
- 13:03you have uh two fixed oscillators and
- 13:06Xfinion gives them for specific reasons.
- 13:08It's only for backup. They uh their
- 13:11point of view. I mean, I guess you could
- 13:13use them as your primary, but um the
- 13:15intent is that you'll have high
- 13:17frequency crystals. Uh you have a low
- 13:19frequency crystal and those have really
- 13:21good time uh parts per million, so
- 13:24really good time accuracy. But should
- 13:27any of those fail for some reason,
- 13:28especially on bootup, uh because they're
- 13:31in real-time controls, they really want
- 13:33to make sure that your device will boot.
- 13:35And so now you they have these backup on
- 13:37on internal things that will allow the
- 13:39system to boot up and at least throw an
- 13:41error or explain why uh there's a
- 13:43problem.
- 13:46Um yeah, and so those are these little
- 13:50backup units here.
- 13:52So uh you might notice that um so your
- 13:56device and if you look at the data sheet
- 13:58it says that it's preferred
- 13:59highfrequency crystal is 16 meghertz but
- 14:02you may have noticed that all your
- 14:03devices run off of uh or sorry your core
- 14:06and your buses run off 144 mehz at least
- 14:08that's a high speed. So how do you
- 14:11create a clock source that's higher than
- 14:13the input clock sources?
- 14:17Yeah.
- 14:22>> Well, you you have the right idea, but
- 14:24the word divide got you there. So, the
- 14:25input frequency is 16 MHz and they want
- 14:29uh 144 MHz. So, how do we go up in
- 14:32frequency?
- 14:35>> Okay, I gave it away. Yes, you're right.
- 14:36Um, there is a multiply function. Uh,
- 14:40essentially, it's called a phase lock
- 14:41loop. It's e blackmagic. I'll tell you
- 14:44that. I should say black magic. is black
- 14:46magic too. I don't know. But uh black
- 14:48magic for sure. Um I remember when I was
- 14:52in graduate school taking a class and
- 14:54just being destroyed just like just like
- 14:56your exams only I didn't know what was
- 14:58going on. And uh because it was all
- 15:01control theory eventually figured it
- 15:02out. But a phase lock loop essentially
- 15:04says here's an incoming waveform. I'm
- 15:06going to generate a faster waveform and
- 15:09then I'm going to take like every tenth
- 15:11edge let's say and compare that to the
- 15:13incoming edge. And so now I have this
- 15:15mechanism of I'm gonna go faster than I
- 15:17need to and every tenth edge I'm going
- 15:19to see if I'm aligned and I'm gonna go a
- 15:20little bit faster, a little bit slower,
- 15:22so I'm aligned with the next clock edge.
- 15:23So there is a way to increase your clock
- 15:26frequency using this thing called a PLL.
- 15:29The drawback is it's higher power and
- 15:31higher higher noise. So the there's a
- 15:33little bit more jitter in that signal
- 15:35that's coming in. Engineers have done a
- 15:37great job and made it so it's a
- 15:39tractable usable solution, but there are
- 15:41trade-offs in it.
- 15:45Um, and the last thing I want you to be
- 15:46aware of in general is that uh there is
- 15:49a relatively well this is simpler than
- 15:51the STM32 but relatively complicated
- 15:54clocking structure um where on all chips
- 15:58the idea of how you would distribute
- 16:00your clock to different peripherals and
- 16:02throughout the chip is a you know a
- 16:04field of study or at least a sub
- 16:06community of EES in its own right. Um
- 16:10and you're while you're not necessarily
- 16:12responsible for knowing all this from
- 16:14like an exam or even lab point of view,
- 16:16you should good to be aware of um so
- 16:18that if you needed to for your project,
- 16:21you can manipulate the clock frequency
- 16:23of your buses, of your core, of your
- 16:25peripheral so that they meet the timing
- 16:28needs you have. You don't have to just
- 16:29go with a default. Um, one common one
- 16:32that I see each year is if somebody
- 16:34wanted to do like a VGA HDMI controller
- 16:37or so FPGA to uh interface with HDMI. So
- 16:41you could directly um uh you know uh
- 16:46project on a projector or on a screen
- 16:48from your microcontroller. In that case,
- 16:50it's usually very advantageous just to
- 16:53make the core function of your buses and
- 16:56your CPU the exactly the right frequency
- 16:59because everything just lines up and
- 17:01then you don't have uh weird
- 17:02asynchronous
- 17:04events. Everything is just lined up with
- 17:05your data clock coming in. But just this
- 17:09is just good to be aware of.
- 17:12And then uh I guess the part I didn't
- 17:13say is so here often times you can
- 17:15select different sources, you can route
- 17:17them different places and then you can
- 17:18divide them down or dividing is really
- 17:21easier than multiplying but you can
- 17:23control them so that different parts get
- 17:25different uh frequencies.
- 17:28Okay. So now down to the anatomy of a
- 17:32basic counter. So um we're going to
- 17:35spend a lot of what lab is going to be
- 17:37is like the additional parts to the
- 17:39counter. We'll start here. So you oh you
- 17:43usually have some clock frequency coming
- 17:44in um let's just say it's like 100 mehz
- 17:48it's going to be whatever say the
- 17:50default is on your system could be 144
- 17:52MHz the max speed and then they have
- 17:54this idea of a pre-scaler so the
- 17:56pre-scaler reduces that input clock
- 17:59frequency down to something lower
- 18:01and that's typically because you might
- 18:03be running at say human speeds humans
- 18:06it's way too fast to go at 144 meghertz
- 18:09or just talking to PWM or sorry using
- 18:13PDW PWM to talk to a servo or motor
- 18:17controller. It's just unreasonably fast
- 18:19to go with that speed. And this also
- 18:21allows on the actual peripheral level at
- 18:23this timer slice uh you can control the
- 18:27um uh duration clock speed to that
- 18:30particular timer which gives you a lot
- 18:32more flexibility. So each one of your
- 18:33modules is going to have this pre-scaler
- 18:36um in this particular device. We'll talk
- 18:38about pre-scalers in a second, but uh
- 18:40the way it's computed is it's just a
- 18:42power of two offset. So, we're just
- 18:44going to divide by a power of two. Input
- 18:46frequency is going to be uh lower output
- 18:48frequency.
- 18:50Then you have the the counter which is
- 18:53just um you know a device that will
- 18:55count up and count down and usually
- 18:56there's a a timer register associated
- 18:58with it. So now you can keep track of
- 19:01the current time. uh you can write into
- 19:03it, read out of it, do all those things.
- 19:06And then almost every counter that
- 19:08you'll ever see has something along the
- 19:10lines of a reload register or a period
- 19:13register where after a certain period of
- 19:16clock cycles, it will it holds a value
- 19:19of what the counter should reset to. So
- 19:21your say your counter counts up to, you
- 19:24know, 10 or something, it hits that and
- 19:26then it sets back to zero. The
- 19:28alternative mode is when you uh you
- 19:29could be counting down from some high
- 19:31number, you count down to zero and then
- 19:32when it hits zero, that number gets
- 19:35loaded back in. So you can keep cycling
- 19:37between some period of time. It doesn't
- 19:40have to be just whatever the pre-scaler
- 19:42gave you.
- 19:44Any questions? We'll go over each
- 19:46component in a little bit.
- 19:49>> Yeah.
- 19:55>> Yeah.
- 20:05It's it's dictated by external clock.
- 20:08That's a good question. It doesn't have
- 20:10to be that way, but there's good
- 20:11practical reasons for it. Um,
- 20:15uh, let's see. This one is fine. Um, so
- 20:19you can see all the sources there. So
- 20:21the external clocks are much more
- 20:22accurate and it's really you should
- 20:24think of more of like a clock source. So
- 20:26the crystal doesn't give out a square
- 20:28wave. It gives out this horrible analog
- 20:30wave that kind of has this period to it
- 20:31but is very very precise. You can have
- 20:34an external clock. So that crystal/c
- 20:37clock in that pin could be driven by
- 20:40some other clock. Kind of rare that you
- 20:42would want that but it could be. Then
- 20:45internally you have this say we go back
- 20:46to the analog solution where you have a
- 20:47crystal the uh clock HP block up there
- 20:51turns that analog signal into a digital
- 20:53signal that drives the rest of the
- 20:54device. That's the normal way. There's
- 20:57this kind of backup way where you could
- 20:59have the internal clocks the relaxation
- 21:02oscillator those things control the
- 21:04timing but um due to the variations in
- 21:08manufacturing process as well as
- 21:09temperature those are less precise and
- 21:12they're also higher power. So typically
- 21:14almost all embedded systems they use an
- 21:16external clock or clock source let's say
- 21:20uh we got off topic well we're not off
- 21:22topic but it was a good topic but
- 21:23questions about this
- 21:26reload
- 21:31>> um no uh the p the period register is
- 21:35just is some other value we'll see it in
- 21:38a little bit but it's it's you have the
- 21:40so on your devices 16- bit timers it
- 21:42could the other uh on other devices, but
- 21:45it will count up to 16 and then it would
- 21:47overflow naturally. But this is a
- 21:48register that forces overflow at a
- 21:50different or reset at a different uh
- 21:52time or different count. So you get to
- 21:54control the period that occurs in terms
- 21:57of ticks. Yeah.
- 22:02>> No, I'll show I'll show an example of
- 22:04that in just a second. It's only
- 22:06dividing. It doesn't multiply. So phase
- 22:08loop is for multiplying. This is only
- 22:10dividing.
- 22:17Okay.
- 22:18So going to the uh clock divider. So uh
- 22:22this is I'll this is a basic structure
- 22:26that this allows the input thing. You go
- 22:28through a flip-flop and then you divide
- 22:30by two divide by four divide by eight
- 22:32and then you could tap off at different
- 22:34uh signals here. You imagine you could
- 22:36have a MX on top and then you can choose
- 22:38do I want to select the divide by two by
- 22:40a four by eight by n whatever whatever
- 22:42the largest value is and I think on your
- 22:44device might be 32 I'm not exactly sure
- 22:46off the top of my head um so that is
- 22:49it's a very simple functional block so
- 22:51they're uh easy to implement on device
- 22:54and that's why each one of your timer
- 22:55gets one um there is a counter example
- 22:59uh which is called a pre-scaler now
- 23:01clock divider and pre-scaler uh these
- 23:03terms are getting overused but A
- 23:05pre-scaler allows you to not choose
- 23:08increments of four. It's a more
- 23:10complicated analog or digital circuit
- 23:12and it can do any incremental value in
- 23:15between there. So it doesn't have to be
- 23:16just a divide by two. The problem for
- 23:20you guys at least going forward when you
- 23:22go out into industry is that uh there's
- 23:25a documentation warning here in the
- 23:26sense that Infinion calls this structure
- 23:29which I would call a clock divider a
- 23:31pre-scaler. Now there's not wrong in the
- 23:34grand scheme of how we talk about
- 23:36electronics, okay? But I would say that
- 23:39if you went to EE textbook, uh the word
- 23:42pre-scaler is probably not defined by
- 23:44this circuit. This circuit is probably a
- 23:45clock divider. But it's more not that it
- 23:48will ever affect you in this class. It's
- 23:50more of a statement if you go out into
- 23:51industry and you're like, how is it
- 23:53possible that a pre-scaler can go
- 23:55anything but a power of two? Well, uh
- 23:57this confusion, this nomenclature
- 23:59confusion is the reason.
- 24:02Um, let me see if this will actually
- 24:04work.
- 24:06Nope. Cool.
- 24:09Uh, okay. So, just to give an example
- 24:13here. So, this is just a normal clock
- 24:15divider example.
- 24:18There we go. And so, here we have a
- 24:20clock at some frequency coming in. And
- 24:22we can see that each one of these taps
- 24:26here, here, or here, right? These are
- 24:29just a divided version of that thing.
- 24:32And so now you get to control. You're
- 24:33going to mux in essentially when you
- 24:36select your pre-scaler, you're mxing in
- 24:38one of these uh n taps. And now you can
- 24:42slow down your clock.
- 24:47What do you think?
- 24:49I see some people are like, "Yep, I'm
- 24:51bored." Some people are like, "I have no
- 24:52idea what this guy's talking about."
- 24:57>> Yeah. Can you explain how?
- 25:01>> So, uh, basically, uh, so if you just
- 25:05look at, so you look at clock goes in
- 25:07the Q, right? And it takes one clock
- 25:09cycle to feed back into D. And so now
- 25:12you're going to eat up two slot for
- 25:14every one uh, every two clock edges that
- 25:18go in, one clock edge comes out, one
- 25:20transition comes out the other side. So
- 25:22just the structure itself
- 25:25um, you know, uh, slows things down. And
- 25:28you can kind of see here we go slower.
- 25:30So you can kind of see the truth table
- 25:32here. But you see there's a let's see
- 25:34what happened. There's a click here.
- 25:37Come on.
- 25:39So it just takes two clicks on here to
- 25:42get one click on the other side. And
- 25:44we're just going to And the nice thing
- 25:45is from a hardware point of view that's
- 25:47so cheap. It's so cheap to have D
- 25:49flip-flops. Um as many as you want on
- 25:51device. So they don't care about putting
- 25:54any of those uh to give you as many
- 25:56clock division opportunities as you
- 25:58like. Okay.
- 26:02Okay.
- 26:06And all the I think all the um what do
- 26:09you call it? Uh
- 26:11links in fall in uh the PowerPoint I
- 26:13sent or the PDF I sent you are broken.
- 26:15So I will fix them tonight and uh and uh
- 26:19you can go play with these if you would
- 26:20like. Uh, so a counter module, there are
- 26:23lots of different topologies of a
- 26:25counter module, but the core
- 26:26essententral thing is it's just a thing
- 26:28that's going to count up and count down.
- 26:30You learned about these in 270, correct?
- 26:33Yes. Fantastic. Um, and so the topology,
- 26:37I don't care whatever topology the
- 26:38manufacturer wants to use. The real
- 26:40important things is that you can count
- 26:41up, count down, typically uh you can
- 26:44load data into them, right? You can go
- 26:46in through the D flip-flops. And it's
- 26:48not shown here in the structure, but uh
- 26:50and it may not be obvious, but almost
- 26:52every D flip-flop on your device is
- 26:55connected through a separate set of
- 26:57wires that allow you to shift into them.
- 27:01And this is what your J-TAG debugger is.
- 27:03This when you read out the memory state
- 27:04or the of your registers, you are
- 27:06literally just uh putting all the the
- 27:09flip-flops into a shift register mode
- 27:11where data is propagated through each
- 27:13one. Uh so there's that functionality
- 27:15and here you can also just load values
- 27:17in and you could have any number uh you
- 27:19want for a counter on your particular
- 27:21devices they're almost always uh 16 bits
- 27:24unless um there's a slightly different
- 27:26version uh for the uh CCUM8
- 27:30version of the timer on your device. Um
- 27:33and so here's just an example of a
- 27:35counter. Uh we have a clock coming in
- 27:37and we're just showing this is a binary
- 27:39to digital converter and this is showing
- 27:40that we're counting up and we can hold
- 27:44no this is sorry we can go up and down
- 27:46and if I had a little uh brake button to
- 27:48uh hold the um clock that would do that
- 27:51but I can count up and I can count down
- 27:53and really it's just this little bit
- 27:54simple of logic. So all you have to do
- 27:56is think this thing is clicking away.
- 27:58It's either counting up, it's counting
- 27:59down, and when it uh and you can also
- 28:02load in values into it to reset it if
- 28:04you needed to.
- 28:09>> Questions? Yeah.
- 28:20>> Oh, these are J flip-flops.
- 28:22>> They're not D flip-flops. Uh, so I think
- 28:24it's every edge uh makes a transition.
- 28:26And it's not just the positive edge if I
- 28:28remember correctly.
- 28:29But Nan, you are asking me like
- 28:3120-year-old material. Like I like I took
- 28:34this class when I was in undergrad. I
- 28:36forget how J flip-flops work.
- 28:40But I think the the big thing is I mean
- 28:42you're welcome to look through this
- 28:43topology and say like, okay, I've
- 28:44mastered it. I'm not asking you to
- 28:46master how the clock turns into a a
- 28:50count. I'm asking you to realize, hey,
- 28:52this thing does count, right? like
- 28:54there's a module that can go up, can go
- 28:56down, it can be reset, it can be held.
- 28:58Uh all those different functionalities.
- 29:02Okay.
- 29:09Uh okay. So now the the conceptual model
- 29:12I want you to have is we have a a clock
- 29:14coming in. We I took off the pre-scaler
- 29:15for here.
- 29:17And then uh you'll often see these
- 29:19diagrams like this and you're like
- 29:21here's the uh the count value [snorts]
- 29:24as a function of time and time is
- 29:26progressing at a certain rate. So you
- 29:29have a clock with a period and we're
- 29:31just going to assume that we're so
- 29:32zoomed out that you're not actually
- 29:34seeing little stair steps, right? But if
- 29:36it was truly a shorter uh if the thing
- 29:39wasn't 16 bits you or we were more
- 29:42accurate in how we were drawing, you
- 29:43would see these little stair steps as
- 29:45you progress in time. to go from one to
- 29:46two to three to four. But this is just a
- 29:49common way of representing
- 29:52um the progression of a timer. And then
- 29:56uh so we have a 16- bit timer here and
- 29:58then he has some pro uh period reset
- 30:00values. So in here is the value at which
- 30:03it will reset and so the timer is going
- 30:05to progress up to this value and then
- 30:08when count in here equals the period
- 30:12register you're going to have you're
- 30:14going to reset. And I wrote here reload
- 30:16reset because if you're counting down
- 30:17then it takes the value that's in PR and
- 30:20puts it into the timer. If you're
- 30:21counting up it uh makes uh the counter
- 30:24go back to zero. So then you've come to
- 30:26some reset period and then you do it all
- 30:29again. And so this is where the idea of
- 30:31this is a period uh register because
- 30:33it's defining the period that this timer
- 30:35goes to. It's not saying that the timer
- 30:37itself say is 16 bits. It could go
- 30:39longer of course. Well, not of course
- 30:41but if PR is less than 16 uh the value
- 30:44of 16 bits then uh it will go up to that
- 30:46value and then uh reset.
- 30:52>> Yeah.
- 30:56>> Can be anything you want. You guys get
- 30:58to pick. So um you are all in 16- bit
- 31:02timers and what uh you'll see is that
- 31:04you are going to control like the the um
- 31:08pre-scaler value and the uh program or
- 31:11the period timer to hit some goals.
- 31:14That's that's the thing you're trying to
- 31:15do. And your goal will be make a
- 31:16waveform that does this.
- 31:21Um all right. So we can kind of kind of
- 31:23see an example how this works. So given
- 31:25a input clock of uh 32.76 kHz which is
- 31:29this very happens to be a nice crystal
- 31:31that's very popular uh what would be the
- 31:34reset uh period or or sorry the period
- 31:37register to hit 1 second. So you could
- 31:40kind of do the formal method where
- 31:41you're like aha okay so I oh I didn't
- 31:43spell that right. Okay. Um you have uh
- 31:46the the goal here of your uh I don't
- 31:49know why what I was even writing there
- 31:50but the goal here is to uh figure out
- 31:53from your frequency what's the period uh
- 31:56or of your waveform not the period that
- 31:59you're trying to go after for and then
- 32:01you go from that times the number of
- 32:02counts and that's your time interval. So
- 32:04that would be the um
- 32:07uh the way to think about this. Uh it
- 32:10just happens to be that engineers are
- 32:12clever. Engineers maybe a little lazy.
- 32:14They didn't want to do the math. So they
- 32:16said, "Okay, let's manufacture this
- 32:18watch crystal such that um it just
- 32:22happens that 2 to the 15 is equal to
- 32:25that, right? It makes math easy." And
- 32:27then so you could go through all of this
- 32:29math and figure out that oh, if I invert
- 32:32the period to figure out how how long a
- 32:34time slice is and then I count up to
- 32:36some value, I can figure out what the
- 32:38time interval. That's fine. But this the
- 32:40trick here is that that number happens
- 32:41to be special.
- 32:44And so if you want to with a 16- bit
- 32:47timer, all you do is set your um period
- 32:50timer. So your PR to this value and it
- 32:53will go off every second, right? Because
- 32:55that Yeah.
- 32:57>> What do you mention go off? Is there
- 32:59like an inter
- 33:05>> what is the mechanism
- 33:07that
- 33:08>> when what is the mechanism I think is
- 33:09what you're saying? All right. So it is
- 33:13hardware inside your timer module that
- 33:16when your PR in if you're counting up
- 33:19mode when the value stored in the uh
- 33:22period register equals the count time it
- 33:25does something and that is reset or
- 33:27reload
- 33:29doesn't is independent of the core.
- 33:32Now we will find out later of course you
- 33:34can go set an interrupt to go off but
- 33:37this functionality of repeating uh is
- 33:40independent of the core.
- 33:48Okay. And so if we put this value in
- 33:51here assuming this is a 16- bit timer. I
- 33:54guess you could be longer. Um and then
- 33:56you have a clock that is uh 32.768 kHz
- 34:00coming in. you find out that like we
- 34:02said the time just comes out nicely to
- 34:04this one second tick right and this is
- 34:06kind of why the engineers chose this
- 34:09because now you have a reference that
- 34:10all those humans care about I don't care
- 34:12how many you know micros seconds it
- 34:14takes for things to happen that's not
- 34:16important to me um it's nice to have
- 34:18this external timer that is going to go
- 34:20off every second and now I have a human
- 34:23uh readable time and it's so painful to
- 34:26go between like the worlds of 144
- 34:29megahertz and 1 second that they just
- 34:32decided, look, is this easier to have
- 34:34two different clock domains? One for
- 34:35high frequency things, one for low
- 34:37frequency things.
- 34:40What questions you have?
- 34:44Zero questions. Okay. Uh, as I alluded
- 34:48to, you can count up, you can count
- 34:49down, and then there's a special case
- 34:51where you count up, and then countdown.
- 34:53And here it says auto reload register or
- 34:56auto reload. You can this is called the
- 34:59PR the period register but you hit some
- 35:02value and then you count counting down
- 35:03and so this is such a common function
- 35:05that needs to happen uh there's various
- 35:08waveforms it's a lot easier to compute
- 35:09if you do this that most manufacturers
- 35:11give you this option of you know up down
- 35:13or then up down and then you can also
- 35:15almost go you can go twice as long. So
- 35:18being able to count long things is also
- 35:20important.
- 35:23All right. So now we're going to talk
- 35:24about the things that we add on to a
- 35:26timer. So there's two primary things
- 35:30that we want to measure like how long
- 35:32something takes. We want to capture the
- 35:33interval of time and then we want to
- 35:35compare the alternative modalities. We
- 35:38want to compare to some ex um some known
- 35:41value and say okay go off when these two
- 35:44things are equal then a task or a thing
- 35:47should occur. So measuring interval of
- 35:49time and then basically a stopwatch that
- 35:51would be a stopwatch and then setting an
- 35:53internal timer so that an event goes off
- 35:55at a certain time. Those are the two
- 35:56main modalities that we're looking at.
- 36:00All right. So
- 36:02uh
- 36:04yes okay that's not what I want to talk
- 36:06about. Um
- 36:08so here we have our basic timer
- 36:10structure and I just added this output
- 36:12block uh your uh microcontroller much
- 36:16like how the uh interrupt uh the ru eru
- 36:21uh device we talked about on Monday
- 36:23there's an output layer to the actual uh
- 36:26device and that uh to the timer module
- 36:29and so your timer is going around doing
- 36:31tasks and then this output layer allows
- 36:33you to route things around like set an
- 36:34interrupt or send a a signal out to a
- 36:38GPO pin. Um, we'll we'll talk about that
- 36:41later, but uh that's why that block is
- 36:43there. And so what we're going to do is
- 36:45we're going to add this extra timer. So
- 36:48a capture timer and it can be connected
- 36:50to external event. Usually it's a GPO,
- 36:53but it could be internal. And basically
- 36:55what you're saying is when this thing
- 36:57goes off, when that thing goes high, I
- 36:59want you to do with something. So this
- 37:02thing goes high and you're going to
- 37:03capture uh should yeah I should say c
- 37:06yeah you're going to capture the value
- 37:07here and store it in this register. So
- 37:10pin goes high take the value of this in
- 37:12timer put it into uh the capture
- 37:14register and then do something with it.
- 37:16Does that modality make sense?
- 37:25Make sense so far? Okay.
- 37:28So um again here is uh you're trying to
- 37:31measure the time span of something one
- 37:34rising edge to another rising edge and
- 37:35if you remember this great problem the
- 37:38super interesting problem that I gave
- 37:40you guys didn't you didn't know at the
- 37:43time that if I had just given you the
- 37:45correct module it would have made your
- 37:46life so much easier. So here if you had
- 37:49instead of it was magical P2 it was just
- 37:51a timer capture unit that thing would
- 37:54have been solved effectively easier. I
- 37:56mean you still have to program the whole
- 37:58thing and do everything but that's the
- 37:59uh the idea and so we can walk through
- 38:02an example here. Um
- 38:06so say we have a 16- bit timer and our
- 38:08goal is to measure the intervals between
- 38:11uh the wind speed and we're going to set
- 38:13some arbitrary goals here like I want to
- 38:15go a th00and RPMs or uh 100 or 10 RPMs
- 38:19which these are just arbitrary values
- 38:21that don't have to make sense. And we're
- 38:23given a clock like this. So the the goal
- 38:25here would be, you know, you'd be asked
- 38:27to determine uh the pre-scaler value
- 38:30that would allow you to hit those time
- 38:31functions. So if you're t if you're um
- 38:36and so here we can look at the like
- 38:37what's the maximum speed that you would
- 38:39be going. So the maximum speed would be
- 38:41you're trying to go at a,000 RPMs
- 38:43divided by 60 and then you can figure
- 38:45out the the minimum amount of time you
- 38:47have to go. So you if you went if your
- 38:49clock speed was longer than that then
- 38:51you wouldn't be able to hit that
- 38:52measurement, right? So maybe I slowed my
- 38:54clock real slow. I can't if I go too
- 38:57slow, I can't actually measure small
- 39:00units of time.
- 39:02Probably the counter example which is uh
- 39:04a little bit easier to start with is the
- 39:06minimum speed that you want to go is um
- 39:10oh no sorry this is the this is 6
- 39:12seconds. No no
- 39:18did I write this backwards? Oh 100 RPMs.
- 39:22No. Okay, that's right. So slow speed,
- 39:23so you're going 10 uh 10 RPMs. And so
- 39:27you need to be able to count out in time
- 39:29up to 6 seconds. So the question is,
- 39:31what pre-scaler
- 39:33uh period of time will allow me to
- 39:36measure something as long as 6 seconds?
- 39:38So if I go at 140 uh megahertz, I'll get
- 39:43before I get to 6 seconds, I will
- 39:45overflow. And that's not a that will not
- 39:47allow me to keep track of time. So now
- 39:49you need to choose a pre-scaler that
- 39:51will allow you to at least uh before you
- 39:53overflow that you will uh have a valid
- 39:56value. So you can go through the math
- 39:58and figure out okay uh 2 to the 16th
- 40:01here it is. You plug it back in the
- 40:02equation and you say like my minimum
- 40:04frequency timer uh has to be uh 10.96
- 40:08kHz. So now you have a goal of where
- 40:10you're supposed to be hitting uh your
- 40:12pre-scaler to slow your thing down.
- 40:17uh and you can go through uh to try and
- 40:20find the closest pre-scaler, right?
- 40:21Because you don't get infinite
- 40:22solutions. There's only certain uh power
- 40:25divide by twos that you can have. And so
- 40:28you you go through the math. I think the
- 40:29one the closest one is actually nine,
- 40:31but then it wouldn't give you a valid
- 40:32answer. So then you have to go through
- 40:34uh because it just wouldn't be
- 40:35achievable. And so then you go through
- 40:3710 and you can see okay here is the
- 40:40closest pre-scaler value that would
- 40:42allow me to measure long enough to see a
- 40:466-second long thing. And now from that
- 40:48you can go through and say uh so here's
- 40:50my timer in my pre-scaler value and
- 40:54here's the frequency I want to have out
- 40:55and go through this math and say the the
- 40:57longest tick the ideal tick uh I need to
- 41:01have is 102 microsconds. So that's how
- 41:05long that period of my clock needs to be
- 41:08so that I can count up to 6 seconds. And
- 41:11then here you go through the math to
- 41:12figure out that um so you have your
- 41:16seconds, you have your your your clock
- 41:18period tick and then you can figure out
- 41:20how many counts it will be. So this
- 41:22would be your p this would be your
- 41:23capture compare value uh if you went to
- 41:27six seconds. So if it takes uh the clock
- 41:29the mometer 6 seconds to go around that
- 41:32would be the value and you know if you
- 41:34did incorrectly if you chose the wrong
- 41:36value then if that's larger than what
- 41:38can fit in a 16- bit uh register then
- 41:41you know that it would have overflow and
- 41:43you haven't hit your mark.
- 41:47Yeah, you're all looking at me like I'm
- 41:48crazy. That's fine. I get that.
- 41:52But what questions do you have?
- 42:01Okay. So, you guys can work through the
- 42:04math. I think the real uh trick here is
- 42:07that you have to think about you have to
- 42:09slow down the clock such that as you
- 42:13count up on your counter, you can hit
- 42:16your timing requirements. And if you go
- 42:19too if you don't pre-scale enough, if it
- 42:22if it's too short a time, you'll
- 42:23overflow. be hit before you hit your
- 42:25requirements. The downside would be you
- 42:28could go really really really long,
- 42:30right? You could just say I will always
- 42:31go for the longest pre-scaler, therefore
- 42:33I'll be able to hit my time. But then
- 42:35you might not have enough resolution if
- 42:37needed to measure small periods of time.
- 42:41And while we didn't put a resolution
- 42:42requirement here, we did say that we
- 42:44would need to measure some fast uh
- 42:47interval uh the the thing when it's
- 42:49going the fastest. And here we can say
- 42:51at least I didn't specify an interval
- 42:54but here this would be the value when
- 42:56it's going uh 10,00 RPMs that the
- 42:59counter would the capture counter would
- 43:02record from one tick to another uh from
- 43:04one revolution to another revolution and
- 43:06at least you can say all right that's a
- 43:08good enough value there's enough
- 43:09resolution here but I think the probably
- 43:12the better trade-off would have been to
- 43:13give you a resolution um uh spec where
- 43:17it was you have to be able to measure so
- 43:19many re
- 43:21like 0.1 revolutions per second or
- 43:23something like that.
- 43:26Okay. And then the key idea here is that
- 43:28there's this time trade-off between the
- 43:31resolution at which you set up your uh
- 43:34sorry the the longest duration you can
- 43:36measure versus the resolution. You can't
- 43:38have both. The only way to get better is
- 43:41to increase the the size of your
- 43:43counter. So going to from 16 to 32 bits
- 43:46will certainly give you more tick marks
- 43:48to count and therefore that will
- 43:49increase your resolution but uh on your
- 43:52hardware it's already kind of baked in.
- 43:53You don't have any um
- 43:57uh you don't have any ability to change
- 43:59that. So here you're just saying that
- 44:01you have to make this trade-off between
- 44:03resolution and time.
- 44:11Nobody. You're like either you're like,
- 44:13"Yeah, I got this." Or you're like, "I
- 44:14have no idea what this guy went and
- 44:16talked about."
- 44:21Cool.
- 44:23All right. Well, we're going to go
- 44:24through another example. We'll have a
- 44:25little worksheet. You can play with the
- 44:27math. See see what makes sense. All
- 44:30right. So, the other type of count uh uh
- 44:34event is the compare event. And this is
- 44:36really good not for measuring the
- 44:38passage of time per se, but controlling
- 44:40motors and those sorts of things. So
- 44:42here um
- 44:45uh yeah, so it's really about
- 44:47controlling motors and having pulse with
- 44:49modulation and being able to control
- 44:51units of time very precisely. That's
- 44:53generally the goal. Um so here we go
- 44:56back to our basic structure. We have a
- 44:58clock coming in, a pre-scaler. We have a
- 45:00timer that's either going to count up or
- 45:01countdown. Uh we have some periods so
- 45:03that we know it's going to reset. And
- 45:05then we have some event controller on
- 45:07the output. And then we're going to add
- 45:08this uh compare timer. And so what
- 45:12happens is when these two things are
- 45:13equal, then some event occurs. Um so
- 45:16there's no prompting. There's no trigger
- 45:18from the external world. We're just
- 45:20going to say this thing starts at zero
- 45:21and then I'm going to compare to some
- 45:23value. Uh and then you're going to do an
- 45:25output. Now you could argue, wait a
- 45:27second, why not just uh control your pir
- 45:30your um period register? that's
- 45:34effectively the same. But this allows
- 45:36you to, as we'll see later, is you can
- 45:38vary your compare register while keeping
- 45:41the period the same. So it's like you
- 45:43could vary your period the whole time,
- 45:45but that's only one degree of
- 45:46flexibility. We're going to see later
- 45:48that you're going to want to do things
- 45:49like um control when a pulse goes up,
- 45:53not just when uh a pulse changes. So you
- 45:55get to have more flexibility in time
- 45:58control with this extra register.
- 46:02And so the classic example here is pulse
- 46:04switch modulation. So we'll just show
- 46:05you how it's done for an LED.
- 46:15All right. So in this simulation, does
- 46:18duty cycle work? Duty cycle. Yeah, duty
- 46:20cycle works. Cool. Sometimes it doesn't.
- 46:24Uh so this is just an LED is supposed to
- 46:25be blinking. And here the idea of duty
- 46:27cycle
- 46:29is just this width here. I can't really
- 46:32see but just this width here. Usually
- 46:35duty cycle is determined by the positive
- 46:38period of time. It's not you could do
- 46:41the negative period of time if you
- 46:42wanted but you should let's just assume
- 46:44it's always the positive period of time
- 46:45unless somebody uh explains why or tells
- 46:48you what you're doing. Um, but it's
- 46:50usually this positive period of time and
- 46:52you can go and control the duty cycle.
- 46:54And that's really what that compare
- 46:55register is doing. It's allowing you to
- 46:57control this pulse width here. And why
- 47:00that's particularly important is when
- 47:02you're doing things like LED dimming.
- 47:04So, can you see here? Maybe this
- 47:06projector is good enough. Right? If
- 47:09effectively that looks dimmer to you,
- 47:11right? And if I go with my duty cycle up
- 47:14here now, that would appear brighter.
- 47:17And that's different than trying to do
- 47:19just off and on. So here I'm using time
- 47:21to control the dimming of LEDs. You guys
- 47:25see this in 270?
- 47:28No. Okay. 215. No, you wouldn't do it in
- 47:30215. So this is like how every one of
- 47:33your electronic devices dims. It's too
- 47:35painful to control voltage, right? You
- 47:38got a little microcontroller. Again,
- 47:39it's only good at time. it doesn't it's
- 47:42not really good at like sending out a
- 47:43particular voltage or even a particular
- 47:45current to to dim an LED. So what
- 47:48they're doing is they're just uh have
- 47:50they set up some period and they are
- 47:53going to change the duty cycle. This is
- 47:54where you get uh slowly blinking or or
- 47:57heartbeat things. And the trick is that
- 48:00just like this they go faster than you
- 48:02can see. So as long as you're going like
- 48:04a kilohertz humans can't tell that it's
- 48:06blinking for you. It just looks like
- 48:08it's dim dimmer or brighter.
- 48:17Any questions?
- 48:25Yes.
- 48:30>> Why we need the compare? So, um, as
- 48:34we'll see in a second, the compare
- 48:36register allows us to do things like,
- 48:38uh, so we have a period. You can see the
- 48:41period there, right? So, I can stop
- 48:43here. And so, we have a period from like
- 48:46this node to this node, right? But how
- 48:49long should this pin be high?
- 48:53Right? It's got to be high for some
- 48:55period of time. And I suppose you could
- 48:57like dynamically change the period
- 49:00register so that it's like off for this
- 49:02period of time and then you quickly go
- 49:04and write in uh how long it should be
- 49:06high and back and forth. That's going to
- 49:07consume a lot of energy and effort on
- 49:10the core. So instead you can set up by
- 49:13setting up the period between here and
- 49:15here and then the compare register
- 49:18literally has the number of clock ticks
- 49:21in it from here to here is the value
- 49:24that's stored in that register and then
- 49:26it's going to go high and low and so
- 49:27we'll talk about that mechanism a little
- 49:28bit but that compare register is
- 49:31changing is effectively the same as duty
- 49:32cycle in the simulation.
- 49:38So here I mean duty cycle is usually
- 49:400ero to one but there's a mapping here
- 49:42between duty cycle and the count value
- 49:44the digital value that's stored in the
- 49:46compare register. So here the compare
- 49:49register would have a high value and
- 49:51when the duty cycle goes down in this
- 49:52example it would have a low value and
- 49:54you can get to zero. Yeah you can get to
- 49:56zero in this example you could get to
- 49:58zero in your devices as well.
- 50:04What other questions you have?
- 50:11Okay.
- 50:15And this is almost exactly how motors
- 50:18almost all motors are controlled as
- 50:19well.
- 50:22I guess I should have done this slide
- 50:24first. Maybe that would have been
- 50:25helpful. So here um we have uh a period
- 50:29that's been set up and then the uh
- 50:32reference signal here we'll call uh
- 50:34which is your compare register. So this
- 50:36reference signal basically when that and
- 50:39the count equal each other then some
- 50:41event occurs and you get to control
- 50:42whether that is the pulse goes high the
- 50:44pulse goes low. There's different
- 50:47configurations but let's just say it's
- 50:48the pulse goes high. So when reference
- 50:50signal equals count, then you go to T
- 50:52on. And then while it's above, you're
- 50:54on. And then when you go below, when it
- 50:56overflows at your period, it goes back
- 50:58down. And so now you're doing this
- 51:00waveform up and down. And um yeah, and
- 51:04uh this can be as one of those alternate
- 51:06functions for your GPO pins. You could
- 51:09send this signal out a GPO pin. Here
- 51:11we're just in still in the timer. And
- 51:14then there's an output block on your
- 51:16device that controls how it's being
- 51:17routed. But for now, it's just uh a
- 51:20signal that's going high or low. And you
- 51:23could do this. This is just a digital
- 51:24comparator that's just going high uh
- 51:26high or low. Okay.
- 51:29So, we'll walk through maybe a little
- 51:31bit more detailed example. Um because
- 51:33there's some small little caveats here.
- 51:35So, here we have a timer or a clock spin
- 51:37foot spin in here. Here we're stepping
- 51:39up. We zoomed way in in time. So, that's
- 51:42why we can actually see the stair steps
- 51:43now. And before we couldn't. Um, and so
- 51:47here this is the the period register.
- 51:52Um, and we're going to we're counting up
- 51:54counting counting up. And so here the
- 51:56trick is that it's uh P R + one times
- 51:59the clock period. So what you set this
- 52:01to a certain frequency 1 over F, you get
- 52:04your period. And then so that's the tick
- 52:06period. So we'll have to have some good
- 52:08nomenclature between what is the clock
- 52:10period versus what is the period of the
- 52:12timer. Um, and then that's just because
- 52:15the counting always starts at zero. So
- 52:17you're including zero essentially that
- 52:19transition step uh in your count up. So
- 52:21you're going to count up in this case.
- 52:23Again, you can do the opposite. You can
- 52:25count down. You start at six or some
- 52:27value and then you're going to include
- 52:29zero. So it's the the period register
- 52:31plus one times the clock period equals
- 52:34the period of the uh waveform. Um, okay.
- 52:40And same for well time clock up clock
- 52:44down uh this mode. The only caveat that
- 52:46gets me and every other student is that
- 52:49um you don't count uh the end uh
- 52:54conditions here. You don't count six or
- 52:56zero or six in this example. You're
- 52:57going to count 0 1 2 3 4 up to six and
- 52:59then back down. So you're not double
- 53:01counting six. You're not double counting
- 53:02zero. And that's why the period for up
- 53:05down counting is actually the same as
- 53:07your um it should say uh pr instead of a
- 53:11a r but that is um the trick the
- 53:15conceptual trick that gets people each
- 53:17time for uh center line mode.
- 53:22Okay.
- 53:24Um and then to how much time? Okay, we
- 53:26got time. Uh so then to bring it back to
- 53:28the CCR or the the compare register.
- 53:32So here we're going to go through we're
- 53:34going to set the the PR to six and then
- 53:36we have a compare and so here at some
- 53:39time right when this thing equals from
- 53:42uh equals three then it's going to have
- 53:44a transition it's going to overflow. So
- 53:46this is um low if true and then you can
- 53:49go through the math to kind of uh figure
- 53:51out what the clock period or what the
- 53:54period is and what the duty cycle is.
- 53:58I don't know why that happened. And then
- 54:00uh you can go high if true. This is kind
- 54:02of like normal case of how I think about
- 54:03things. There are different modes and
- 54:05again this is just your output mode to
- 54:08do that. Uh same same math. It's just
- 54:11you have to think uh the only math
- 54:12that's different is your duty cycle
- 54:13because it diff typically is saying that
- 54:16duty cycle is your high time. So you
- 54:18just have to remember if you're high
- 54:20true or low true uh how that affects
- 54:22your duty cycle. And then the the trick
- 54:25here is to make things brighter. you
- 54:26just change your CCR or your CR and now
- 54:30you've shortened your pulse width. Um,
- 54:33and so now in this case the LED would be
- 54:35dimmer
- 54:38and here you're just on for two cycles
- 54:40in this case. So that's why your duty
- 54:41cycle is lower
- 54:45and same for CCR mode. Okay, any
- 54:48questions before we do a little
- 54:49worksheet problem?
- 54:54Here, I'll put that up.
- 55:00You guys are very quiet. I think that
- 55:02exam scared the heck out of you. You
- 55:04shouldn't be too worried. I promise you.
- 55:07Just pass those down that way.
- 55:13All right.
- 55:15Thank you.
- 55:18That happens every time. You You scare
- 55:20me. That's what's happening here. Take
- 55:22that one. Thank you.
- 55:30Okay, you pass that way or I'll take
- 55:32this one over to her.
- 55:35>> I gave you the the nicely crinkled one.
- 55:37Sorry about that.
- 56:00>> [clears throat]
- 56:12>> All right. So in this problem
- 56:15uh you've been given the task of
- 56:17creating some duty cycle here
- 56:20and you have to figure out where your
- 56:22pre-scaler is going to be, your uh
- 56:26period register and your uh capture or
- 56:28your compare register. Now the trick is
- 56:31there are multiple solutions.
- 56:34It's not like there's only one possible
- 56:36solution because it's a trade-off
- 56:37between resolution and time. So if you
- 56:41uh go for a very long potential period
- 56:45uh or sorry if you configure your timer
- 56:47such that it could go for a very long
- 56:49time you would give up resolution even
- 56:52uh or if you went for just the minimum
- 56:54period of time then you would have high
- 56:55resolution. We don't really care about
- 56:57that. We're you said is you have to get
- 56:59a 10% duty cycle uh with 10
- 57:02milliseconds. So it's giving you the
- 57:03spec and you have uh where does it say?
- 57:08a 16 megahertz timer. So you have a
- 57:10clock that you have to slow down such
- 57:13that you can hit a period. It could be
- 57:15exactly the period to hit your period
- 57:18and then you have to go uh it doesn't
- 57:20have sorry it doesn't have to be the
- 57:22length of a 16- bit timer. It could be
- 57:24some period value. Just has to fit in a
- 57:2716- bit timer. And so once you figured
- 57:29out your period value then you had to
- 57:30figure out the math to come up with a 10
- 57:33millisecond uh chunk of time. Okay.
- 57:38And so as you work through this problem,
- 57:40the trick is really you're trying to
- 57:41make this. So I would say start figuring
- 57:45out what pre-scaler would give you
- 57:48pre-scaler and period register value
- 57:50could give you 10 milliseconds. Okay?
- 57:54And then work over to figure out what um
- 57:57CCR value will give you 1 millisecond.
- 58:14And there. There we go.
- 58:52You got it.
- 58:56Uh please.
- 59:43Wait.
- 1:00:26I'm wondering
- 1:00:41[snorts]
- 1:01:16What are you doing?
- 1:01:27Good job.
- 1:01:53Yeah.
- 1:02:19Awesome.
- 1:02:42That would be easy.
- 1:03:29Oh yeah.
- 1:03:35We have
- 1:03:40Amen.
- 1:03:54God.
- 1:04:06[clears throat]
- 1:04:20Yes.
- 1:04:47What's going to be
- 1:04:51Okay.
- 1:04:54[clears throat]
- 1:05:01Yeah.
- 1:05:31Heat. Heat.
- 1:05:47Sorry.
- 1:06:37What's the answer? It's good about
- 1:06:52>> All right, I know we're running out of
- 1:06:53time. Uh D answers.
About this transcript
This page contains the full transcript of EECS 373 - Fall 2026 - Lecture 9: "Timers: Capture & Compare" by Interactive Sensing and Computing Lab , generated from the public captions YouTube serves with the video. The transcript has 9,876 words across 1,410 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.