User Tools

Site Tools


tutorials:microcontroller_programming_tips

This is an old revision of the document!


Microcontroller Programming Tips

When programming microcontrollers (like the MSP430), a few things will be different from what you’re used to on a laptop.

Registers: You will often be interacting with hardware by writing to and reading from registers. These registers have names, just like variables. Often you will use bit-wise operations, like & and | to set or clear a single bit in the register.

Volatile: The volatile keyword is part of the C standard, but it isn’t used much on laptops. When you label a variable as “volatile” you are telling the compiler that that variable can change even if the program doesn’t explicitly write to it. When a hardware event may update a value without any action from the program, making that variable “volatile” will prevent the compiler from optimizing that variable out (since it may otherwise think that the variable isn’t used).

Pins: The processor interacts through different components of your device through its pins (the metal tabs coming off the sides of the chip). Some pins can be used in different ways.

Digital Pins: A digital pin or GPIO (general purpose input/output) pin can be used in two different modes: input and output. In output mode, it can be set to a high voltage or a low voltage. In input mode, the pins voltage floats and the processor can read whether the pin is high or low.

ADCs: The Analog to Digital Controller is a component in the MSP430 (and many other microcontrollers) that allows your program to convert an analog voltage to a digital value. Some sensors provide their output as an analog voltage. Connecting that voltage to a pin in ADC mode will allow your program to measure the voltage.

DACs: A Digital to Analog Controller is the opposite of an ADC. You give it a number and it converts that number into an analog voltage. This is useful if you want to generate an analog signal that varies over time.

Interrupts: You may have heard of interrupts in a class, but most laptop programmers don’t actually deal with them. On microcontrollers, you will. The idea behind interrupts is that you want some code to run when some hardware event occurs. On the MSP430, you can request an interrupt whenever a particular pin goes from low to high (rising edge) or from high to low (falling edge). There are also some internal interrupts that can be handled for events like timers. Your interrupt handler will interrupt whatever is currently running on the processor. So, as a general rule, you want interrupt-handling code to be as short and fast as possible. Long running interrupt handlers can prevent other events from being detected, and usually causes undesired behavior.

UART/SPI/I2C: These are three common serial communication technologies that are all supported by the MSP430. If you ever need to send or receive binary data from another microcontroller, sensor, or other component, you will probably be using one of these three. UART is probably the simplest. SPI is probably the most common.

Memory: The MSP430s we use have very little RAM (usually 2Kb). This means that the usual games you play on your laptop have to change. For one, we generally do not use dynamic memory allocation — no malloc, calloc, realloc, free, new. Everything is static. The reason is that with so little memory, having a heap that grows dynamically at runtime makes things really hard to debug. Your stack grows from one end of that 2KB. Your globals go on the other end. If your stack ever grows too large, it will start overwriting the globals (or the heap, if you have one). For this reason, we have a fairly strict no-heap policy. It just makes life easier. Note, that recursion, deep function call graphs, and passing large arrays or structs as parameters are similarly dangerous. You want to keep your stack small, so it doesn’t run over your globals.

FRAM: RAM may be tight, but you do have FRAM to use. FRAM is like Flash memory (it’s nonvolatile) but faster and less complicated to use. On the MSP430s, your code is stored in FRAM, and the remaining FRAM is available for data that you want to keep around even if your device reboots.

tutorials/microcontroller_programming_tips.1730233691.txt.gz · Last modified: 2024/10/29 20:28 by ibchadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki