Autodidact Ambitions 3 – C no Evil

by | May 7, 2024 | LifeSkills, Pastimes, Technology | 82 comments

In the first two installments of trying to build a clock, I’d acquired a Texas Instruments Launchpad, written some code, wired it up to chips and an LED display. I’d expected garbled output, but not nothing.

I broke out an LED, a resistor, and some jumper wires and started probing. It didn’t really tell me anything, so I dove into the datasheets and found my first mistake in wiring. You see, despite both being shift registers from Texas Instruments operating the same logic core, the SN74 and TPIC had very different pinouts. Among the things that were different was the order between the SR-Clock and the R-Clock pins. They were reversed between the two chips. So the signals going to the TPIC were garbage. I fixed that and tried again.

Still nothing. Frustrated, I triple checked the wiring, and verified that the display could indeed light up when I just fed it power. So, the problem had to be software. I could try to spin a web of excitement about debugging code and draw some arachnid analogies – but really it consisted of me staring at a screen going “now where did I screw up?” It came down to how I triggered the shift register clock pins. In the original derblinkenlights code I was using an expression of “P1OUT ^= SER_OUT;” (as well as “P1OUT ^= R_CLK;” and “P1OUT ^= SR_CLK;”). Now, I knew that this does not work for my purposes because ^= toggles the bits in the variable on the left based upon the expression to the right. To simplify things, SER_OUT was bit 0. P1OUT was a byte at a fixed spot in memory that controlled the state of eight pins when they are set to output. So “P1OUT ^= SER_OUT;” would toggle pin 2 between high and low based upon the previous state. I needed to know exactly what these pins were set to, so I ditched the toggle and subbed in a different bitwise operator.

If you recall (or go check) the shift register article, the clock signals only trigger an action when changing from a low to a high. No other states do anything. Well, the bitwise operator I started to use was |=. That’s an ‘OR’. Cue facepalming from savvy members of the audience. You see, if either the bit in that position is a 1 or the value on the right is a 1, the OR operator sets the bit to a 1. |= is incapable of switching a bit from a 1 to a 0. So my code was literally going “Yer all good, mate” whenever I wanted to switch back to a 0 regardless of what was on the right. By sprinkling in some &= operators (that’s an ‘AND’), I was able to get it setting some zeroes.

Nonsense began dancing across the display. Progress! But it was still nonsense that didn’t resemble the bitmap I’d created for the output. It was back to staring at code going “now where did I screw up?” This was equally embarrassing when I realized that the code segment that fed the contents of the digit hadn’t been updated to include the &= when I’d fixed the rest, so none of the digits were getting written. I was seeing control data. I fixed that, and all of the digits were now showing the same thing, except one which was dark. I had gotten mixed up between whether the TPIC worked active low or active high for sinking to ground. I fixed that. Now, only one digit lit up, and it said 0.

Another embarrassing code mistake – I’d left the part that told the shift registers to move their data to output outside the loop that filled them with data for each digit, so it only updated once a cycle. Moving a curly bracket got all four digits lit up with data. Slightly garbled data, but consistent with a timer… except it was in the wrong place. The digits were reversed. My second major wiring mistake – I’d connected the common cathode pins to the TPIC in the opposite order of what they were supposed to be. After swapping those jumper wires around, the display worked. I adjusted it so that the display showed hours and minutes, and the colon blinked in a two second cycle, and it was good.

So, I installed a ZIF (Zero Insertion Force) socket on the breadboard, pulled my MSP430F2003 out of the launchpad, and wired it up without the debugger for the final triumphant… wait, now nothing is lighting up. What?

My first suspicion was either that I miswired it or the ZIF socket was bad. So I plugged the chip directly into the breadboard. I’d wanted to avoid this because it’s annoying to pull it back out again. This didn’t change anything. Nothing lit up. Putting the chip back in the Launchpad I wired it into the ZIF socket via jumper cables, and everything was back to working. Had I wired the power lines wrong? I took another look at the datasheet. Nope, the power and ground were on the correct pins… but what was that? A ‘reset’ pin? And it’s active low? Of course it is. I hadn’t wired anything to any of the pins I wasn’t using for I/O because I hadn’t given it much thought. But an active low reset pin getting no power will hold the processor in a halted state. So I tied that pin to 3.3 volts, and the display came to life.

But now the colon wasn’t lighting up. Poring over the code that was supposed to make the colon blink, I saw nothing wrong with it. I almost broke down and asked Glibs for assistance in finding what I missed. Then I had an idea.

I was changing the seconds variable in an interrupt. C code doesn’t normally expect variables to change outside of the course of execution, so the compiler and its integrated optimizer didn’t take that into account. I was initializing seconds to 0, and the compiler was not seeing anything in the main code that was making changes. It had decided to turn that into a constant, and the MSP430 series has a hardware constant generator for each bit, including zeroes. That is much faster than reading a variable from memory. There was still a variable that the interrupt code was incrementing, but the optimizer had detached the blink code from it in an effort to speed up the program. By declaring seconds to be a volatile int instead of an int, the code resumed normal operation the second time around.

With a celebratory comment on the site, I set about drafting this article.

But I’m not done. I want to be able to set the time, to make this not a timer but a proper clock. Well, I haven’t gotten that sorted. I do know that I have five more I/O pins on the F2003 to work with, but I have this reminder from when I loaded the code onto the chip last time:

MSP430: Flash/FRAM usage is 838 bytes. RAM usage is 62 bytes.

The F2003 is close to the bottom of the line of the MSP430 series. It has 128 Bytes of memory and 1kB of flash storage. I have to fit the code for user interactions into the remaining 186 Bytes of storage and 66 Bytes of RAM.

Oof.

The good news is, I think I can do it. I don’t need new variables, so I just need to set up interrupts for some button presses. Except I haven’t done that before. Time to get back to researching.

About The Author

UnCivilServant

UnCivilServant

A premature curmudgeon and IT drone at a government agency with a well known dislike of many things popular among the Commentariat. Also fails at shilling Books

