What to learn before embedded lessons?

As a lecturer, I sometimes see students before they begin my embedded course. Most are fairly excited to start on embedded systems (I don't blame them!) and have very little previous experience in embedded systems (except for a little bit of Arduino, here and there). One of the most frequent questions I get is - What can I learn to get up and running ion embedded systems as quickly as possible? The answer is simple - brush up on binary. One, two, four, eight, sixteen, thirty-two, et cetera. You don't need to go too far, we won't be using 64-bit systems, but binary will always be used, and not only for counting. We'll be talking about registers, bit-wise operations, bit flipping, and so on. Believe me, you are going to need it!

Binary isn't just about counting. Deep inside your microcontroller, you will be faced with the joys, and pains, of registers. Let's have a look at a register inside the PIC 16F18877, called WPUA:

This register is used to specify if an internal pull-up is activated or not on the pins of PORT A. This won't be a number, even if when reading from this address, the microcontroller will return an 8-bit number. Lets' say this register is configured using the decimal number 42, or 0010 1010 in binary. That means that WPUA0 is at a logical state 0, or as seen in this extract, pull-up disabled. WPUA1 is at a logical state 1, and therefore the pull-up is enabled. Now, I'd like to activate the internal pull-up on WPUA0, but I can't just write 0000 0001 to the register, otherwise I'll change the configuration for the other pins. I'll need to do a "bit-wise" operation, that's to say read in the contents of the register, apply a specific mathematical function so that the state of one binary digit is changed, and then write that out.

In this case, we could read in the contants (42, or b0010 1010), and add one. The answer would be 43, or b0010 1011, which would be correct, but only in this case. If bit 0 was already at a logical one, then we would add b0000 0001 to b0010 1011, resulting in b0010 1100. Not only would bit 0 not be set, but we would also have changed the state of two other pins that shouldn't have been changed. Luckily, there are mathematical operations for these, and we will be getting further into this subject soon.

This is one of the basics of embedded systems. Sooner or later, you will be performing binary operations, and you need to get it right.