YouTube2Text

EECS 373 - Fall 2026 - Lecture 9: "Timers: Capture & Compare" — Transcript

by Interactive Sensing and Computing Lab · 9,876 words · 1,410 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Okay, we finally are ready to go. I've
  2. 0:04solved all the world's AV problems.
  3. 0:06We're ready. All right, so well, they're
  4. 0:10not released. They'll be released right
  5. 0:11after class. We'll talk about grades.
  6. 0:15[laughter]
  7. 0:16Homework uh four is due on Monday.
  8. 0:20And then we're going to do slide
  9. 0:21ideation slides will be due next
  10. 0:23Thursday. We'll talk about that and then
  11. 0:25team formation is next week. So for the
  12. 0:28ideation slides, we'll release the
  13. 0:29details tonight. Basically, your job is
  14. 0:31to create just three slides. Uh
  15. 0:34individual slides will have a upload
  16. 0:36folder for you and it's supposed to be
  17. 0:38like not a lot of words. Like there's
  18. 0:40got to be an image, less than 100 words,
  19. 0:43something that describes your particular
  20. 0:45project. And if you want, you can go and
  21. 0:48put a little star up there and that will
  22. 0:50like key to me that you want to present
  23. 0:52this particular thing. But the point is
  24. 0:54that you have to start thinking about
  25. 0:56what projects you want to work on. And
  26. 0:57if this is a forcing function to make
  27. 0:59you say, "Oh, this is the sort of thing
  28. 1:01I'd like to do." And then you can also
  29. 1:02look through other people's projects and
  30. 1:04be like, "Okay, I see uh other people
  31. 1:07are at least aligned with my vision
  32. 1:08here." Uh that's too loud. Let's see
  33. 1:14better.
  34. 1:16Okay.
  35. 1:18And then we'll serve pizza. It'll be
  36. 1:21there. uh endow one of the rooms we took
  37. 1:24the exam in. And then uh the next I
  38. 1:27think that date's right, the next
  39. 1:28Tuesday, you need to register your team.
  40. 1:30So we'll have a form online. You'll just
  41. 1:32say this is our team. This is our team
  42. 1:34name. This is what we're going to do. Uh
  43. 1:36if you're having trouble uh finding a
  44. 1:38teammate, you guys should meet with me
  45. 1:40after class on that uh previous Monday.
  46. 1:47Okay. Uh and then uh after so I'm just
  47. 1:51also laying out what the project is
  48. 1:53going to be. Uh the first next step is
  49. 1:55after the uh team formation then you
  50. 1:57have to do a project proposal. I don't
  51. 2:00think these are online yet in which you
  52. 2:02will basically there's a description for
  53. 2:04the project. You will write a brief
  54. 2:05statement that is really me forcing you
  55. 2:08to think through what is this project
  56. 2:10going to be? What components do you
  57. 2:11need? What's its goal? And then there'll
  58. 2:14be one-on-one meetings with me or James
  59. 2:18uh to go over your project and mostly
  60. 2:20give you feasibility feedback
  61. 2:23um and help you think about like okay
  62. 2:25here are the parts we have get you guys
  63. 2:27started on your projects. So any
  64. 2:30questions about those parts?
  65. 2:37Okay. So background of timers. Uh, and
  66. 2:40then later on we're going to talk about
  67. 2:42cycic and Ashley this semester. We're
  68. 2:43not going to talk about virtual timers
  69. 2:45anymore in this course because 473
  70. 2:47doesn't use them anymore in the same way
  71. 2:48that they did. All right. So, the best
  72. 2:52source of information is there is a
  73. 2:53separate manual just for the capture
  74. 2:56compare timers. This is more
  75. 2:58specifically for your device. And you
  76. 3:00can more or less be at the point now
  77. 3:02where you could read through this
  78. 3:03manual. It's long, sure, but um for the
  79. 3:08uh unit that you're using, the capture
  80. 3:09and compare unit 4, all the information
  81. 3:12you need is there. I'm going to try and
  82. 3:13give you the background so that it's
  83. 3:15easy to interpret this
  84. 3:18and then your labs are going to have you
  85. 3:20actually exercise the details of this
  86. 3:22unit. All right, so let's back up for a
  87. 3:25second. So, we have timers and we think
  88. 3:27about timekeeping in general. We have
  89. 3:29things like stopwatches and alarm clocks
  90. 3:33and world zone time zones and all those
  91. 3:36sorts of things. So there's lots of
  92. 3:37these passage of time things that we
  93. 3:40have to keep track of stopwatches. Um
  94. 3:43but then that's kind of like our
  95. 3:44userfacing point of view. But then
  96. 3:46understanding and manipulating time in
  97. 3:48the embedded systems world sometimes you
  98. 3:50have to make uh give information to
  99. 3:52users in their world. But most of it is
  100. 3:55controlling things. And I think the
  101. 3:57conceptual idea is that computers uh
  102. 4:00when interfacing with the world are bad
  103. 4:02at almost everything like they don't
  104. 4:05understand analog. They only understand
  105. 4:07voltage at best, right? But the one
  106. 4:10advantage they have is they are really
  107. 4:11good at controlling time, right? So that
  108. 4:13you have a clock, it goes extremely fast
  109. 4:16and you can manipulate that. So a lot of
  110. 4:19these devices that we're going to
  111. 4:21interact with are controlled through
  112. 4:23some bit of time. So we talked about
  113. 4:25servers servos before and that's you're
  114. 4:27doing a little pulse that moves the
  115. 4:29servo to one location. You're encoding
  116. 4:31that position as a function of pulse
  117. 4:34timing.
  118. 4:35Uh we'll talk I think in lab you'll do
  119. 4:38uh DC motors and those are using
  120. 4:41pulsewidth modulation to control the
  121. 4:43speed of motors effectively how much
  122. 4:46current is going into it and LEDs. So
  123. 4:49all these things are really about
  124. 4:50controlling time because that's the only
  125. 4:52thing a computer or microcontroller is
  126. 4:54good at.
  127. 4:56So really that's what we're trying to
  128. 4:57get you guys the ability to do. And this
  129. 4:59word pulse with have you all heard
  130. 5:01pulsewith modulation before? Probably
  131. 5:03not one some people. Yes. Okay. So
  132. 5:06pulsewidth modulation is kind of the
  133. 5:07goal of where we're going to be at the
  134. 5:09end of this lecture. And it's really
  135. 5:11just well we'll talk about it but it's
  136. 5:13really just pulse with modulation is
  137. 5:15controlling the width of pulses to
  138. 5:17control uh timing information.
  139. 5:20All right.
  140. 5:22Uh so okay uh what am I trying to say
  141. 5:25here? All right. I kind of repeated all
  142. 5:26this but here these are the things you
  143. 5:29can do with it. Um so how does a
  144. 5:32computer measure time? So typically
  145. 5:33there's an event that goes off right?
  146. 5:35You have some main thread, you have an
  147. 5:37interrupt that's controlling time, but
  148. 5:39that's what it does when it has time. So
  149. 5:42my question to you is uh what is the
  150. 5:45electrical method that electronics use
  151. 5:48or the method electronics use to measure
  152. 5:50the passage of time? It's probably
  153. 5:52useful to think about how humans do it.
  154. 5:54So we look at the natural world. There
  155. 5:56has to be something in the natural world
  156. 5:58for us to use for timekeeping. So rise
  157. 6:00and fall of the sun, spring mass const
  158. 6:03constants for uh clocks. And so my
  159. 6:06question to you is what is the physical
  160. 6:08principle that electronics use to
  161. 6:11measure the passage of time?
  162. 6:14All right, take a take a minute to chat.
  163. 6:35Yeah.
  164. 6:49Heat. Heat.
  165. 6:59Heat. Heat.
  166. 7:33All right. What do we think? How do
  167. 7:35computers measure the passage of time?
  168. 7:41generates
  169. 7:44>> a magic. It's true. There are magic
  170. 7:46crystals. I know we read around a lot of
  171. 7:48fantasy books when we were kids, but
  172. 7:49there are magic crystals, but that's not
  173. 7:52it's very that is the object. But what's
  174. 7:54the mechanism that we're that we're
  175. 7:56using? Yeah.
  176. 7:57>> Electricity.
  177. 7:58>> What's that?
  178. 7:59>> Electricity.
  179. 8:01>> Yes. So, uh there's different
  180. 8:03mechanisms, but really it's there is a
  181. 8:06pislectric material in that crystal, the
  182. 8:08quartz crystal, and it's vibration. We
  183. 8:10are looking at the pro the propagation
  184. 8:13of a physical wave through that piece of
  185. 8:15crystal going back and forth and it's
  186. 8:17the pisoelectric effect which goes from
  187. 8:19electricity which the computer at least
  188. 8:21can measure pulsing that crystal you
  189. 8:23we're looking at the delay in time as a
  190. 8:26wave goes across that crystal structure
  191. 8:28and it just happens that our species
  192. 8:29figured out how to make that crystal
  193. 8:31very accurate and repeatable from one
  194. 8:33device to another device. But these
  195. 8:35things seem magical, right? But it
  196. 8:37really is just this just a time delay of
  197. 8:40a physical wave oscillating across a
  198. 8:42piece of uh crystal. I still think
  199. 8:44that's amazing. Um yeah. Okay. So,
  200. 8:49anatomy of a timer. So, we start with
  201. 8:52some crystal. All right. There's some
  202. 8:54clock divider. We're not really going to
  203. 8:55get into too much detail about this, but
  204. 8:57the analog parts of it, but basically
  205. 8:59this crystal here along with some
  206. 9:01digital internal circuitry will turn it
  207. 9:03into a square wave. Then we're going to
  208. 9:05have a pre-scaler and a counter. So
  209. 9:06these are the things that we're more or
  210. 9:08less going to learn about today. Capture
  211. 9:10and compare components. And this is all
  212. 9:13basically at the assembly level where
  213. 9:16this mod or these are the RTL level I
  214. 9:18should say where the module here is um
  215. 9:21controlling the underlying function and
  216. 9:23then we get up to the sub layer where we
  217. 9:25have assembly and then the abstract
  218. 9:27layer and then the examples we talked
  219. 9:29about before with you know from the
  220. 9:31iPhone that's up at the application
  221. 9:33layer and there are ways at least
  222. 9:35mentioned them so that you guys are
  223. 9:36aware to virtualize your timer so that
  224. 9:38you can have one timer that can do um
  225. 9:41you know any number a scalable amount of
  226. 9:43time with some um caveats.
  227. 9:48Okay. So, yeah. So, here we're starting
  228. 9:50with the lowest physics going all the
  229. 9:52way up into uh different applications.
  230. 9:54So, we'll start off with the crystal as
  231. 9:56you guys pointed out. Um
  232. 9:59there are different types of crystals to
  233. 10:01be aware of. So, these are typically
  234. 10:03quartz. Um they're uh low frequency. You
  235. 10:07can see some with leads. Those are
  236. 10:08called throughhole. And most modern ones
  237. 10:10are going to be um uh surface mount.
  238. 10:13They're rarely are they ever in the C
  239. 10:16the uh microcontroller.
  240. 10:19I can't think of a time when I've ever
  241. 10:21seen that. Although I have seen them
  242. 10:22integrated with real-time clocks. Um
  243. 10:25usually outside the device. And here you
  244. 10:28can see that this is for your XM XMC
  245. 10:324770 your Infinia chip. So there's
  246. 10:34external inputs here for your crystal.
  247. 10:37Um, and these are happen to be really
  248. 10:39good at lower frequencies. So, a common
  249. 10:41one you're going to be in the range of
  250. 10:43like 10 kHz up to maybe 100 kilhertz or
  251. 10:46so. And a very common one is 32.768
  252. 10:50kHz. And we'll see why in a little bit.
  253. 10:52But your device has a specific module
  254. 10:55just to interface with this external low
  255. 10:58frequency clock.
  256. 11:02And then the other type would be higher
  257. 11:04frequency uh resonators. So these are
  258. 11:06like ceramic uh saw uh crystals. Um they
  259. 11:11work on slightly different principle uh
  260. 11:13surface acoustic waves than the uh
  261. 11:16quartz crystals we talked about earlier.
  262. 11:17And they were really in the 10 meghertz
  263. 11:20up to 100 meghertz range. That's
  264. 11:22typically what these things can be made
  265. 11:25to. So there is kind of like this this
  266. 11:27gap this between like a 100 kilhertz and
  267. 11:31or sorry 1 megahertz and 10 kHz or so or
  268. 11:34or 10 meghertz or so where it's hard to
  269. 11:36find crystals or oscillators but um so
  270. 11:40that's why the the community generally
  271. 11:42has these two types and your device has
  272. 11:45an input for that. So now we have a way
  273. 11:47to we can control the clock how fast
  274. 11:50your um processor is going, how fast the
  275. 11:53advanced peripheral bus is going and
  276. 11:55your timers and your peripherals u by
  277. 11:57choosing the correct clock frequency.
  278. 12:01Any questions?
  279. 12:04Okay.
  280. 12:06And then uh it's not you don't have one
  281. 12:08on your device, but it's good to know
  282. 12:10just embedded systems that there are
  283. 12:11many microcontrollers that have
  284. 12:12digitally controlled oscillators, which
  285. 12:14is basically something like a relaxation
  286. 12:16oscillator, a little or ring oscillator,
  287. 12:18a little thing on chip where they can
  288. 12:20have registers and you can dynamically
  289. 12:22control the speed of your processor,
  290. 12:23which is really nice for low power
  291. 12:25things. So you have to do something very
  292. 12:26quickly because you have a timing
  293. 12:28constraint, you can go into high power
  294. 12:29mode, otherwise you can go down to uh a
  295. 12:32lower power mode if you if you needed
  296. 12:34to. Um there still is that kind of speed
  297. 12:36energy trade-off. Um but there's certain
  298. 12:39reasons why you don't want to draw too
  299. 12:41much current out all the time,
  300. 12:42especially with batteries uh to keep
  301. 12:45them uh operating for a long longest
  302. 12:48time. You don't actually want to pull
  303. 12:49them at their peak current. So there's
  304. 12:51good reasons to control how fast your
  305. 12:54oscillator goes for power reasons. Also,
  306. 12:56if there's different protocol reasons,
  307. 12:57you might want to change your oscillator
  308. 12:59speed. So that is available on many
  309. 13:01microcontrollers, but not on yours. uh
  310. 13:03you have uh two fixed oscillators and
  311. 13:06Xfinion gives them for specific reasons.
  312. 13:08It's only for backup. They uh their
  313. 13:11point of view. I mean, I guess you could
  314. 13:13use them as your primary, but um the
  315. 13:15intent is that you'll have high
  316. 13:17frequency crystals. Uh you have a low
  317. 13:19frequency crystal and those have really
  318. 13:21good time uh parts per million, so
  319. 13:24really good time accuracy. But should
  320. 13:27any of those fail for some reason,
  321. 13:28especially on bootup, uh because they're
  322. 13:31in real-time controls, they really want
  323. 13:33to make sure that your device will boot.
  324. 13:35And so now you they have these backup on
  325. 13:37on internal things that will allow the
  326. 13:39system to boot up and at least throw an
  327. 13:41error or explain why uh there's a
  328. 13:43problem.
  329. 13:46Um yeah, and so those are these little
  330. 13:50backup units here.
  331. 13:52So uh you might notice that um so your
  332. 13:56device and if you look at the data sheet
  333. 13:58it says that it's preferred
  334. 13:59highfrequency crystal is 16 meghertz but
  335. 14:02you may have noticed that all your
  336. 14:03devices run off of uh or sorry your core
  337. 14:06and your buses run off 144 mehz at least
  338. 14:08that's a high speed. So how do you
  339. 14:11create a clock source that's higher than
  340. 14:13the input clock sources?
  341. 14:17Yeah.
  342. 14:22>> Well, you you have the right idea, but
  343. 14:24the word divide got you there. So, the
  344. 14:25input frequency is 16 MHz and they want
  345. 14:29uh 144 MHz. So, how do we go up in
  346. 14:32frequency?
  347. 14:35>> Okay, I gave it away. Yes, you're right.
  348. 14:36Um, there is a multiply function. Uh,
  349. 14:40essentially, it's called a phase lock
  350. 14:41loop. It's e blackmagic. I'll tell you
  351. 14:44that. I should say black magic. is black
  352. 14:46magic too. I don't know. But uh black
  353. 14:48magic for sure. Um I remember when I was
  354. 14:52in graduate school taking a class and
  355. 14:54just being destroyed just like just like
  356. 14:56your exams only I didn't know what was
  357. 14:58going on. And uh because it was all
  358. 15:01control theory eventually figured it
  359. 15:02out. But a phase lock loop essentially
  360. 15:04says here's an incoming waveform. I'm
  361. 15:06going to generate a faster waveform and
  362. 15:09then I'm going to take like every tenth
  363. 15:11edge let's say and compare that to the
  364. 15:13incoming edge. And so now I have this
  365. 15:15mechanism of I'm gonna go faster than I
  366. 15:17need to and every tenth edge I'm going
  367. 15:19to see if I'm aligned and I'm gonna go a
  368. 15:20little bit faster, a little bit slower,
  369. 15:22so I'm aligned with the next clock edge.
  370. 15:23So there is a way to increase your clock
  371. 15:26frequency using this thing called a PLL.
  372. 15:29The drawback is it's higher power and
  373. 15:31higher higher noise. So the there's a
  374. 15:33little bit more jitter in that signal
  375. 15:35that's coming in. Engineers have done a
  376. 15:37great job and made it so it's a
  377. 15:39tractable usable solution, but there are
  378. 15:41trade-offs in it.
  379. 15:45Um, and the last thing I want you to be
  380. 15:46aware of in general is that uh there is
  381. 15:49a relatively well this is simpler than
  382. 15:51the STM32 but relatively complicated
  383. 15:54clocking structure um where on all chips
  384. 15:58the idea of how you would distribute
  385. 16:00your clock to different peripherals and
  386. 16:02throughout the chip is a you know a
  387. 16:04field of study or at least a sub
  388. 16:06community of EES in its own right. Um
  389. 16:10and you're while you're not necessarily
  390. 16:12responsible for knowing all this from
  391. 16:14like an exam or even lab point of view,
  392. 16:16you should good to be aware of um so
  393. 16:18that if you needed to for your project,
  394. 16:21you can manipulate the clock frequency
  395. 16:23of your buses, of your core, of your
  396. 16:25peripheral so that they meet the timing
  397. 16:28needs you have. You don't have to just
  398. 16:29go with a default. Um, one common one
  399. 16:32that I see each year is if somebody
  400. 16:34wanted to do like a VGA HDMI controller
  401. 16:37or so FPGA to uh interface with HDMI. So
  402. 16:41you could directly um uh you know uh
  403. 16:46project on a projector or on a screen
  404. 16:48from your microcontroller. In that case,
  405. 16:50it's usually very advantageous just to
  406. 16:53make the core function of your buses and
  407. 16:56your CPU the exactly the right frequency
  408. 16:59because everything just lines up and
  409. 17:01then you don't have uh weird
  410. 17:02asynchronous
  411. 17:04events. Everything is just lined up with
  412. 17:05your data clock coming in. But just this
  413. 17:09is just good to be aware of.
  414. 17:12And then uh I guess the part I didn't
  415. 17:13say is so here often times you can
  416. 17:15select different sources, you can route
  417. 17:17them different places and then you can
  418. 17:18divide them down or dividing is really
  419. 17:21easier than multiplying but you can
  420. 17:23control them so that different parts get
  421. 17:25different uh frequencies.
  422. 17:28Okay. So now down to the anatomy of a
  423. 17:32basic counter. So um we're going to
  424. 17:35spend a lot of what lab is going to be
  425. 17:37is like the additional parts to the
  426. 17:39counter. We'll start here. So you oh you
  427. 17:43usually have some clock frequency coming
  428. 17:44in um let's just say it's like 100 mehz
  429. 17:48it's going to be whatever say the
  430. 17:50default is on your system could be 144
  431. 17:52MHz the max speed and then they have
  432. 17:54this idea of a pre-scaler so the
  433. 17:56pre-scaler reduces that input clock
  434. 17:59frequency down to something lower
  435. 18:01and that's typically because you might
  436. 18:03be running at say human speeds humans
  437. 18:06it's way too fast to go at 144 meghertz
  438. 18:09or just talking to PWM or sorry using
  439. 18:13PDW PWM to talk to a servo or motor
  440. 18:17controller. It's just unreasonably fast
  441. 18:19to go with that speed. And this also
  442. 18:21allows on the actual peripheral level at
  443. 18:23this timer slice uh you can control the
  444. 18:27um uh duration clock speed to that
  445. 18:30particular timer which gives you a lot
  446. 18:32more flexibility. So each one of your
  447. 18:33modules is going to have this pre-scaler
  448. 18:36um in this particular device. We'll talk
  449. 18:38about pre-scalers in a second, but uh
  450. 18:40the way it's computed is it's just a
  451. 18:42power of two offset. So, we're just
  452. 18:44going to divide by a power of two. Input
  453. 18:46frequency is going to be uh lower output
  454. 18:48frequency.
  455. 18:50Then you have the the counter which is
  456. 18:53just um you know a device that will
  457. 18:55count up and count down and usually
  458. 18:56there's a a timer register associated
  459. 18:58with it. So now you can keep track of
  460. 19:01the current time. uh you can write into
  461. 19:03it, read out of it, do all those things.
  462. 19:06And then almost every counter that
  463. 19:08you'll ever see has something along the
  464. 19:10lines of a reload register or a period
  465. 19:13register where after a certain period of
  466. 19:16clock cycles, it will it holds a value
  467. 19:19of what the counter should reset to. So
  468. 19:21your say your counter counts up to, you
  469. 19:24know, 10 or something, it hits that and
  470. 19:26then it sets back to zero. The
  471. 19:28alternative mode is when you uh you
  472. 19:29could be counting down from some high
  473. 19:31number, you count down to zero and then
  474. 19:32when it hits zero, that number gets
  475. 19:35loaded back in. So you can keep cycling
  476. 19:37between some period of time. It doesn't
  477. 19:40have to be just whatever the pre-scaler
  478. 19:42gave you.
  479. 19:44Any questions? We'll go over each
  480. 19:46component in a little bit.
  481. 19:49>> Yeah.
  482. 19:55>> Yeah.
  483. 20:05It's it's dictated by external clock.
  484. 20:08That's a good question. It doesn't have
  485. 20:10to be that way, but there's good
  486. 20:11practical reasons for it. Um,
  487. 20:15uh, let's see. This one is fine. Um, so
  488. 20:19you can see all the sources there. So
  489. 20:21the external clocks are much more
  490. 20:22accurate and it's really you should
  491. 20:24think of more of like a clock source. So
  492. 20:26the crystal doesn't give out a square
  493. 20:28wave. It gives out this horrible analog
  494. 20:30wave that kind of has this period to it
  495. 20:31but is very very precise. You can have
  496. 20:34an external clock. So that crystal/c
  497. 20:37clock in that pin could be driven by
  498. 20:40some other clock. Kind of rare that you
  499. 20:42would want that but it could be. Then
  500. 20:45internally you have this say we go back
  501. 20:46to the analog solution where you have a
  502. 20:47crystal the uh clock HP block up there
  503. 20:51turns that analog signal into a digital
  504. 20:53signal that drives the rest of the
  505. 20:54device. That's the normal way. There's
  506. 20:57this kind of backup way where you could
  507. 20:59have the internal clocks the relaxation
  508. 21:02oscillator those things control the
  509. 21:04timing but um due to the variations in
  510. 21:08manufacturing process as well as
  511. 21:09temperature those are less precise and
  512. 21:12they're also higher power. So typically
  513. 21:14almost all embedded systems they use an
  514. 21:16external clock or clock source let's say
  515. 21:20uh we got off topic well we're not off
  516. 21:22topic but it was a good topic but
  517. 21:23questions about this
  518. 21:26reload
  519. 21:31>> um no uh the p the period register is
  520. 21:35just is some other value we'll see it in
  521. 21:38a little bit but it's it's you have the
  522. 21:40so on your devices 16- bit timers it
  523. 21:42could the other uh on other devices, but
  524. 21:45it will count up to 16 and then it would
  525. 21:47overflow naturally. But this is a
  526. 21:48register that forces overflow at a
  527. 21:50different or reset at a different uh
  528. 21:52time or different count. So you get to
  529. 21:54control the period that occurs in terms
  530. 21:57of ticks. Yeah.
  531. 22:02>> No, I'll show I'll show an example of
  532. 22:04that in just a second. It's only
  533. 22:06dividing. It doesn't multiply. So phase
  534. 22:08loop is for multiplying. This is only
  535. 22:10dividing.
  536. 22:17Okay.
  537. 22:18So going to the uh clock divider. So uh
  538. 22:22this is I'll this is a basic structure
  539. 22:26that this allows the input thing. You go
  540. 22:28through a flip-flop and then you divide
  541. 22:30by two divide by four divide by eight
  542. 22:32and then you could tap off at different
  543. 22:34uh signals here. You imagine you could
  544. 22:36have a MX on top and then you can choose
  545. 22:38do I want to select the divide by two by
  546. 22:40a four by eight by n whatever whatever
  547. 22:42the largest value is and I think on your
  548. 22:44device might be 32 I'm not exactly sure
  549. 22:46off the top of my head um so that is
  550. 22:49it's a very simple functional block so
  551. 22:51they're uh easy to implement on device
  552. 22:54and that's why each one of your timer
  553. 22:55gets one um there is a counter example
  554. 22:59uh which is called a pre-scaler now
  555. 23:01clock divider and pre-scaler uh these
  556. 23:03terms are getting overused but A
  557. 23:05pre-scaler allows you to not choose
  558. 23:08increments of four. It's a more
  559. 23:10complicated analog or digital circuit
  560. 23:12and it can do any incremental value in
  561. 23:15between there. So it doesn't have to be
  562. 23:16just a divide by two. The problem for
  563. 23:20you guys at least going forward when you
  564. 23:22go out into industry is that uh there's
  565. 23:25a documentation warning here in the
  566. 23:26sense that Infinion calls this structure
  567. 23:29which I would call a clock divider a
  568. 23:31pre-scaler. Now there's not wrong in the
  569. 23:34grand scheme of how we talk about
  570. 23:36electronics, okay? But I would say that
  571. 23:39if you went to EE textbook, uh the word
  572. 23:42pre-scaler is probably not defined by
  573. 23:44this circuit. This circuit is probably a
  574. 23:45clock divider. But it's more not that it
  575. 23:48will ever affect you in this class. It's
  576. 23:50more of a statement if you go out into
  577. 23:51industry and you're like, how is it
  578. 23:53possible that a pre-scaler can go
  579. 23:55anything but a power of two? Well, uh
  580. 23:57this confusion, this nomenclature
  581. 23:59confusion is the reason.
  582. 24:02Um, let me see if this will actually
  583. 24:04work.
  584. 24:06Nope. Cool.
  585. 24:09Uh, okay. So, just to give an example
  586. 24:13here. So, this is just a normal clock
  587. 24:15divider example.
  588. 24:18There we go. And so, here we have a
  589. 24:20clock at some frequency coming in. And
  590. 24:22we can see that each one of these taps
  591. 24:26here, here, or here, right? These are
  592. 24:29just a divided version of that thing.
  593. 24:32And so now you get to control. You're
  594. 24:33going to mux in essentially when you
  595. 24:36select your pre-scaler, you're mxing in
  596. 24:38one of these uh n taps. And now you can
  597. 24:42slow down your clock.
  598. 24:47What do you think?
  599. 24:49I see some people are like, "Yep, I'm
  600. 24:51bored." Some people are like, "I have no
  601. 24:52idea what this guy's talking about."
  602. 24:57>> Yeah. Can you explain how?
  603. 25:01>> So, uh, basically, uh, so if you just
  604. 25:05look at, so you look at clock goes in
  605. 25:07the Q, right? And it takes one clock
  606. 25:09cycle to feed back into D. And so now
  607. 25:12you're going to eat up two slot for
  608. 25:14every one uh, every two clock edges that
  609. 25:18go in, one clock edge comes out, one
  610. 25:20transition comes out the other side. So
  611. 25:22just the structure itself
  612. 25:25um, you know, uh, slows things down. And
  613. 25:28you can kind of see here we go slower.
  614. 25:30So you can kind of see the truth table
  615. 25:32here. But you see there's a let's see
  616. 25:34what happened. There's a click here.
  617. 25:37Come on.
  618. 25:39So it just takes two clicks on here to
  619. 25:42get one click on the other side. And
  620. 25:44we're just going to And the nice thing
  621. 25:45is from a hardware point of view that's
  622. 25:47so cheap. It's so cheap to have D
  623. 25:49flip-flops. Um as many as you want on
  624. 25:51device. So they don't care about putting
  625. 25:54any of those uh to give you as many
  626. 25:56clock division opportunities as you
  627. 25:58like. Okay.
  628. 26:02Okay.
  629. 26:06And all the I think all the um what do
  630. 26:09you call it? Uh
  631. 26:11links in fall in uh the PowerPoint I
  632. 26:13sent or the PDF I sent you are broken.
  633. 26:15So I will fix them tonight and uh and uh
  634. 26:19you can go play with these if you would
  635. 26:20like. Uh, so a counter module, there are
  636. 26:23lots of different topologies of a
  637. 26:25counter module, but the core
  638. 26:26essententral thing is it's just a thing
  639. 26:28that's going to count up and count down.
  640. 26:30You learned about these in 270, correct?
  641. 26:33Yes. Fantastic. Um, and so the topology,
  642. 26:37I don't care whatever topology the
  643. 26:38manufacturer wants to use. The real
  644. 26:40important things is that you can count
  645. 26:41up, count down, typically uh you can
  646. 26:44load data into them, right? You can go
  647. 26:46in through the D flip-flops. And it's
  648. 26:48not shown here in the structure, but uh
  649. 26:50and it may not be obvious, but almost
  650. 26:52every D flip-flop on your device is
  651. 26:55connected through a separate set of
  652. 26:57wires that allow you to shift into them.
  653. 27:01And this is what your J-TAG debugger is.
  654. 27:03This when you read out the memory state
  655. 27:04or the of your registers, you are
  656. 27:06literally just uh putting all the the
  657. 27:09flip-flops into a shift register mode
  658. 27:11where data is propagated through each
  659. 27:13one. Uh so there's that functionality
  660. 27:15and here you can also just load values
  661. 27:17in and you could have any number uh you
  662. 27:19want for a counter on your particular
  663. 27:21devices they're almost always uh 16 bits
  664. 27:24unless um there's a slightly different
  665. 27:26version uh for the uh CCUM8
  666. 27:30version of the timer on your device. Um
  667. 27:33and so here's just an example of a
  668. 27:35counter. Uh we have a clock coming in
  669. 27:37and we're just showing this is a binary
  670. 27:39to digital converter and this is showing
  671. 27:40that we're counting up and we can hold
  672. 27:44no this is sorry we can go up and down
  673. 27:46and if I had a little uh brake button to
  674. 27:48uh hold the um clock that would do that
  675. 27:51but I can count up and I can count down
  676. 27:53and really it's just this little bit
  677. 27:54simple of logic. So all you have to do
  678. 27:56is think this thing is clicking away.
  679. 27:58It's either counting up, it's counting
  680. 27:59down, and when it uh and you can also
  681. 28:02load in values into it to reset it if
  682. 28:04you needed to.
  683. 28:09>> Questions? Yeah.
  684. 28:20>> Oh, these are J flip-flops.
  685. 28:22>> They're not D flip-flops. Uh, so I think
  686. 28:24it's every edge uh makes a transition.
  687. 28:26And it's not just the positive edge if I
  688. 28:28remember correctly.
  689. 28:29But Nan, you are asking me like
  690. 28:3120-year-old material. Like I like I took
  691. 28:34this class when I was in undergrad. I
  692. 28:36forget how J flip-flops work.
  693. 28:40But I think the the big thing is I mean
  694. 28:42you're welcome to look through this
  695. 28:43topology and say like, okay, I've
  696. 28:44mastered it. I'm not asking you to
  697. 28:46master how the clock turns into a a
  698. 28:50count. I'm asking you to realize, hey,
  699. 28:52this thing does count, right? like
  700. 28:54there's a module that can go up, can go
  701. 28:56down, it can be reset, it can be held.
  702. 28:58Uh all those different functionalities.
  703. 29:02Okay.
  704. 29:09Uh okay. So now the the conceptual model
  705. 29:12I want you to have is we have a a clock
  706. 29:14coming in. We I took off the pre-scaler
  707. 29:15for here.
  708. 29:17And then uh you'll often see these
  709. 29:19diagrams like this and you're like
  710. 29:21here's the uh the count value [snorts]
  711. 29:24as a function of time and time is
  712. 29:26progressing at a certain rate. So you
  713. 29:29have a clock with a period and we're
  714. 29:31just going to assume that we're so
  715. 29:32zoomed out that you're not actually
  716. 29:34seeing little stair steps, right? But if
  717. 29:36it was truly a shorter uh if the thing
  718. 29:39wasn't 16 bits you or we were more
  719. 29:42accurate in how we were drawing, you
  720. 29:43would see these little stair steps as
  721. 29:45you progress in time. to go from one to
  722. 29:46two to three to four. But this is just a
  723. 29:49common way of representing
  724. 29:52um the progression of a timer. And then
  725. 29:56uh so we have a 16- bit timer here and
  726. 29:58then he has some pro uh period reset
  727. 30:00values. So in here is the value at which
  728. 30:03it will reset and so the timer is going
  729. 30:05to progress up to this value and then
  730. 30:08when count in here equals the period
  731. 30:12register you're going to have you're
  732. 30:14going to reset. And I wrote here reload
  733. 30:16reset because if you're counting down
  734. 30:17then it takes the value that's in PR and
  735. 30:20puts it into the timer. If you're
  736. 30:21counting up it uh makes uh the counter
  737. 30:24go back to zero. So then you've come to
  738. 30:26some reset period and then you do it all
  739. 30:29again. And so this is where the idea of
  740. 30:31this is a period uh register because
  741. 30:33it's defining the period that this timer
  742. 30:35goes to. It's not saying that the timer
  743. 30:37itself say is 16 bits. It could go
  744. 30:39longer of course. Well, not of course
  745. 30:41but if PR is less than 16 uh the value
  746. 30:44of 16 bits then uh it will go up to that
  747. 30:46value and then uh reset.
  748. 30:52>> Yeah.
  749. 30:56>> Can be anything you want. You guys get
  750. 30:58to pick. So um you are all in 16- bit
  751. 31:02timers and what uh you'll see is that
  752. 31:04you are going to control like the the um
  753. 31:08pre-scaler value and the uh program or
  754. 31:11the period timer to hit some goals.
  755. 31:14That's that's the thing you're trying to
  756. 31:15do. And your goal will be make a
  757. 31:16waveform that does this.
  758. 31:21Um all right. So we can kind of kind of
  759. 31:23see an example how this works. So given
  760. 31:25a input clock of uh 32.76 kHz which is
  761. 31:29this very happens to be a nice crystal
  762. 31:31that's very popular uh what would be the
  763. 31:34reset uh period or or sorry the period
  764. 31:37register to hit 1 second. So you could
  765. 31:40kind of do the formal method where
  766. 31:41you're like aha okay so I oh I didn't
  767. 31:43spell that right. Okay. Um you have uh
  768. 31:46the the goal here of your uh I don't
  769. 31:49know why what I was even writing there
  770. 31:50but the goal here is to uh figure out
  771. 31:53from your frequency what's the period uh
  772. 31:56or of your waveform not the period that
  773. 31:59you're trying to go after for and then
  774. 32:01you go from that times the number of
  775. 32:02counts and that's your time interval. So
  776. 32:04that would be the um
  777. 32:07uh the way to think about this. Uh it
  778. 32:10just happens to be that engineers are
  779. 32:12clever. Engineers maybe a little lazy.
  780. 32:14They didn't want to do the math. So they
  781. 32:16said, "Okay, let's manufacture this
  782. 32:18watch crystal such that um it just
  783. 32:22happens that 2 to the 15 is equal to
  784. 32:25that, right? It makes math easy." And
  785. 32:27then so you could go through all of this
  786. 32:29math and figure out that oh, if I invert
  787. 32:32the period to figure out how how long a
  788. 32:34time slice is and then I count up to
  789. 32:36some value, I can figure out what the
  790. 32:38time interval. That's fine. But this the
  791. 32:40trick here is that that number happens
  792. 32:41to be special.
  793. 32:44And so if you want to with a 16- bit
  794. 32:47timer, all you do is set your um period
  795. 32:50timer. So your PR to this value and it
  796. 32:53will go off every second, right? Because
  797. 32:55that Yeah.
  798. 32:57>> What do you mention go off? Is there
  799. 32:59like an inter
  800. 33:05>> what is the mechanism
  801. 33:07that
  802. 33:08>> when what is the mechanism I think is
  803. 33:09what you're saying? All right. So it is
  804. 33:13hardware inside your timer module that
  805. 33:16when your PR in if you're counting up
  806. 33:19mode when the value stored in the uh
  807. 33:22period register equals the count time it
  808. 33:25does something and that is reset or
  809. 33:27reload
  810. 33:29doesn't is independent of the core.
  811. 33:32Now we will find out later of course you
  812. 33:34can go set an interrupt to go off but
  813. 33:37this functionality of repeating uh is
  814. 33:40independent of the core.
  815. 33:48Okay. And so if we put this value in
  816. 33:51here assuming this is a 16- bit timer. I
  817. 33:54guess you could be longer. Um and then
  818. 33:56you have a clock that is uh 32.768 kHz
  819. 34:00coming in. you find out that like we
  820. 34:02said the time just comes out nicely to
  821. 34:04this one second tick right and this is
  822. 34:06kind of why the engineers chose this
  823. 34:09because now you have a reference that
  824. 34:10all those humans care about I don't care
  825. 34:12how many you know micros seconds it
  826. 34:14takes for things to happen that's not
  827. 34:16important to me um it's nice to have
  828. 34:18this external timer that is going to go
  829. 34:20off every second and now I have a human
  830. 34:23uh readable time and it's so painful to
  831. 34:26go between like the worlds of 144
  832. 34:29megahertz and 1 second that they just
  833. 34:32decided, look, is this easier to have
  834. 34:34two different clock domains? One for
  835. 34:35high frequency things, one for low
  836. 34:37frequency things.
  837. 34:40What questions you have?
  838. 34:44Zero questions. Okay. Uh, as I alluded
  839. 34:48to, you can count up, you can count
  840. 34:49down, and then there's a special case
  841. 34:51where you count up, and then countdown.
  842. 34:53And here it says auto reload register or
  843. 34:56auto reload. You can this is called the
  844. 34:59PR the period register but you hit some
  845. 35:02value and then you count counting down
  846. 35:03and so this is such a common function
  847. 35:05that needs to happen uh there's various
  848. 35:08waveforms it's a lot easier to compute
  849. 35:09if you do this that most manufacturers
  850. 35:11give you this option of you know up down
  851. 35:13or then up down and then you can also
  852. 35:15almost go you can go twice as long. So
  853. 35:18being able to count long things is also
  854. 35:20important.
  855. 35:23All right. So now we're going to talk
  856. 35:24about the things that we add on to a
  857. 35:26timer. So there's two primary things
  858. 35:30that we want to measure like how long
  859. 35:32something takes. We want to capture the
  860. 35:33interval of time and then we want to
  861. 35:35compare the alternative modalities. We
  862. 35:38want to compare to some ex um some known
  863. 35:41value and say okay go off when these two
  864. 35:44things are equal then a task or a thing
  865. 35:47should occur. So measuring interval of
  866. 35:49time and then basically a stopwatch that
  867. 35:51would be a stopwatch and then setting an
  868. 35:53internal timer so that an event goes off
  869. 35:55at a certain time. Those are the two
  870. 35:56main modalities that we're looking at.
  871. 36:00All right. So
  872. 36:02uh
  873. 36:04yes okay that's not what I want to talk
  874. 36:06about. Um
  875. 36:08so here we have our basic timer
  876. 36:10structure and I just added this output
  877. 36:12block uh your uh microcontroller much
  878. 36:16like how the uh interrupt uh the ru eru
  879. 36:21uh device we talked about on Monday
  880. 36:23there's an output layer to the actual uh
  881. 36:26device and that uh to the timer module
  882. 36:29and so your timer is going around doing
  883. 36:31tasks and then this output layer allows
  884. 36:33you to route things around like set an
  885. 36:34interrupt or send a a signal out to a
  886. 36:38GPO pin. Um, we'll we'll talk about that
  887. 36:41later, but uh that's why that block is
  888. 36:43there. And so what we're going to do is
  889. 36:45we're going to add this extra timer. So
  890. 36:48a capture timer and it can be connected
  891. 36:50to external event. Usually it's a GPO,
  892. 36:53but it could be internal. And basically
  893. 36:55what you're saying is when this thing
  894. 36:57goes off, when that thing goes high, I
  895. 36:59want you to do with something. So this
  896. 37:02thing goes high and you're going to
  897. 37:03capture uh should yeah I should say c
  898. 37:06yeah you're going to capture the value
  899. 37:07here and store it in this register. So
  900. 37:10pin goes high take the value of this in
  901. 37:12timer put it into uh the capture
  902. 37:14register and then do something with it.
  903. 37:16Does that modality make sense?
  904. 37:25Make sense so far? Okay.
  905. 37:28So um again here is uh you're trying to
  906. 37:31measure the time span of something one
  907. 37:34rising edge to another rising edge and
  908. 37:35if you remember this great problem the
  909. 37:38super interesting problem that I gave
  910. 37:40you guys didn't you didn't know at the
  911. 37:43time that if I had just given you the
  912. 37:45correct module it would have made your
  913. 37:46life so much easier. So here if you had
  914. 37:49instead of it was magical P2 it was just
  915. 37:51a timer capture unit that thing would
  916. 37:54have been solved effectively easier. I
  917. 37:56mean you still have to program the whole
  918. 37:58thing and do everything but that's the
  919. 37:59uh the idea and so we can walk through
  920. 38:02an example here. Um
  921. 38:06so say we have a 16- bit timer and our
  922. 38:08goal is to measure the intervals between
  923. 38:11uh the wind speed and we're going to set
  924. 38:13some arbitrary goals here like I want to
  925. 38:15go a th00and RPMs or uh 100 or 10 RPMs
  926. 38:19which these are just arbitrary values
  927. 38:21that don't have to make sense. And we're
  928. 38:23given a clock like this. So the the goal
  929. 38:25here would be, you know, you'd be asked
  930. 38:27to determine uh the pre-scaler value
  931. 38:30that would allow you to hit those time
  932. 38:31functions. So if you're t if you're um
  933. 38:36and so here we can look at the like
  934. 38:37what's the maximum speed that you would
  935. 38:39be going. So the maximum speed would be
  936. 38:41you're trying to go at a,000 RPMs
  937. 38:43divided by 60 and then you can figure
  938. 38:45out the the minimum amount of time you
  939. 38:47have to go. So you if you went if your
  940. 38:49clock speed was longer than that then
  941. 38:51you wouldn't be able to hit that
  942. 38:52measurement, right? So maybe I slowed my
  943. 38:54clock real slow. I can't if I go too
  944. 38:57slow, I can't actually measure small
  945. 39:00units of time.
  946. 39:02Probably the counter example which is uh
  947. 39:04a little bit easier to start with is the
  948. 39:06minimum speed that you want to go is um
  949. 39:10oh no sorry this is the this is 6
  950. 39:12seconds. No no
  951. 39:18did I write this backwards? Oh 100 RPMs.
  952. 39:22No. Okay, that's right. So slow speed,
  953. 39:23so you're going 10 uh 10 RPMs. And so
  954. 39:27you need to be able to count out in time
  955. 39:29up to 6 seconds. So the question is,
  956. 39:31what pre-scaler
  957. 39:33uh period of time will allow me to
  958. 39:36measure something as long as 6 seconds?
  959. 39:38So if I go at 140 uh megahertz, I'll get
  960. 39:43before I get to 6 seconds, I will
  961. 39:45overflow. And that's not a that will not
  962. 39:47allow me to keep track of time. So now
  963. 39:49you need to choose a pre-scaler that
  964. 39:51will allow you to at least uh before you
  965. 39:53overflow that you will uh have a valid
  966. 39:56value. So you can go through the math
  967. 39:58and figure out okay uh 2 to the 16th
  968. 40:01here it is. You plug it back in the
  969. 40:02equation and you say like my minimum
  970. 40:04frequency timer uh has to be uh 10.96
  971. 40:08kHz. So now you have a goal of where
  972. 40:10you're supposed to be hitting uh your
  973. 40:12pre-scaler to slow your thing down.
  974. 40:17uh and you can go through uh to try and
  975. 40:20find the closest pre-scaler, right?
  976. 40:21Because you don't get infinite
  977. 40:22solutions. There's only certain uh power
  978. 40:25divide by twos that you can have. And so
  979. 40:28you you go through the math. I think the
  980. 40:29one the closest one is actually nine,
  981. 40:31but then it wouldn't give you a valid
  982. 40:32answer. So then you have to go through
  983. 40:34uh because it just wouldn't be
  984. 40:35achievable. And so then you go through
  985. 40:3710 and you can see okay here is the
  986. 40:40closest pre-scaler value that would
  987. 40:42allow me to measure long enough to see a
  988. 40:466-second long thing. And now from that
  989. 40:48you can go through and say uh so here's
  990. 40:50my timer in my pre-scaler value and
  991. 40:54here's the frequency I want to have out
  992. 40:55and go through this math and say the the
  993. 40:57longest tick the ideal tick uh I need to
  994. 41:01have is 102 microsconds. So that's how
  995. 41:05long that period of my clock needs to be
  996. 41:08so that I can count up to 6 seconds. And
  997. 41:11then here you go through the math to
  998. 41:12figure out that um so you have your
  999. 41:16seconds, you have your your your clock
  1000. 41:18period tick and then you can figure out
  1001. 41:20how many counts it will be. So this
  1002. 41:22would be your p this would be your
  1003. 41:23capture compare value uh if you went to
  1004. 41:27six seconds. So if it takes uh the clock
  1005. 41:29the mometer 6 seconds to go around that
  1006. 41:32would be the value and you know if you
  1007. 41:34did incorrectly if you chose the wrong
  1008. 41:36value then if that's larger than what
  1009. 41:38can fit in a 16- bit uh register then
  1010. 41:41you know that it would have overflow and
  1011. 41:43you haven't hit your mark.
  1012. 41:47Yeah, you're all looking at me like I'm
  1013. 41:48crazy. That's fine. I get that.
  1014. 41:52But what questions do you have?
  1015. 42:01Okay. So, you guys can work through the
  1016. 42:04math. I think the real uh trick here is
  1017. 42:07that you have to think about you have to
  1018. 42:09slow down the clock such that as you
  1019. 42:13count up on your counter, you can hit
  1020. 42:16your timing requirements. And if you go
  1021. 42:19too if you don't pre-scale enough, if it
  1022. 42:22if it's too short a time, you'll
  1023. 42:23overflow. be hit before you hit your
  1024. 42:25requirements. The downside would be you
  1025. 42:28could go really really really long,
  1026. 42:30right? You could just say I will always
  1027. 42:31go for the longest pre-scaler, therefore
  1028. 42:33I'll be able to hit my time. But then
  1029. 42:35you might not have enough resolution if
  1030. 42:37needed to measure small periods of time.
  1031. 42:41And while we didn't put a resolution
  1032. 42:42requirement here, we did say that we
  1033. 42:44would need to measure some fast uh
  1034. 42:47interval uh the the thing when it's
  1035. 42:49going the fastest. And here we can say
  1036. 42:51at least I didn't specify an interval
  1037. 42:54but here this would be the value when
  1038. 42:56it's going uh 10,00 RPMs that the
  1039. 42:59counter would the capture counter would
  1040. 43:02record from one tick to another uh from
  1041. 43:04one revolution to another revolution and
  1042. 43:06at least you can say all right that's a
  1043. 43:08good enough value there's enough
  1044. 43:09resolution here but I think the probably
  1045. 43:12the better trade-off would have been to
  1046. 43:13give you a resolution um uh spec where
  1047. 43:17it was you have to be able to measure so
  1048. 43:19many re
  1049. 43:21like 0.1 revolutions per second or
  1050. 43:23something like that.
  1051. 43:26Okay. And then the key idea here is that
  1052. 43:28there's this time trade-off between the
  1053. 43:31resolution at which you set up your uh
  1054. 43:34sorry the the longest duration you can
  1055. 43:36measure versus the resolution. You can't
  1056. 43:38have both. The only way to get better is
  1057. 43:41to increase the the size of your
  1058. 43:43counter. So going to from 16 to 32 bits
  1059. 43:46will certainly give you more tick marks
  1060. 43:48to count and therefore that will
  1061. 43:49increase your resolution but uh on your
  1062. 43:52hardware it's already kind of baked in.
  1063. 43:53You don't have any um
  1064. 43:57uh you don't have any ability to change
  1065. 43:59that. So here you're just saying that
  1066. 44:01you have to make this trade-off between
  1067. 44:03resolution and time.
  1068. 44:11Nobody. You're like either you're like,
  1069. 44:13"Yeah, I got this." Or you're like, "I
  1070. 44:14have no idea what this guy went and
  1071. 44:16talked about."
  1072. 44:21Cool.
  1073. 44:23All right. Well, we're going to go
  1074. 44:24through another example. We'll have a
  1075. 44:25little worksheet. You can play with the
  1076. 44:27math. See see what makes sense. All
  1077. 44:30right. So, the other type of count uh uh
  1078. 44:34event is the compare event. And this is
  1079. 44:36really good not for measuring the
  1080. 44:38passage of time per se, but controlling
  1081. 44:40motors and those sorts of things. So
  1082. 44:42here um
  1083. 44:45uh yeah, so it's really about
  1084. 44:47controlling motors and having pulse with
  1085. 44:49modulation and being able to control
  1086. 44:51units of time very precisely. That's
  1087. 44:53generally the goal. Um so here we go
  1088. 44:56back to our basic structure. We have a
  1089. 44:58clock coming in, a pre-scaler. We have a
  1090. 45:00timer that's either going to count up or
  1091. 45:01countdown. Uh we have some periods so
  1092. 45:03that we know it's going to reset. And
  1093. 45:05then we have some event controller on
  1094. 45:07the output. And then we're going to add
  1095. 45:08this uh compare timer. And so what
  1096. 45:12happens is when these two things are
  1097. 45:13equal, then some event occurs. Um so
  1098. 45:16there's no prompting. There's no trigger
  1099. 45:18from the external world. We're just
  1100. 45:20going to say this thing starts at zero
  1101. 45:21and then I'm going to compare to some
  1102. 45:23value. Uh and then you're going to do an
  1103. 45:25output. Now you could argue, wait a
  1104. 45:27second, why not just uh control your pir
  1105. 45:30your um period register? that's
  1106. 45:34effectively the same. But this allows
  1107. 45:36you to, as we'll see later, is you can
  1108. 45:38vary your compare register while keeping
  1109. 45:41the period the same. So it's like you
  1110. 45:43could vary your period the whole time,
  1111. 45:45but that's only one degree of
  1112. 45:46flexibility. We're going to see later
  1113. 45:48that you're going to want to do things
  1114. 45:49like um control when a pulse goes up,
  1115. 45:53not just when uh a pulse changes. So you
  1116. 45:55get to have more flexibility in time
  1117. 45:58control with this extra register.
  1118. 46:02And so the classic example here is pulse
  1119. 46:04switch modulation. So we'll just show
  1120. 46:05you how it's done for an LED.
  1121. 46:15All right. So in this simulation, does
  1122. 46:18duty cycle work? Duty cycle. Yeah, duty
  1123. 46:20cycle works. Cool. Sometimes it doesn't.
  1124. 46:24Uh so this is just an LED is supposed to
  1125. 46:25be blinking. And here the idea of duty
  1126. 46:27cycle
  1127. 46:29is just this width here. I can't really
  1128. 46:32see but just this width here. Usually
  1129. 46:35duty cycle is determined by the positive
  1130. 46:38period of time. It's not you could do
  1131. 46:41the negative period of time if you
  1132. 46:42wanted but you should let's just assume
  1133. 46:44it's always the positive period of time
  1134. 46:45unless somebody uh explains why or tells
  1135. 46:48you what you're doing. Um, but it's
  1136. 46:50usually this positive period of time and
  1137. 46:52you can go and control the duty cycle.
  1138. 46:54And that's really what that compare
  1139. 46:55register is doing. It's allowing you to
  1140. 46:57control this pulse width here. And why
  1141. 47:00that's particularly important is when
  1142. 47:02you're doing things like LED dimming.
  1143. 47:04So, can you see here? Maybe this
  1144. 47:06projector is good enough. Right? If
  1145. 47:09effectively that looks dimmer to you,
  1146. 47:11right? And if I go with my duty cycle up
  1147. 47:14here now, that would appear brighter.
  1148. 47:17And that's different than trying to do
  1149. 47:19just off and on. So here I'm using time
  1150. 47:21to control the dimming of LEDs. You guys
  1151. 47:25see this in 270?
  1152. 47:28No. Okay. 215. No, you wouldn't do it in
  1153. 47:30215. So this is like how every one of
  1154. 47:33your electronic devices dims. It's too
  1155. 47:35painful to control voltage, right? You
  1156. 47:38got a little microcontroller. Again,
  1157. 47:39it's only good at time. it doesn't it's
  1158. 47:42not really good at like sending out a
  1159. 47:43particular voltage or even a particular
  1160. 47:45current to to dim an LED. So what
  1161. 47:48they're doing is they're just uh have
  1162. 47:50they set up some period and they are
  1163. 47:53going to change the duty cycle. This is
  1164. 47:54where you get uh slowly blinking or or
  1165. 47:57heartbeat things. And the trick is that
  1166. 48:00just like this they go faster than you
  1167. 48:02can see. So as long as you're going like
  1168. 48:04a kilohertz humans can't tell that it's
  1169. 48:06blinking for you. It just looks like
  1170. 48:08it's dim dimmer or brighter.
  1171. 48:17Any questions?
  1172. 48:25Yes.
  1173. 48:30>> Why we need the compare? So, um, as
  1174. 48:34we'll see in a second, the compare
  1175. 48:36register allows us to do things like,
  1176. 48:38uh, so we have a period. You can see the
  1177. 48:41period there, right? So, I can stop
  1178. 48:43here. And so, we have a period from like
  1179. 48:46this node to this node, right? But how
  1180. 48:49long should this pin be high?
  1181. 48:53Right? It's got to be high for some
  1182. 48:55period of time. And I suppose you could
  1183. 48:57like dynamically change the period
  1184. 49:00register so that it's like off for this
  1185. 49:02period of time and then you quickly go
  1186. 49:04and write in uh how long it should be
  1187. 49:06high and back and forth. That's going to
  1188. 49:07consume a lot of energy and effort on
  1189. 49:10the core. So instead you can set up by
  1190. 49:13setting up the period between here and
  1191. 49:15here and then the compare register
  1192. 49:18literally has the number of clock ticks
  1193. 49:21in it from here to here is the value
  1194. 49:24that's stored in that register and then
  1195. 49:26it's going to go high and low and so
  1196. 49:27we'll talk about that mechanism a little
  1197. 49:28bit but that compare register is
  1198. 49:31changing is effectively the same as duty
  1199. 49:32cycle in the simulation.
  1200. 49:38So here I mean duty cycle is usually
  1201. 49:400ero to one but there's a mapping here
  1202. 49:42between duty cycle and the count value
  1203. 49:44the digital value that's stored in the
  1204. 49:46compare register. So here the compare
  1205. 49:49register would have a high value and
  1206. 49:51when the duty cycle goes down in this
  1207. 49:52example it would have a low value and
  1208. 49:54you can get to zero. Yeah you can get to
  1209. 49:56zero in this example you could get to
  1210. 49:58zero in your devices as well.
  1211. 50:04What other questions you have?
  1212. 50:11Okay.
  1213. 50:15And this is almost exactly how motors
  1214. 50:18almost all motors are controlled as
  1215. 50:19well.
  1216. 50:22I guess I should have done this slide
  1217. 50:24first. Maybe that would have been
  1218. 50:25helpful. So here um we have uh a period
  1219. 50:29that's been set up and then the uh
  1220. 50:32reference signal here we'll call uh
  1221. 50:34which is your compare register. So this
  1222. 50:36reference signal basically when that and
  1223. 50:39the count equal each other then some
  1224. 50:41event occurs and you get to control
  1225. 50:42whether that is the pulse goes high the
  1226. 50:44pulse goes low. There's different
  1227. 50:47configurations but let's just say it's
  1228. 50:48the pulse goes high. So when reference
  1229. 50:50signal equals count, then you go to T
  1230. 50:52on. And then while it's above, you're
  1231. 50:54on. And then when you go below, when it
  1232. 50:56overflows at your period, it goes back
  1233. 50:58down. And so now you're doing this
  1234. 51:00waveform up and down. And um yeah, and
  1235. 51:04uh this can be as one of those alternate
  1236. 51:06functions for your GPO pins. You could
  1237. 51:09send this signal out a GPO pin. Here
  1238. 51:11we're just in still in the timer. And
  1239. 51:14then there's an output block on your
  1240. 51:16device that controls how it's being
  1241. 51:17routed. But for now, it's just uh a
  1242. 51:20signal that's going high or low. And you
  1243. 51:23could do this. This is just a digital
  1244. 51:24comparator that's just going high uh
  1245. 51:26high or low. Okay.
  1246. 51:29So, we'll walk through maybe a little
  1247. 51:31bit more detailed example. Um because
  1248. 51:33there's some small little caveats here.
  1249. 51:35So, here we have a timer or a clock spin
  1250. 51:37foot spin in here. Here we're stepping
  1251. 51:39up. We zoomed way in in time. So, that's
  1252. 51:42why we can actually see the stair steps
  1253. 51:43now. And before we couldn't. Um, and so
  1254. 51:47here this is the the period register.
  1255. 51:52Um, and we're going to we're counting up
  1256. 51:54counting counting up. And so here the
  1257. 51:56trick is that it's uh P R + one times
  1258. 51:59the clock period. So what you set this
  1259. 52:01to a certain frequency 1 over F, you get
  1260. 52:04your period. And then so that's the tick
  1261. 52:06period. So we'll have to have some good
  1262. 52:08nomenclature between what is the clock
  1263. 52:10period versus what is the period of the
  1264. 52:12timer. Um, and then that's just because
  1265. 52:15the counting always starts at zero. So
  1266. 52:17you're including zero essentially that
  1267. 52:19transition step uh in your count up. So
  1268. 52:21you're going to count up in this case.
  1269. 52:23Again, you can do the opposite. You can
  1270. 52:25count down. You start at six or some
  1271. 52:27value and then you're going to include
  1272. 52:29zero. So it's the the period register
  1273. 52:31plus one times the clock period equals
  1274. 52:34the period of the uh waveform. Um, okay.
  1275. 52:40And same for well time clock up clock
  1276. 52:44down uh this mode. The only caveat that
  1277. 52:46gets me and every other student is that
  1278. 52:49um you don't count uh the end uh
  1279. 52:54conditions here. You don't count six or
  1280. 52:56zero or six in this example. You're
  1281. 52:57going to count 0 1 2 3 4 up to six and
  1282. 52:59then back down. So you're not double
  1283. 53:01counting six. You're not double counting
  1284. 53:02zero. And that's why the period for up
  1285. 53:05down counting is actually the same as
  1286. 53:07your um it should say uh pr instead of a
  1287. 53:11a r but that is um the trick the
  1288. 53:15conceptual trick that gets people each
  1289. 53:17time for uh center line mode.
  1290. 53:22Okay.
  1291. 53:24Um and then to how much time? Okay, we
  1292. 53:26got time. Uh so then to bring it back to
  1293. 53:28the CCR or the the compare register.
  1294. 53:32So here we're going to go through we're
  1295. 53:34going to set the the PR to six and then
  1296. 53:36we have a compare and so here at some
  1297. 53:39time right when this thing equals from
  1298. 53:42uh equals three then it's going to have
  1299. 53:44a transition it's going to overflow. So
  1300. 53:46this is um low if true and then you can
  1301. 53:49go through the math to kind of uh figure
  1302. 53:51out what the clock period or what the
  1303. 53:54period is and what the duty cycle is.
  1304. 53:58I don't know why that happened. And then
  1305. 54:00uh you can go high if true. This is kind
  1306. 54:02of like normal case of how I think about
  1307. 54:03things. There are different modes and
  1308. 54:05again this is just your output mode to
  1309. 54:08do that. Uh same same math. It's just
  1310. 54:11you have to think uh the only math
  1311. 54:12that's different is your duty cycle
  1312. 54:13because it diff typically is saying that
  1313. 54:16duty cycle is your high time. So you
  1314. 54:18just have to remember if you're high
  1315. 54:20true or low true uh how that affects
  1316. 54:22your duty cycle. And then the the trick
  1317. 54:25here is to make things brighter. you
  1318. 54:26just change your CCR or your CR and now
  1319. 54:30you've shortened your pulse width. Um,
  1320. 54:33and so now in this case the LED would be
  1321. 54:35dimmer
  1322. 54:38and here you're just on for two cycles
  1323. 54:40in this case. So that's why your duty
  1324. 54:41cycle is lower
  1325. 54:45and same for CCR mode. Okay, any
  1326. 54:48questions before we do a little
  1327. 54:49worksheet problem?
  1328. 54:54Here, I'll put that up.
  1329. 55:00You guys are very quiet. I think that
  1330. 55:02exam scared the heck out of you. You
  1331. 55:04shouldn't be too worried. I promise you.
  1332. 55:07Just pass those down that way.
  1333. 55:13All right.
  1334. 55:15Thank you.
  1335. 55:18That happens every time. You You scare
  1336. 55:20me. That's what's happening here. Take
  1337. 55:22that one. Thank you.
  1338. 55:30Okay, you pass that way or I'll take
  1339. 55:32this one over to her.
  1340. 55:35>> I gave you the the nicely crinkled one.
  1341. 55:37Sorry about that.
  1342. 56:00>> [clears throat]
  1343. 56:12>> All right. So in this problem
  1344. 56:15uh you've been given the task of
  1345. 56:17creating some duty cycle here
  1346. 56:20and you have to figure out where your
  1347. 56:22pre-scaler is going to be, your uh
  1348. 56:26period register and your uh capture or
  1349. 56:28your compare register. Now the trick is
  1350. 56:31there are multiple solutions.
  1351. 56:34It's not like there's only one possible
  1352. 56:36solution because it's a trade-off
  1353. 56:37between resolution and time. So if you
  1354. 56:41uh go for a very long potential period
  1355. 56:45uh or sorry if you configure your timer
  1356. 56:47such that it could go for a very long
  1357. 56:49time you would give up resolution even
  1358. 56:52uh or if you went for just the minimum
  1359. 56:54period of time then you would have high
  1360. 56:55resolution. We don't really care about
  1361. 56:57that. We're you said is you have to get
  1362. 56:59a 10% duty cycle uh with 10
  1363. 57:02milliseconds. So it's giving you the
  1364. 57:03spec and you have uh where does it say?
  1365. 57:08a 16 megahertz timer. So you have a
  1366. 57:10clock that you have to slow down such
  1367. 57:13that you can hit a period. It could be
  1368. 57:15exactly the period to hit your period
  1369. 57:18and then you have to go uh it doesn't
  1370. 57:20have sorry it doesn't have to be the
  1371. 57:22length of a 16- bit timer. It could be
  1372. 57:24some period value. Just has to fit in a
  1373. 57:2716- bit timer. And so once you figured
  1374. 57:29out your period value then you had to
  1375. 57:30figure out the math to come up with a 10
  1376. 57:33millisecond uh chunk of time. Okay.
  1377. 57:38And so as you work through this problem,
  1378. 57:40the trick is really you're trying to
  1379. 57:41make this. So I would say start figuring
  1380. 57:45out what pre-scaler would give you
  1381. 57:48pre-scaler and period register value
  1382. 57:50could give you 10 milliseconds. Okay?
  1383. 57:54And then work over to figure out what um
  1384. 57:57CCR value will give you 1 millisecond.
  1385. 58:14And there. There we go.
  1386. 58:52You got it.
  1387. 58:56Uh please.
  1388. 59:43Wait.
  1389. 1:00:26I'm wondering
  1390. 1:00:41[snorts]
  1391. 1:01:16What are you doing?
  1392. 1:01:27Good job.
  1393. 1:01:53Yeah.
  1394. 1:02:19Awesome.
  1395. 1:02:42That would be easy.
  1396. 1:03:29Oh yeah.
  1397. 1:03:35We have
  1398. 1:03:40Amen.
  1399. 1:03:54God.
  1400. 1:04:06[clears throat]
  1401. 1:04:20Yes.
  1402. 1:04:47What's going to be
  1403. 1:04:51Okay.
  1404. 1:04:54[clears throat]
  1405. 1:05:01Yeah.
  1406. 1:05:31Heat. Heat.
  1407. 1:05:47Sorry.
  1408. 1:06:37What's the answer? It's good about
  1409. 1:06:52>> All right, I know we're running out of
  1410. 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.