82 Comments

  1. UnCivilServant

    *Insert “furious typing scene” here*

    It’s difficult to make coding exciting.

    • Ownbestenemy

      While reading code isn’t all that exciting, the process is. Thanks for writing these up.

      • UnCivilServant

        You’ll love Entries 5 and 6 where I dissect the C code.

      • R.J.

        I enjoyed it. Nice to see someone else going through the struggles of learning a process. Too often articles start with an expert lecturing, instead of showing all the work it took to get there.

      • UnCivilServant

        I am not an expert at much of anything.

    • Sean

      Hugh Jackman did it, though he did need Holle Berry…

      • UnCivilServant

        But the password was still swordfish.

      • EvilSheldon

        Johnny Lee Miller did it too, but he needed Angelina Jolie…

        That’s why computer crime is so boring in real life – not enough hot chicks.

      • Ownbestenemy

        Even Rami Malek needed Carly Chaikin…

      • Nephilium

        Mr. Robot was good, and Sneakers was entertaining.

      • EvilSheldon

        Sneakers was just full of Easter eggs. Even the part in the end, about the chip only being useful against domestic encryption, was loosely based on prime factoring vs. elliptic curve ciphers, which were not in common use in the West at the time.

      • Gustave Lytton

        It punched well above its weight with the cast.

      • Fatty Bolger

        One of the only movies about “hacking” that didn’t completely annoy me.

      • Nephilium

        Fatty Bolger:

        There’s a reason I said it was entertaining. Mr. Robot also stays mostly realistic with hacking.

  2. ron73440

    Did you pull out all you hair?

    Sounds frustrating.

    • UnCivilServant

      My hair is cut too short to get a grip on, so I did not separate any follicles from my scalp.

    • robc

      I hate dealing with hardware. But this is pretty typical hardware/software interaction debugging.

  3. robc

    From the morning linx:

    Breaking football news: Nottingham Forest’s appeal against 4-point deduction denied.

    My percentages posted over the weekend stay in place.

    In a best case scenario, Forest can secure safety this weekend. In the worst case, they will have a must win against Burnley in their final game.

  4. The Late P Brooks

    Does full autodidacting require a special license?

    • UnCivilServant

      No. It’s officially banned.

    • Drake

      We had 2007 Saab 9-3 Sportcombi. Really liked that car.

    • EvilSheldon

      That looks spiffy!

      • ron73440

        I’m an idiot, so I already replaced the one that was in there.

        I run a Bluetooth through an aux plug.

        My screen had died a long time ago, but I never use it, so I didn’t care.

        One day the sound went flat and only came out of the front.

        I assumed it reverted to the default setting and with no screen I couldn’t change it.

        I bought a used one off of Ebay and swapped it.

        Went into the menu and all of the settings were the same as I had set them previously.

        Turns out, the cord in the aux plug was loose.

        As far as my wife knows, the “new” stereo fixed it.

      • ron73440

        But because of that, I have a spare to mess with.

      • Timeloose

        I did the same with my washing machine once. Bought a new main board, but it was the DC power to the pump that had a intermittent open.

      • Fourscore

        Isn’t there a rule about that? If not, there should be.

        I’ve broken the rule many times, myself.

        Damned tractor won’t start, runs battery down. It’s got gas. Oops, forgot to turn on the shut off. Charges battery. Ahh, I fixed it.

      • ron73440

        Isn’t there a rule about that? If not, there should be.

        The rule that says if I fixed it, all my wife needs to hear are the successful parts, not the unnecessary ones caused by my own stupidity?

        Also, she does not need to know of the expense.

  5. The Late P Brooks

    I’m sitting here trying to lay out a stairway in my imaginary new house. I’m strictly analog.

  6. The Late P Brooks

    So a stairway to heaven, so to speak.

    Pretty much. Levelling this mileaged-out doublewide and replacing it with a barndominium shop/house would be heavenly.

    • kinnath

      I knew a guy that bought a blacksmith shop that came with a house.

  7. The Late P Brooks

    You’re not gonna believe this, but I’ve been looking at “free” “online’ floor plan drawing programs, and they all want me to create an account or log in with google or facebook.

    Funny how that works.

    • kinnath

      I used Visio to layout my house. The architect read the native file directly into his own tool when we started to build.

    • CPRM

      You can download the old Google Sketchup. The new version requires an account, but this old one didn’t, I don’t think. Google doesn’t own it anymore, but did when this version came out.

  8. The Late P Brooks

    I knew a guy that bought a blacksmith shop that came with a house.

    In Montana I bought a barn and built a loft to live in, and had my shop downstairs. I really miss that setup.

  9. Pope Jimbo

    I’m thinking more crowd funding for people who take action against the yahoo campus protesters.

    I see that the frat boys got $500K. I think there should be a fund for the janitor who roughed up the trust fund baby too.

    It would be interesting to see if crowd funding could become a way to finance the opposition to this nonsense. Even better, as crowd sourcing took root, you’d see people coming up with new and innovative ways to humiliate the hipster campers. They’d have to in order to maximize their appeal to the public. I’d think that a motivated entrepreneur would run laps around the Soros funded drones.

    • Nephilium

      Until the crowd funding site pulls the campaign, and refunds the money (minus fees of course) to the donators.

      • ron73440

        You forgot the part where they turn over information on who donated to the government.

    • Toxteth O'Grady

      Anonymous money orders or pictures of presidents, sent to a (private?) PO box.

  10. The Other Kevin

    These are pretty fun. Every mistake you make is one you won’t make again, so this is turning out to be a good experience for you.

    • UnCivilServant

      I wouldn’t say never – but I am trying to share the learning experience.

  11. kinnath

    The BSA is dead.

    Welcome to Scouting America.

    • Fatty Bolger

      They were already moving that way in cub scouts when my youngest was in, and made it official a few years ago. Not surprised to see boy scouts doing it.

      • Stinky Wizzleteats

        The Genitalia Removal Merit Badge, transsexual crafts, Native American Lesbianism Awareness weekend retreats-it’s going to be a hell of an organization but maybe there’ll be less summer camp molestation at least.

      • kinnath

        I learned how to split kindling, start a fire, and cook a meal when I was 11 in the Boy Scouts.

        Old timers were already complaining back then that the BSA was going soft and didn’t teach boys how to build a cabin in the woods anymore.

      • Not Adahn

        If you can get a copy of the B&W editions of the Pioneering or Engineering merit badge books, those were insane.

        It was something like “build a bridge at least ten yards long that can bear 2000 pounds. It much stand for a year to qualify.”

      • kinnath

        By the time I was 12. I had a pocket knife, a hatchet, a full-sized axe, and a full box of matches.

        Some how I didn’t hurt myself or burn anything down.

    • Gender Traitor

      Doggone it! For a moment there I thought you were talking about the Bank Secrecy Act, and I got all excited thinking, “Woo hoo! I don’t have to go through that stupid training every year anymore!” 😒

      • UnCivilServant

        Whatever replaces it will have worse training

    • pistoffnick (370HSSV)

      We considered joining the boy scouts (they were open to girls 20 years ago) when my daughters were of that age. They had much better programs than the girl scouts. Ultimately we went with the YMCA Indian Princesses. I took the name “Bewildered Moose”.

      • kinnath

        We had a son and a daughter. BSA was great. GSA was terrible. I had my daughter tagging along to some BSA activities before they started accepting girls.

      • Toxteth O'Grady

        Campfire Girls still around?

      • kinnath

        wikipedia say:

        Camp Fire, formerly Camp Fire USA and originally Camp Fire Girls of America, is a co-ed youth development organization. Camp Fire was the first nonsectarian, multicultural organization for girls in America. It is now gender-inclusive, and its programs emphasize camping and other outdoor activities.

      • Gender Traitor

        I was a Camp Fire Girl (after being a Blue Bird!) WoHeLo! We sold Russell Stover and Fanny Farmer candy! (Fanny Farmer French and Frosted Mints! Yum! 😋)

    • Not Adahn

      They had already changed their name to “Scouting BSA”

      • Not Adahn

        The Boy Scouts of America is changing its name for the first time in its 114-year history

        renamed Scouts BSA — in 2019.

  12. The Late P Brooks

    Send basketballs

    New York Gov. Kathy Hochul says she regrets making an offhand remark that suggested Black children in the Bronx do not know what the word “computer” means.

    Hochul, a Democrat, made the extemporaneous comment Monday while being interviewed at a large business conference in California to discuss expanding economic opportunities in artificial intelligence for low-income communities.

    “Right now, we have young Black kids growing up in the Bronx who don’t even know what the word computer is. They don’t know, they don’t know these things,” Hochul said while on stage at the Milken Institute Global Conference.

    The remark was not addressed during the interview and the governor went on to explain that her goal is to provide avenues for communities of color to access emerging artificial intelligence technologies as a means to address social inequality.

    Let them eat chips.

    • Toxteth O'Grady

      Lead paint chips?

    • The Other Kevin

      Wow, sounds like those kids go to terrible schools. Someone should do something.

    • Stinky Wizzleteats

      Meh, it’s hyperbole-stupid ill chosen hyperbole but hyperbole nevertheless.

      • Sensei

        But the MSM has no problem running cover for her and what she really meant.

        OTH, if this was somebody from Team Red…

      • The Gunslinger

        Right? Kathy will be scolded with a stern “do better”. A Team Red wrongthinker would be going out on a Noem style apology tour.

      • Toxteth O'Grady

        (Mr. Teats, your dashes confuse me: they look like hyphens. Sorry to mention it.)

      • Stinky Wizzleteats

        I need to refresh my memory on when to use colons, semicolons, and commas and I just haven’t gotten around to it yet. I figured a dash was a good placeholder for the time being.

      • kinnath

        you want an “m” dash. I think the system will automatically give you the dash if you put two minus signs together in your text.

        let’s try it — see if it works.

  13. The Late P Brooks

    “Of course Black children in the Bronx know what computers are — the problem is that they too often lack access to the technology needed to get on track to high-paying jobs in emerging industries like AI,” Hochul said. “That’s why I’ve been focused on increasing economic opportunity since Day One of my Administration, and will continue that fight to ensure every New Yorker has a shot at a good-paying job.”

    Now tell us your plan for teaching kids the fundamental cognitive skills they need to do those jobs.

    • UnCivilServant

      The Power of Melanin will carry them!

    • Sean

      We’re going to get them fathers?

  14. Ownbestenemy

    Think we are going to make a fun Oct trip to the Moonshiners Ball this year.

  15. The Late P Brooks

    OTH, if this was somebody from Team Red…

    BREAKING: TRUMP SPOUTS NAZI EUGENICIST MYTHS ABOUT BLACK PEOPLE!!!!1

  16. Sensei

    Once again the difference between the public and private sector.

    The report, citing the FDIC’s own public reporting, said none of the 92 harassment complaints made from 2015 to 2023 through an anti-harassment program led to “removal, reductions in grade or pay, or any discipline more serious than a suspension.”

    https://www.politico.com/news/2024/05/07/fdic-workplace-misconduct-00156570