35
• Flags are important event markers and so wherever they are present and needed you must
use and check them unless automatically cleared. For instance, it necessary to clear timer flags
upon exiting timer interrupts.
• Try to avoid polling methods. Try to use interrupt-based ones but make sure that there is no
interrupt-within-interrupt case or otherwise your code may behave erratically. Best is to
attach interrupts for important tasks like timing-related jobs, ADC conversions and
communications. It is up to your design requirements and choices.
• Where fast processing is required, try to mix assembly with your C-code if you can or
temporarily speed up your micro by increasing its oscillator speed or switching to a faster clock
source. Checkout the assembly examples from TI’s Resource Explorer and MSP430Ware. Also
try to study and learn about advanced C concepts like pointer, structures, functions, etc.
• Avoid empty loops and blank conditional statements.
• When you hover mouse cursor over a function, a small window appears. This window shows
the internal coding of that function and relieve you from opening a new tab for it.
• CTRL + Space or code assist is a great helper. Likewise, CCS has some auto complete features.
• If you are using multiple computers during your project’s development stage, make sure that
your custom library locations and workspace paths are properly added.
• Try to follow compiler advices and optimizations. Study the MSP430 header files if you can.
• You can straight include the header file of your target MCU like as shown below if code cross
compatibility among different MCUs of the same group like MSP430x2xx is not needed:
#include <msp430g2553.h>
instead of:
#include <msp430.h>
The latter is universal for all MSP430 micros and should be used unless otherwise.
• Bitwise and logic operations are useful. Not only they are fast, they just deal with the
designated bits only. Although the MSP430 header files have efficient support for such
operations, it is still better to know them. Shown below are some such common operations:
//For setting a bit of a register
#define bit_set(reg, bit_val) reg |= (1 << bit_val)
//For clearing a bit of a register
#define bit_clr(reg, bit_val) reg &= (~(1 << bit_val))
//For toggling a bit of a register
#define bit_tgl(reg, bit_val) reg ^= (1 << bit_val)
//For extracting the bit state of a register
#define get_bit(reg, bit_val) (reg & (1 << bit_val))
//For extracting masked bits of a register
#define get_reg(reg, msk) (reg & msk)