Programming The Encore Using ANSI C
Programming The Encore Using ANSI C
1
Outline
1. Project structure: Source files, include files
2. C data types and derived types
3. Looping (while, for, do-while)
4. Viewing variables in the debugger
5. Conditional flow (if-then and switch)
6. Shorthand
7. Functions and parameter-passing
8. Debugging functions
9. Multi-file projects
2
Project structure
Project
uC selection
Editor settings
Debugger settings
Source files
External dependencies
Include files
Object code libraries
3
Source files
Typically main.c
contains the main()
function, where execution
starts
Split program up into
several functions
Often one function for each
source file
4
External dependencies
Declared by #include <filename>
Include files that declare std C functions, e.g.
stdio.h (standard I/O, e.g. printf, scanf)
math.h (e.g. sin, exp, rand)
5
C Data Types
Character (8-bits)
char c1, c2;
c1 = ‘a’; c2 = ‘\n’;
Integer (16-bits)1
int i;
i = -20; // a decimal number
Floating point (32-bits)
float f1, f2;
f1 = 2.3; f2 = 1.5e3;
1
by default, short is the same as int
6
C Data Types …
Byte (8-bits)
unsigned char a,b;
a = 0xaf; b = 128;
Word (16-bits)
unsigned int i;
i = 0xffff; // hexadecimal FFFFh
Long (32-bits)
long i;
i = -100000;
7
C Derived Types
Array
int tbl[5];
tbl[0] = 3; tbl[4] = -1;
String
char * name = “Encore”;
name[0] = ‘I’;
(name will now contain “Incore”)
8
Conditional if
if (i==1)
ch = ‘y’;
else
ch = ‘n’;
if (i>0) f = f+1;
if (i>=j) {
temp = i; i = j; j = temp;
}
9
Conditional switch
switch (val) {
case 1:
ch = ‘q’; break;
case 2:
ch = ‘z’; break;
default:
ch = ‘x’; break;
}
10
while Loops
int tbl[5]={3,2,-1,4,6}, sum, i;
sum = 0;
i = 0;
while (i<5) {
sum = sum + tbl[i];
i = i + 1;
}
11
for and do-while loops
for loops
sum = 0;
for (i=0; i<5; i=i+1)
sum = sum + tbl[i];
do-while loops
sum = 0; i = 0;
do {
sum = sum + tbl[i];
i = i + 1;
} while (i < 5);
12
Other operations
Math
+, -, *, /, % (remainder)
Logical
! (not), && (and), || (or)
Comparison
<, <=, >, >=, == (equal), != (not equal)
Bitwise
~ (not), & (and), | (or), ^ (xor),
>> (right shift), << (left shift)
13
Logical and bitwise ops
int a=0xf0, b=0x21, c, d, e;
c will be 20h
c = a & b;
d = a && b;
d will be 01h
e = b << 2;
e will be 84h
14
Shorthand
Increment/Decrement
int i,j,k;
i = 3; j will be 3; i will be 4
j = i++;
k = ++i; k will be 5; i will be 5
Accumulator operations
Equivalent to i=i+4; i will be 9
i += 4;
PAOUT |= 0x0f; Sets lower nibble of PAOUT high
PAOUT &= 0xfb;
Sets bit 2 (3rd bit) of PAOUT low
15
Functions
delay() {
…
}
main() {
…
while (1) {
…
delay();
…
}
}
16
Passing arguments
delay(int imax) {
int i,j;
for (i=0; i<imax; i++) {
for (j=0; j<0xff; j++) { }
}
}
main() {
…
delay(0x80);
…
}
17
Returning arguments
float getfreq(int ch) {
…
return clkfreq/count;
}
main() {
…
float freq;
freq = getfreq(1);
…
}
18
Why use functions?
Re-use code
Keep size of each function small,
especially main()
Rule of thumb: < 100 lines
Can repeat variable names for local use
If placed in separate files, functions can be
compiled separately
Speeds up overall compilation
Faster debugging cycle
Necessary for vectored interrupts
19
Debugging functions
Step into (F11)
Step into:
Enter and
debug the
subroutine
Step over:
Execute the
Step over (F10)
subroutine
20
Breakpoints
Right-click on the line of interest,
Select “Insert Breakpoint”
Breakpoint is set
21
Placing functions
in separate files
The “clean” way (easier to debug)
Put functions in a separate “.c” file in your project folder
Put function prototypes in a separate “.h” file
Function header w/o the body
E.g. void delay(int imax);
Add the “.c” file to your project files list
Put #include statements
In source code files that use your function
In the file where your function is completely written
22
Placing functions
in separate files …