So, I’ve been making a clock. Up to this point I’ve managed to assemble and code a timer that starts at power-on and has no interactivity with the human world outside of killing or enabling the power. It also runs entirely off the internal timer of the MSP430 series microcontroller. This timer works, but is not very good if you want to count real time. The way the signal is generated changes frequency with temperature. While Texas Instruments has people smart enough to develop a means to compensate for this effect, it still results in an inconstant operating frequency, raising and lowering as it shifts through these compensation methods. While this is fine for making sure the code and wiring works, this is no good for an actual clock.
Luckily, that’s a solved problem. Your everyday watch crystal is a piece of quartz which vibrates at a fixed frequency when electricity is applied. This frequency is 32,768 times per second. That does not immediately sound like a useful number for the average person, but to people versed in binary, that is a very useful number. It is a power of 2, 2^15 to be precise. That means it is very easy to translate that frequency into the binary realm where all of our code lives. Watch crystals have another useful property – they are cheap. I got a heap of them for less than the cost of a bottle of soda. That says more about the price of groceries these days, however.
The entire MSP430 series is built to play nice with an external crystal, and each of them have a pair of dedicated pins to connect the crystal to so that it can be used to drive one of the chip’s internal timers. My biggest hurdle on the hardware side was getting these teeny-tiny pins on this teeny-tiny can to make a reliable electrical connection inside the breadboard. I ended up making it share the hole with the end of a jumper wire which wasn’t connected to anything. While the back of my mind I knew this had the ability to cause interference with the precise signal coming off the crystal, I also knew that the final design wouldn’t have this antenna wire hanging off one of the leads. So long as the prototype got a functioning signal, I could move forward with development.
The rest of dealing with the crystal was code, and I’m leaving that for the deep dive into the program. What was my remaining problem? Oh yeah, you still can’t set the time.
Well, we need some buttons and switches. The F2003 has fourteen pins, four of these are not I/O pins, and of the ten left, we’ve used five. Two for the clock, and three to drive the output display. Five pins is ample for what we need. The real constraint is that we don’t have much room for more code on the F2003. We have got to keep it simple. What is the bare minimum functionality? Setting hours and minutes. Most people have set a clock where there’s no reverse option and you just have to keep going up until the value loops around if you want to go down. It’s annoying, but a familiar enough circumstance that we wouldn’t be doing anything strange using it.
So we only need two buttons, right?
Technically speaking, we could get away with that, but momentary switches are easy to hit by accident. So I want to add a switch that enables or disables the buttons. If I were smart I’ve have put this switch on the power line for the buttons and eliminated any need to code for it. This is the benefit of hindsight. You see, I spend so much time on the computer that code solutions come to mind first, and this idea literally only struck me as I’m writing this article. So, since I was focused on code, I have three pins for input – one for the switch, one for setting the hour, and one for setting the minute. This is my finalized hardware design. I ran through a couple of code iterations because I had different ideas about what needed versus what the compiler thought was needed. Most of these iterations were frustrating attempts to get the program to register the button presses.
So what was the problem? The compiler figured because I never set the pin for the switch to trigger interrupts that I wasn’t going to have activity on that pin. Once I got the button presses to actually do something, I found that the buttons were not very responsive. Another code tweak. Now they were too responsive, getting multiple events per button press. More code tweaks. Now it was properly responsive, but the display flickered when you pushed a button. Then I went back to the drawing board and completely redesigned my button code, breaking it entirely. Then it wouldn’t fit on the chip. Then it did, but the output was wonky. So many iterations of what was the simplest element from the novice’s perspective before I settled on a design. It was quite literally the example the internet puts forth as “well, this works, but you shouldn’t do it this way because it wastes resources.” And that’s true, it wastes CPU cycles. The thing is, of all the resources I’ve got, CPU cycles are the cheapest and most abundant.
But it worked, and it fit on the chip.
So I decided to finalize this design.
In the shift register article, I drew out a wiring diagram in Visio because I was accustomed to Visio from my day job. It also sucks for this purposes. So I decided to try out some other software, and drew out a design schematic using an open-source program called KiCAD. Schematics are not wiring diagrams, but logical diagrams, but you can readily translate to the hardware. So, here’s the final hardware design.
While KiCAD had built-in symbols for my common Texas Instruments chips, my pet peeve is that the schematic diagram does not resemble the physical pinout. My brain wants to associate to the physical chip I’m looking at. Here’s the pinouts from the datasheets for the three chips I used:
These pinout diagrams from the datasheets fit my mind’s eye better. But the schematic standard is there for people to follow the logic, not to help in soldering the parts together.
I had wanted to end this part of the series with a picture of the soldered wiring on a protoboard, but I’m still a novice at soldering, so bodging together the circuits with jumper wire is a slow process for me. In the name of getting content onto the site, I’m foregoing that final picture.
There are still two more articles in the series. These two might have lower engagement because in them I dissect the code I wrote for the microcontroller and explain what each and every bit does. Still, you can’t talk so much about code changes and never show the code.
Next up adding a calendar and stopwatch!
That’d probably involve an RTC chip, and I2C or the like – so I say go for it!
Although looking at the pinout diagrams, it doesn’t look like the MCU has dedicated lines for I2C, so…bit-banging it?
It’ll be done when it can read mail
I thought it wasn’t really done until you could run Doom on it.
It will be done when it passes butter
Using a watch crystal is a great move. Looking forward to project pictures soon.
I forget, is this powered by a potato?
That would be fun. Power it with a series of absurd battery solutions. Throw in a potato battery, a lemon battery, the fun never ends. As long as you hit the right voltage for that clock it could probably work, it’s very low current draw. I am teaching my daughter Watt’s Law right now with batteries vs. a solar cell for powering a basic fan and that would be quite relevant.
How are you holding up?
How many potatoes does it take to replace a single coal fired plant? Who cares, it’s green.
ALL of them.
We have some potato and spud based commenters who will have to get in on the action to make a go of it.
” This frequency is 32,768 times per second”
Is that something that something set by how they create the clock crystal or is that some wild, Intelligent Design-esque coincidence?
(I could look it up but figured others might be wondering that too)
OK, so I did look it up – apparently it’s a function of the shape and size of the crystal. So based on physical properties, but not intrinsic ones.
This. Varying the power changes the amplitude of the crystal rather than the frequency, making “quartz movements” a good way to keep accurate time.
More in my series of college flashbacks… I seem to remember us using 555 timers a lot.
Charles Platt’s Make: Electronics books use the 555 a lot. Some interesting use cases.
Thanks for the schematic – it really does make it easier to follow along.
I may have to give KiCAD a look, now that my bootleg copy of CirCAD is no longer running…
For those of you not on Twitter, I’m seeing something interesting from Trump. During this porn start trial in NY, he’s inviting various GOP people to spend that day in the courtroom. I’ve seen the AG of Alabama, I think DeSantis(?), now Vivek. Each of them then has a press conference to talk about how absurd the trial is. This is genius.
Local news posted about J. D. Vance as well.
Don’t worry, they have a letter to the editor to let you know how to feel about Vance.
They don’t like my ad blocker.
Mercifully it’s pretty short, just a paragraph about his Jan 6 denial, and a second one about him losing self-respect by going to the trial.
Oh, I don’t know…why is Alvin Bragg going after Trump rather than prosecuting violent criminals?
Why did the entire Democrat party spend years of time and millions of dollars going after Trump instead of doing something useful?
Huh. Oddly politically astute of him.
During this porn start trial in NY, he’s inviting various GOP people to spend that day in the courtroom.
There is a flurry of stories in the legitimate media about sycophant Trump-worshippers making pilgrimage to the courtroom, groveling at his feet and then spouting all the hateful things his gag order prevents him from saying.
Assumes facts not in evidence.
The funny thing is, those sycophants are probably doing better reporting than the “legitimate media”.
Some comedy gold here! To many gems to pick from.
Where’s the best place to raise a small child: New York City, or literally anywhere else?
https://www.theguardian.com/lifeandstyle/article/2024/may/14/best-city-raise-child
I especially enjoy this because of all the places I go regularly Manhattan is the least safe.
New Yorker moves to more desirable place, immediately tries to turn the more desirable place into New York. Take your kid and raise him in Brooklyn lady, just make sure he’s not named Steve Buschemi. Safe my ass…
May the city folk live in the city. May the country folk live in the country.
Let’s build walls around the city to ensure the safety and sanctity of those city folk who fear us wretched country folk.
Well, how likely are you to encounter a random bear in NYC? On the other hand, you’re probably very likely to encounter an unknown man there.
She sounds like an asshole.
Hey now… some of us flyover people have a lot of local pride shirts too.
Many New Yorkers have a superiority complex.
The Guardian…that’s like the Weekly World News, isn’t it? Or those advice columns? Trolling, they just make shit up for clicks.
I feel safer in Manhattan and Brooklyn than I do almost anywhere in the world.
Gun free zones.
Thank God for that, now be sure to keep your back against the wall while waiting for the subway.
Sadly not blade free however…
https://nypost.com/2024/05/13/us-news/times-square-tourist-mom-stabbed-by-maniac-was-chaperoning-school-girls/
In a just world the stabber would have been gunned down like a dog by that lady defending herself. We’ve got to figure out some kind of solution for chronically batshit, violent people.
We used to lock them up.
Before that, they were driven undergound.
Not the most libertarian solution but locking them in a nuthouse sounds like a fine idea to me.
There are people with broken minds that are a constant threat to others.
There is no good solution.
Letting them wander the street to attack people who are, for all practical purposes, prohibited from defending themselves is the worst solution.
“…for all practical purposes, prohibited from defending themselves.”
See, this is the part where I start feeling my face getting hot.
Manhattan and trendy Brooklyn feel extremely safe to me too, but these are all the rich areas of New York. I feel safe in Beverly Hills too.
I don’t feel unsafe in Manhattan, but there have certainly been times where I haven’t felt safe.
I probably deal with mentally ill people at least once a week on the street and you have no idea what to expect.
This morning I was told by one that Helsinki doesn’t exist.
Have you ever been there? Are you sure it does exist?
Exactly!
I can’t confirm it either.
https://www.msn.com/en-us/money/companies/california-restaurants-are-closing-over-20-wage-increase/ss-BB1mkO7o
Proving again that the real minimum wage is $0.
Which is why we need UBI!
Isn’t there a saying about foreseeable consequences? How does it go again?
For a dying organization that everyone hates, people sure do want to be let into it:
https://www.msn.com/en-us/news/us/boy-scouts-more-inclusionary-rebranding-leaves-one-group-feeling-slighted/ar-BB1m7gTr
I have fond memories of my time as a Boy Scout. We learned shooting with .22 rifles, shotguns (doing skeet, I was… not good), and (rarely) black powder musket.
The BSA is not an athletic organization. It’s not an outdoor adventure organization. It’s not a glorified babysitting service.
It’s a character building organization. And that process includes nurturing reverence in a higher authority.
If you don’t want your kids to go through that ordeal, take them somewhere else.
Huh. I was a scout. Nobody asked me about my religion. I was raised catholic but was a non-believer since before the scouting thing. They didn’t ask, I didn’t tell.