C Programming for Embedded Microcontroller Systems

Topics: For loop, Perl, Data types Pages: 15 (1596 words) Published: October 27, 2013
C programming for embedded
microcontroller systems.
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
16KB Vacant: [0x8000..0xBFFF]
(Available in larger devices - MC9S12C64/96/128)
16K byte Flash EEPROM [0x4000..0x7FFF]
for program code & constant data storage
Interrupt vectors: [0xFF00..0xFFFF]
(Last 256 bytes of Flash EEPROM)
ELEC 3040/3050 Embedded Systems Lab –
V. P. Nelson

Microcontroller “include file”
• CodeWarrior provides a derivative-specific “include file” for each microcontroller, which defines memory addresses and
symbolic labels for CPU and peripheral function registers.
#include
/* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "MC9S12C32"
// DDRA and PORTA addresses are defined in MC9S12C32.h
void main(void) {
DDRA = 0xff;
// Set direction of Port A as output
PORTA = 0x55; // Set bits of Port A to 01010101
for(;;) {}
/* execute forever */
}

Spring 2013

ELEC 3040/3050 Embedded Systems Lab –
V. P. Nelson

CodeWarrior C data types
• Always match data type to data characteristics
• Variable type indicates how data is represented

• #bits determines range of numeric values
• signed/unsigned determines which arithmetic/relational
operators are to be used by the compiler
• non-numeric data should be “unsigned”
Data type declaration * Number of bits
char k;
8
signed char k;
unsigned char k;
8
int k;
16
signed int k;
short k;
signed short k;
unsigned int k;
16
unsigned short k;
* First (black) form is preferred

Spring 2013

Range of values
-128..+127
0..255
-32768..+32767

0..65535

ELEC 3040/3050 Embedded Systems Lab –
V. P. Nelson

Data type examples
• Read bits from PORTA (8 bits, non-numeric)
– unsigned char n; n = PORTA;

• Read TCNT timer value (16-bit unsigned)
– unsigned int t; t = TCNT;

• Read 10-bit value from ADC (unsigned)
– unsigned int a; a = ADC;

• System control value range [-1000…+1000]
– int ctrl; ctrl = (x + y)*z;

• Loop counter for 100 program loops (unsigned)
– unsigned char cnt; for (cnt = 0; cnt < 20;...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Avr Microcontroller and Embedded Systems Essay
  • Embedded Systems Essay
  • Embedded Systems 2 marks Essay
  • Essay about C Programming
  • Programming in C# Essay
  • programming C Essay
  • C++ Programming Language Essay
  • C Programming Tutorial Essay

Become a StudyMode Member

Sign Up - It's Free