Traditional and Structure Approach to C Coding
C2000 Microcontroller Workshop - Peripheral Registers Header Files 3 - 3
Traditional and Structure Approach to C Coding
Traditional Approach to C Coding
#define ADCCTL1 (volatile unsigned int *)0x00007100
...
void main(void)
{
*ADCCTL1 = 0x1234; //write entire register
*ADCCTL1 |= 0x4000; //enable ADC module
}
Disadvantages - Requires individual masks to be generated to
manipulate individual bits
- Cannot easily display bit fields in debugger window
- Will generate less efficient code in many cases
Advantages - Simple, fast and easy to type
- Variable names exactly match register names (easy
to remember)
In the traditional approach to C coding, we used a #define to assign the address of the register and
referenced it with a pointer. The first line of code on this slide we are writing to the entire
register with a 16-bit value. The second line, we are ORing a bit field.
Advantages? Simple, fast, and easy to type. The variable names can exactly match the register
names, so it's easy to remember. Disadvantages? Requires individual masks to be generated to
manipulate individual bits, it cannot easily display bit fields in the debugger window, and it will
generate less efficient code in many cases.