Jump to content
 

What every embedded programmer should know about ...

  • entries
    25
  • comments
    10
  • views
    1,159

Contributors to this blog

About this blog

Over the years programming microcontrollers we have noticed that some problems keep on popping up again and again.

In this blog we will try to address these problems like floating point numbers, concurrency, pointers, interrupts, CRC calculations, sign extension etc. over time. These blogs will not aim to be a full reference, instead they will introduce the problems, explore what can go wrong, show some ways to avoid these problems and provide a load of external links which the reader can go explore for a thorough education on the topic.Although these topics are explained in the context of embedded programming they actually carry over to any kind of programming, but somehow embedded programmers have fallen prey to these more often, perhaps because there is not a lot of material published on these in Embedded Programming communities, until now!

Entries in this blog

 

Getting Started in Embedded Programming Pt 6

Here I am on a clear cool evening, by the fire outside with my laptop.  Tonight I will talk about a new peripheral, the timer/counter.  Periodically I will be interrupted to put another log on my fire, but it should not slow me down too much. Timers and counters are almost the same peripheral with only the difference of what is causing the counter to count.  If the counter is incrementing from a clock source, it becomes a timer because each count registers the passage of a precise unit of t

N9WXU

N9WXU

How to use PIC32 Linker Scripts with XC32 and MPLAB-X IDE

How to use PIC32 Linker Scripts with XC32 and MPLAB-X IDE

I decided to write this up as bootloaders have pretty much become ubiquitous for 32-bit projects, yet I was unable to find any good information on the web about how to use linker scripts with XC32 and MPLAB-X. When you need to control where the linker will place what part of your code, you need to create a linker script which will instruct the linker where to place each section of the program. Before we get started you should download the MPLAB XC32 C/C++ Linker and Utilities Users Gui

Orunmila

Orunmila

Concurrency and race conditions

Concurrency and race conditions

There is one problem we keep seeing bending people's minds over and over again and that is race conditions under concurrency. Race conditions are hard to find because small changes in the code which subtly modifies the timing can make the bug disappear entirely. Adding any debugging code such as a printf can cause the bug to mysteriously disappear. We spoke about this kind of "Heisenbug" before in the lore blog. In order for there to be a concurrency bug you have to either have multipl

Orunmila

Orunmila

 

Getting Started in Embedded Programming Pt 5

It has been a busy few weeks but finally I can sit down and write another installment to this series on embedded programming. Today's project will be another peripheral and a visualization tool for our development machines.  We are going to learn about the UART.  Then we are going to write enough code to attach the UART to a basic C printing library (printf) and finally we are going to use a program called processing to visualize data sent over the UART.  This should be a fun adventure so l

N9WXU

N9WXU

About Translation Units and how C code is compiled

About Translation Units and how C code is compiled

Beginners always have a hard time understanding when to use a header file and what goes in the .c and what goes in the .h.  I think the root cause of the confusion is really a lack of information about how the compilation process of a C program actually works. C education tends to focus on the semantics of loops and pointers way too quickly and completely skips over linkage and how C code is compiled, so today I want to shine a light on this a bit. C code is compiled as a number of "tr

Orunmila

Orunmila

 

Getting Started in Embedded Programming Pt 4

Today I am going to introduce a peripheral.  So far we have interacted with the I/O pins on the micro controller.  This can get you pretty far and if you can manipulate the pins quickly enough you can produce nearly any signal.  However, there are times when the signal must be generated continuously and your MCU must perform other tasks. Today we will be generating a PWM signal.  PWM stands for Pulse Width Modulation.  The purpose of PWM is to produce a fixed frequency square wave but the w

N9WXU

N9WXU

 

Getting started in embedded firmware pt 3

It is now time for my favorite introductory lab.  This lab is my favorite because it.... nope I will not spoil the surprise! Today we will do two labs and we will learn about digital inputs, and pushbuttons. First the digital input.  The PIC16F18446 has 20 pins spread across 3 ports.  PORTA, PORTB and PORTC.  Last time we did not learn about the digital I/O pins but we did learn how to drive a pin as an output.  It turns out that making a pin an output is pretty simple... Just clear th

N9WXU

N9WXU

 

Introduction to Assembly Language on a PIC16F18446

