Assumes experience with assembly language programming.
V. P. Nelson
Spring 2013
ELEC 3040/3050 Embedded Systems Lab –
V. P. Nelson
Outline
• Program organization and microcontroller memory • Data types, constants, variables
• Microcontroller register/port addresses
• Operators: arithmetic, logical, shift
• Control structures: if, while, for
• Functions
• Interrupt routines
Spring 2013
ELEC 3040/3050 Embedded Systems Lab –
V. P. Nelson
Basic C program structure
#include
#include
/* common defines and macros */
/* I/O port/register names/addresses for the MC9S12C32 microcontroller */
/* Global variables – accessible by all functions */ int count, bob;
//global (static) variables – placed in RAM
/* Function definitions*/ int function1(char x) { //parameter x passed to the function, function returns an integer value int i,j;
//local (automatic) variables – allocated to stack or registers
-- instructions to implement the function
}
/* Main program */ void main(void) { unsigned char sw1;
//local (automatic) variable (stack or registers) int k;
//local (automatic) variable (stack or registers)
/* Initialization section */
-- instructions to initialize variables, I/O ports, devices, function registers
/* Endless loop */ while (1) {
//Can also use: for(;;) {
-- instructions to be repeated
} /* repeat forever */
}
ELEC 3040/3050 Embedded Systems Lab –
Spring 2013
V. P. Nelson
Declare local variables
Initialize variables/devices
Body of the program
Address
0x0000
0x0400
0x0800
0x1000
0x4000
MC9S12C32 memory map
I/O Registers
Vacant
2KB RAM
16KB Flash
Memory
Vacant
0xFF00
Spring 2013
2K byte RAM [0x0800..0x0FFF] for variable & stack storage
Vacant
0x8000
0xC000
Control registers for I/O [0x0000..0x03FF]
16KB Flash
Memory
16K byte Flash EEPROM [0x4000..0x7FFF] for program code & constant data storage