EE403W Senior Project
Design
Section 4 – Embedded Systems
C Tutorial
QUIZ
// initialization section
unsigned char x = 2;
// execution section
if (x = 7)
x = 0;
• Aft
After thi
this code
d runs, what
h t is
i the
th value
l stored
t d in
i the
th memory
location referenced by the variable ‘x’?
2
‘C’ Programming Language
Why program microcontrollers in ‘C’?
• More compact
p code (visually
( y speaking)
p g)
• Less cryptic code – easier to understand
• Easier to maintain/update
• Easier to manage large projects w/ multiple programmers
• More portable (to an extent)
• ‘C’ is a more marketable skill (than BASIC, etc)
Why NOT program in ‘C’?
• $$$ for a compiler
• Assembly potentially more compact code (memory size, execution speed)
- Assuming you have a competent assembly programmer on-hand
• M bbe quicker/easier
May i k / i for f very small
ll projects
j t tot code
d in
i ASM (< 1kbyte)
1kb t )
3
‘C’ Programming Language
C vs. Assembly Language
C Assembly Machine
i=3 LDAA #$03 4000:86 03
STAA $800 4002:7A 08 00
j=5 LDAA #$05 4005:86 05
STAA $
$801 4007:7A 08 01
k=i+j LDAA $800 400A:B6 08 00
ADDA $801 400D:BB 08 01
Compiler converts STAA $803 4010:7A 08 03
A
Assembler
bl converts
t Downloaded
D l d d
to chip
4
‘C’ Programming Language
Going from Assembly to Machine Code requires an Assembler
Example.s19
Example.asm Assembler Linker
Source code Object code Executable - 1s & 0s
that get loaded into
program memory
5
‘C’ Programming Language
Going from ‘C’ to Machine Code requires a Compiler,
Assembler & Linker Adds .lib,
lib function calls
calls,
Compiler and links all object files
Example.c Pre-
processor
p Linker Example.s19
p
Compile Assembler Example.o
Source code Optimize Example asm
Example.asm
Executable - 1s & 0s that
Iterative process, get loaded into program
multiple passes memory
Usually a compiler option
– optimize for code size,
execution speed
‘C’ Programming Language
Basic Program Structure
#______ are preprocessor directives
#include < stdio.h>
Every program needs 1 (and only 1)
void main( ) main function
{
printf("\nHello World\n");
} printf( ) is a function defined in stdio.h
Function is in brackets
‘C’ Programming Language
Use Lots of Comments!!!
1. Traditional ‘C’ comments
/* Everything between is a comment */
2. C++ style comments
// Everything on this line is a comment
3. Preprocessor-enforced comments
#if (0)
Everything between is a comment;
#endif
8
‘C’ Programming Language
Variable Names & Keywords
Variable Names - can be up to 31 characters long
- may use upper/lower case letters, digits 0-9, and ‘_’
- compiler & library vars use ‘_’ as first char in names
Reserved keywords – can’t be used for var names
auto double int struct
break else long switch
case enum register typedef
char
h extern
t return
t union
i
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
‘C’ Programming Language
Data Types
char 8 bits Integers types are signed by
short 16 bits default.
l
long 32 bits
bi
long long 64 bits
float 32 bits / 10+/-38
+/-10 ~ 6.5 significant digits
double 64 bits +/-10+/-308 ~ 15 significant digits
int Usually depends on architecture
(32-bits for x86s 16 bits for HCS08)
Signed #s use Two
Two’ss complement form
• signed char is 8 bits, range is -128 to +127
• unsigned char is 8 bits, range is 0 to +255
‘C’ Programming Language
Straight binary (unsigned)
MSB LSB
1 0 0 1 1 1 0 1 = 0x9D
1 x 27 + 0 x 26 + 0 x 25 + 1 x 24 + 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 15710
Total range of possible values is 010 Æ 25510
To divide
di ide b
by ttwo,
o shift one position to the left
MSB LSB
LSB = 0 if even #
0 1 0 0 1 1 1 0 = 0x4E = 1 if odd #
0 x 27 + 1 x 26 + 0 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 7810
‘C’ Programming Language
Useful #
conversion
chart
12
‘C’ Programming Language
ASCII text
American
A i St d d Code
Standard C d
for Information Interchange
• ASCII iis often
f used d iin
computer systems to
represent characters
- Hyperterm
H
- Many LCD screens
13
‘C’ Programming Language
Math Operators
+ Addition
- Subtraction
* Multiplication Note: ‘*’ and ‘/’ have higher
/ Division pprecedence than ‘+’ and ‘-’
% Modulus operator
if Ans, Rem and the numbers 5 and 8 are integers, then
Ans = 5/8; // result is 0
Rem = 5%8; // result is 5
‘C’ Programming Language
Convert a number to ASCII
unsigned
i d char
h value;
l
…
value = 0xF5;; // 245 base 10
On hyperterm window you’d see …
temp1 = (value%10)+0x30;
temp2 = value/10; 2
temp3 = temp2/10+0x30;
temp2 = temp2%10+0x30; 4
5
printf(temp3”\n"); // MSB
printf(temp2”\n");
printf(temp1”\n"); // LSB
‘C’ Programming Language
Increment & Decrement
i = i + 1; is equivalent to … i++;
k = k - 1; “ “ k--;
C allows
ll pre- and
d post-incrementing
ti ti andd pre- and
d post-decrementing
td ti
num = 1; num = 0;
while (num++ < 3) while (++num < 3)
{ {
// do something // do something
}; }
Are these
code snippets
equivalent?
‘C’ Programming Language
Shift
x = 8;
x = x >> 2;
0000 1000 (810) → 0000 0010 (210)
Equivalent to dividing by 22
y = 8;
y = y << 3;
0000 1000 (810) → 0100 0000 (6410)
Equivalent to multiplying by 23
May take less clocks than executing a multiply or divide instruction
‘C’ Programming Language
Logical Operators
NOTE:
< less than if (0); // Is always false
<= less than or equal to
> greater than
h if (1); // Is always true
>= greater than or equal to
== is equal to
!= is not equal to Logical operators are binary operators
The statement
&& AND
|| OR if (A >= B) …
Returns 0 if the statement is false and 1
if true
‘C’ Programming Language
Bitwise Operators
& Bitwise AND
| Bitwise OR
~ Bitwise NOT Note:
^ Bitwise Exclusive OR
B &= MASK;
is equivalent to
#define MASK (%1111 0000)
B = B & MASK;
A = 0x88 & MASK; // result is 0x80
B = 0x88 | MASK; // result is 0xF8
C = 0x88 ^ MASK;; // result is 0x78
C = ~C; // result is 0x87
‘C’ Programming Language
Loops – for loop
start end increment
for (i=0; i<10; i++)
power[i] = volts[i] * current[i];
for(;;)
{
//loop forever
}
‘C’ Programming Language
Loops – while loop
cntr = 0;
while (cntr < 10) // loop will execute 10 times
{
num[cntr] = 5;
cntr++;
}
while (1)
{
// this loop will execute forever
}
‘C’ Programming Language
Loops – do while loop
cntr = 0;
do
{
num[cntr]
[ ] = 5;
cntr++;
} while (cntr < 10); // still executes 10 times
‘C’ Programming Language
If statement
if (num <= 10 || eli==7)
‘else if’ and ‘else’ never get tested if
{
“if (num<=10) is TRUE
// do
d something
thi
}
else if (num >= 20)
Can have onlyy one ‘if’ and one ‘else’, but
{ as many ‘else if’s as you want
// do something
}
else
{
// default case
}
‘C’ Programming Language
The switch statement
switch (buffer[3]) If you were to look at the assembly
{ or machine code, switch and if-else
case 1: statements are functionally
// execute function 1 equivalent. But if there are many
b k
break; cases, a switch statement is usually
case 2:
easier to look at, add to, etc.
//function eli
case 3:
case 5:
// execute function 2 Switch statements lend themselves
break; well to things like command parsers
… and
d state
t t machines.
hi
case n:
// execute function n
break;
default:
// execute function _ERROR
break;
}
‘C’ Programming Language
Functions
FUNCTION
type function_name( type, type, type, …) PROTOTYPE
- At top of ‘C’ file or
included in header
Return argument – can file
be char, int, etc. Values passed to a
function – one way
‘void’
void means no return copy to function
argument
‘void’ means no
If not ‘void’ function values passed
needs
d a ‘‘return
t (value)’
( l )’
statement at the end
‘C’ Programming Language
// Includes Functions (cont.)
#include <Timer.h>
// Function PROTOTYPES
// Function PROTOTYPES
void config_Timer (void);
void config_IO (void);
// MAIN Routine
void main (void)
{
// Configure Port I/O
config_IO ();
// Initialize Timer 3
config_Timer();
fi Ti ()
}
}
// Other Routines
Timer.h
void config_IO (void)
{
//Set up micro I/O ports
} Main.c
EE403W.4 Spring 26
‘C’ Programming Language
Note on Recursion / Reentrancy
n! = n * (n-1) * (n-2) * …
Function calculates a factorial by
llong factorial
f i l (int
(i n)) calling itself until n = 0.
0
{
Need to be careful doing this, every
if (n == 0) function call puts multiple bytes on the
return (1); stack. If not terminated correctly could
overflow the stack very easily.
else
return (n * factorial (n-1));
}
‘C’ Programming Language
Why use functions?
• Makes code more modular – easier to read
• If sections of code are repeated multiple times, putting that
code in a function saves code space
• If section of code is not repeated more than once, function call
adds extra code (and hence runtime)
• What iff you
y want the modularityy but not the extra stuff,
ff what
do you do?
‘C’ Programming Language
Macros
A way to “modularize” code without the penalty of a function call
In ‘file_name.h’ …
#define square (x) (x) * (x)
In ‘file
file_name.c
name.c’ …
Power = square (I) * R;
If you look at the compiled code,
code the macro definition gets inserted
as in-line code, whereas functions get treated as jumps to a single
block of code somewhere else in memory.
‘C’ Programming Language
Local Variables vs. Global Variables
// Function PROTOTYPES
void calc_number (void);
static unsigned char this_is_global;
// Main Routine
void main (void)
{
unsigned char this_is_local;
this_is_global = 10; This won’t compile - error.
this_is_local = this_is_global;
calc_number ( );
}
Why? Global var’s get dedicated
// Other Routines
void calc
calc_number
number (void)
memory locations, local variables
{ all share a section of ‘scratchpad’
unsigned temp1, temp2; memory – the compiler figures out
temp1 = this_is_global;
temp2 = this_is_local; exactly which variable gets which
} memory location
l ti att any one time.
ti
‘C’ Programming Language
How to share Global Variables among multiple ‘C’ files
// Main.c
#include <main.h> // main.h
unsigned char this_is_global = 7; extern unsigned char this_is_global;
extern void calc_number ( );
// Main Routine
void
id main
i (void)
( id)
{
unsigned char this_is_local; // Algorithm.c
this_is_local = this_is_global; #include <main.h>
calc_number ( );
run_algorithm ( ); // Routine
R ti
} void run_algorithm (void)
{
// Other Routines unsigned char this_is_local_too;
void calc
calc_number
number (void) this_is_local_too = this_is_g
global;
{ calc_number ( );
unsigned temp1; }
temp1 = this_is_global;
}
Variables and functions can be external / global.
‘C’ Programming Language
Arrays
unsigned char cnum[5] = {5, 7, 2, 8, 17}; // 5 bytes of memory
unsigned int inum[5]; // 10 bytes
fl fnum[5];
float f [5] // 20 bytes
b
Mem Location Value
0100 5
Address of cnum[0]
0101 7
0102 2
0103 8
0104 17
‘C’ Programming Language
Multidimensional Arrays
unsigned char cnum[2][3];
cnum[0][0] = 3;
cnum[0][1] = 6;
Mem Location
i Value
l
cnum[0][2] = 8;
cnum[1][0] = 1; 0100 3
cnum[1][1]
[1][1] = 0;
0 0101 6
cnum[1][2] = 12; 0102 8
0103 1
0104 0
0105 12
‘C’ Programming Language
Pointers
Memory Value
Location
* - deference operator F004 F00C p
& - address of F008 q
F00C 5 i
unsigned int *p, *q; F010 F004 r
unsigned
g int i;;
F014
unsigned int **r
i = 5;
p = &i; *p = 0; // like saying “set the contents of
// memory location pointed to by p
r = &p;
// to 0” (i.e. i = 0)
**r = ?
‘C’ Programming Language
Pointers – another example
You are working on a 16 bit machine, and the memory
location at absolute address 0x67A9 needs to be set to an
initialization value of 0xAA55. How do you do it?
iintt *ptr;
* t
ptr = (int *) 0x67A9; //Type Cast!
* = 00xAA55;
*ptr AA55
PORTA_DATA_REG = 1;
#define PORTA_DATA_REG *(unsigned char *)(0x0004)
‘C’ Programming Language
Advantage of Using Pointers
• Allows you to directly access machine
function call
memory
– i.e. contents of specific registers BufCopy (num, &outputBuf, &inputBuf);
• Helps to modularize code
– can pass a pointer in a function call
void BufCopy (char nbytes, char *DstBufPtr, char *SrcBufPtr)
{
while (nbytes-- > 0)
{
*DstBufPtr = *SrcBufPtr;
DstBufPtr++; SrcBufPtr++; function
}
}
‘C’ Programming Language
typedef, struct and union
struct FOURBYTES
{ union FLOAT Impedance [8],
char byte4; unsigned char I_bytes [8][4];
char byte3;
char byte2; Impedance [6].f = 23.556;
char byte1;
}
}; II_bytes
b t [6][0] = Impedance
I d [6].b.byte1;
[6] b b t 1
I_bytes [6][1] = Impedance [6].b.byte2;
typedef union FLOAT I_bytes [6][2] = Impedance [6].b.byte3;
{ I bytes [6][3] = Impedance [6].b.byte4;
I_bytes [6] b byte4;
float f;
struct FOURBYTES b;
};
‘C’ Programming Language
typedef, struct and union
typedef struct
{
unsigned char BIT_0 : 1;
unsigned
g char BIT_1 : 1;; // Definition
unsigned char BIT_2 : 1; BITFLAG UserFlags;
unsigned char BIT_3 : 1;
unsigned char BIT_4 : 1;
// In code
unsigned char BIT_5 : 1;
unsigned char BIT_6 : 1; UserFlags.BIT_0 = TRUE;
unsigned char BIT_7 : 1; UserFlags.BIT_1 = FALSE;
} BITFLAG;
BITFLAG
#define TRUE (1)
#define FALSE (0)
‘C’ Programming Language
Preprocessor Directives
#include
#define
#pragma #define
#d fi GREEN
#define RED
…
Conditional compilation
p #if (GREEN)
#if / #ifdef / #ifndef //compile this code
#elif …
#elif (RED)
#else // compile this code
#endif …
#endif
‘C’ Programming Language
Other keywords – void process_buttons (void)
{
static static unsigned char button_old = 0;
button_new = PORTA & %0000 0001;
1. Local variables declared if (button_new & button_old)
static maintain their value {
between function // button press TRUE 2 times
// in a row do something!
invocations }
2. Functions declared static b
button_old ld = button_new;
b
}
can only be called by
functions in the same
module
‘C’ Programming Language
Other keywords – volatile
• Wh
When a variable
i bl iis declared
d l d volatile,
l il theh compiler
il is
i forced
f d to
reload that variable every time it is used in the program.
Reserved for variables that change frequently.
- Hardware Registers
- Variables used in interrupt service routines (ISR)
- Variables shared by multiple tasks in multi
multi-threaded
threaded apps
Ex.
volatile unsigned char UART_Buffer
UART Buffer [48];
// UART_Buffer is used in the UART ISR to
// record the incoming data stream
‘C’ Programming Language
Other keywords – const
• Doesn’t mean ‘constant’, means ‘read-only’
• The program may not attempt to write a value to a const
variable
i bl
• Generates tighter code, compiler can take advantage of some
additional optimizations
p