Assembly language may no longer be the mainstream way to write code for embedded systems, however it is the best way to learn how a specific CPU works without actually building one.    Assembly language is simply the raw instruction set of a specific CPU broken into easy to remember pneumonics with a very basic syntax.  This enables you full control of everything the CPU does without any translation provided by a compiler.  Sometimes this is the only reasonable way to do something that cannot be

N9WXU

N9WXU

 

Using : Extern "C"

I realize now that I have a new pet peeve. The widespread and blind inclusion, by Lemmings calling themselves embedded C programmers, of  extern "C" in C header files everywhere. To add insult to injury, programmers (if I can call them that) who commit this atrocity tend to gratuitously leave a comment in the code claiming that this somehow makes the code "compatible" with C++ (whatever that is supposed to mean - IT DOES NOT !!!). It usually looks something like this (taken from M

Orunmila

Orunmila

 

Getting Started with Embedded software Pt 2

Time for part 2!  Last time, I gave you the homework of downloading and installing MPLAB and finding a Curiosity Nano DM164144 .   Once you have done your homework, it is time for STEP 3, get that first project running. Normally my advice would be to breakout Mplab Code Configurator and get the initialization code up and running, but I did not assign that for homework!  So we will go old school and code straight to the metal.  Fortunately, our first task is to blink an LED. S

N9WXU

N9WXU

 

Getting started with Embedded software

I have often been asked "how do I get started" in embedded software.  As I think about this question, I realize that the basic steps to get started with embedded software are nearly identical to the basic steps required to bring up a new MCU or new hardware.  There is always a bootstrapping process and a logic progression of steps before you are "home-free" and building your product.  So here is my bootstrapping process, but broken down into each step so the process is clear for those just start

N9WXU

N9WXU

What every embedded programmer should know about ADC measurement

What every embedded programmer should know about ADC measurement

What every embedded programmer should know about ADC measurement, accuracy and sources of error ADC's you encounter will typically be specified as 8, 10 or 12-bit. This is however rarely the accuracy that you should expect from your ADC. It seems counter-intuitive at first, but once you understand what goes on under the hood this will be much clearer. What I am going to do today is take a simple evaluation board for the PIC18F47K40 (MPLAB® Xpress PIC18F47K40 Evaluation Board ) and de

Orunmila

Orunmila

Using the CDC Serial port on the PIC18F47K40 Xpress Evaluation Board

Using the CDC Serial port on the PIC18F47K40 Xpress Evaluation Board

I recently got my hands on a brand new PIC18F47K40 Xpress board (I ordered one after we ran into the Errata bug here a couple of weeks ago). I wanted to start off with a simple "Hello World" app which would use the built-in CDC serial port, which is great for doing printf debugging with, and interacting with the board in general since it has no LED's and no display to let me know that anything is working, but immediately I got stuck. Combing the user's guide I could not find any mention of

Orunmila

Orunmila

 

Initializing Strings in C

Embedded applications are hard for a large number of reasons, but one of the main issues is memory.  Today I want to talk about how our C variables get initialized and a few assumptions we make as we use C to write embedded software. Let us take a few simple declarations such as we might make every day. char *string1 = "string1"; const char *string2 = "string2"; char const *string3 = "string3"; char * const string4 = "string4"; char const * const string5 = "string5";   In C99 th

N9WXU

N9WXU

How to struct - lessons on Structures in C

How to struct - lessons on Structures in C

Structures in the C Programming Language Structures in C is one of the most misunderstood concepts. We see a lot of questions about the use of structs, often simply about the syntax and portability. I want to explore both of these and look at some best practice use of structures in this post as well as some lesser known facts. Covering it all will be pretty long so I will start off with the basics, the syntax and some examples, then I will move on to some more advanced stuff. If you are an

Orunmila

Orunmila

 

Tokenizing Keywords Pt 5

Today we are going to explore the weird and wonderful trie (try).  We are actually going to brute force a trie and we will write a basic code generator to synthesize a static trie so it our trusty NMEA token tester can stay in flash memory and fit in the PICmcu.  I promise this is going to be the LEAST memory efficient trie you ever will see, but it is quick and it is easy to understand so let us press on. First things first, what is a trie?  Simply, the trie takes the if-else construct we

N9WXU

N9WXU

Tokenizing Keywords Pt 4

Tokenizing Keywords Pt 4

This week we will discuss a different strategy for tokenizing our words.  This strategy will be to convert our word to a number in an unambiguous manner.  Then we can simply see if our number is one we recognize.  How hard can that be? The process of converting something into an unambiguous number is hashing.  Hash algorithms come in all shapes and sizes depending upon their application needs.  One nice hash is the Pearson hash which can make an 8-bit number of a string.  For a reasonably c

N9WXU

N9WXU

Tokenizing Keywords Pt 3

Tokenizing Keywords Pt 3

“Code generation, like drinking alcohol, is good in moderation.” — Alex Lowe This episode we are going to try something different.  The brute force approach had the advantage of being simple and easy to maintain.  The hand-crafted decision tree had the advantage of being fast.  This week we will look at an option that will hopefully combine the simplicity of the string list and the speed of the decision tree.  This week we will use a code generator to automatically create the tokenizi

N9WXU

N9WXU

 

The secret weapon of the 8-bit PICmcu

The PICmcu is not known for being fast or supporting large memories but it does have one feature that can significantly simplify developing your applications.  That feature is the lack of a hardware call stack. Sacrilege you say!  But wait... The lack of this important feature has caused the compiler team to develop an incredibly useful alternative that I would argue is better in nearly every way than an actual stack. For those of you who are now wondering what I am talking about,

N9WXU

N9WXU

Schedulers, Timers and RTCOUNTER

Schedulers, Timers and RTCOUNTER

Whenever I start a new project I always start off reaching for a simple while(1) "superloop" architecture https://en.wikibooks.org/wiki/Embedded_Systems/Super_Loop_Architecture . This works well for doing the basics but more often than not I quickly end up short and looking to employ a timer to get some kind of scheduling going. MCC makes this pretty easy and convenient to set up. It contains a library called "Foundation Services" which has 2 different timer implementations, TIMEOUT and RTC

Orunmila

Orunmila

 

Tokenizing Keywords - Part 2

When we left off we had just built a test framework that allowed us to quickly and easily try out different ways to identify NMEA keywords.  The first method shown was a brute force string compare search.  For this week, I promised to write about an if-else decoder.  The brute force search was all about applying computing resources to solve the problem.  This approach is all about applying human resources to make life easy on the computer.  So this solution will suck.  Let us press on. The

N9WXU

N9WXU

 

Tokenizing Keywords - Part 1

This is the first of a 5 part article where I will explore some different ways to tokenize keywords.  This is a simple and common task that seems to crop up when you least expect it.  We have all probably done some variation of the brute force approach in this first posting, but the posts that follow should prove interesting.  Here is the sequence: Part 1 : STRCMP Brute Force and the framework Part 2 : IF-ELSE for speed Part 3 : Automating the IF-ELSE for maintenance Part 4 :

N9WXU

N9WXU

Using the MPLAB-X Simulator to debug UART code

Using the MPLAB-X Simulator to debug UART code

One feature of MPLAB-X which is not nearly used enough is the Simulator. It is actually very powerful and useful for testing and debugging code which will run on your board and interact with the outside world via external stimuli. There is more information on the developer help site here http://microchipdeveloper.com/mplabx:scl This is how you can use the IDE to debug your serial command line interface code using stimulus files and the UART OUTPUT window. The first trick is to enable t

Orunmila

Orunmila

Duff's Device

Duff's Device

What every embedded programmer should know about … Duff's Device "A trick used three times becomes a standard technique" (George Polya) C is a peculiar language and knowing the internal details of how it works can be very powerful. At the same time, when we say things like that of course we first think of the Story of Mel which we wrote about before, so be careful not to write unmaintainable code! Probably the most famous "trick" in C is something called  Duff's Device. It was d

Orunmila

Orunmila

Floating Point Numbers

Floating Point Numbers

What every embedded programmer should know about … Floating Point Numbers "Obvious is the most dangerous word in mathematics."  E. T. Bell “Floating Point Numbers” is one of those areas that seem obvious at first, but do not be fooled! Perhaps when we learn how to do algebra and arithmetic we are never bound in our precision in quite the same way a computer is, and when we encounter computers we are so in awe by how “smart” they are that it becomes hard to fathom how limited flo

Orunmila

Orunmila

×
×
  • Create New...