Net3001 5 C
Net3001 5 C
C Programming
Advantages
● programmers can work at a higher level than
assembly language
– can ignore the low level details
– work at a higher level of abstraction
● code is (mostly) portable
● language provides
– type checking
– control statements other than test-&-jump
– subroutine parameter/temp/return management
● still has low overhead
– can still access pointers
Data Types
void functions that return nothing
char single byte of data
short a small int, 16 bits
int/long a single word, 32 bit integer
unsigned char control the way math operates on larger
unsigned int numbers
boolean a byte
char[] an array of bytes
pointers to be discussed
C keywords
● these words are special
Data int long short char void
Data modifier const volatile static enum
signed unsigned
Control flow do while if else for
Switch switch case default
Disrupt the flow break continue
Extra sizeof
Not used in float double extern inline register
NET3001 auto restrict struct typedef union goto
Subroutines ()
Math + - * / ++ -- %
Logicals & | && || == >= > < <= != ! ~ ^
Line end ;
Comments /* */ //
Preprocessor #
Arrays & pointers [] * &
Strings " '
Initializers {}
Fast assignment += -= *= /= %= &= |= ~= ^=
Either or ? :
Not used ... -> . ::
BEDMAS ()
Logical Operations
● any expression which is non-zero is
considered true
(i<10)
(i>0 && i<10)
(i) same as (i!=0)
● bit-wise operations
–only for char, short, int & long
PCOUT = PCOUT | 0x40; // set bit 6
PCOUT = PCOUT & 0xFFFE; // clear bit
0
PCOUT &= 0xFE; // the same
i = (PBIN >> 2); // shift right
if (!(PAIN & 0x80)) // check bit 7
Operators
● math
+ - * /
% (modulus) ++ --(inc dec)
● bit-wise operations
– returns char, short, int & long
& | ^ << >> (and or xor shift-l shift-r)
~ (invert each bit)
● logical operations
– return true or false/0
&& || == != (and or equal not-equal)
> < >= <= (math tests)
! (logical inversion)
Flow Control
● C has a powerful set of control structures
if (condition) {
doTrueActions();
} else {
doFalseActions();
}
while(condition) {
doActions();
}
– the incr-expression is executed after the loop code has been run
Flow Control
● multiple choice
switch(nChoice) {
case 0:
doFor0();
break;
case 2:
doFor2();
break;
case 7:
doFor7();
break;
default:
doOther();
break;
}
Flow Control
● keywords which affect flow
break
// escape out of the inner most for,while,do or switch
continue
// finish the current for, while or do loop,
//but keep executing
● specialized
– getHttpObject()
setMotorPosition(calcPosition(4,-1));
// you can use a subroutine return value anywhere
PCOUT = calcLedPattern(0x0E,1,1,0,1);
// this does not call the subroutine!
// it just does a look up in the symbol table
a = lcdSetFont;
● when the code generator sees
valid = setFontColor(12);
● it does this
mov r0,#12
bl setFontColor // in the symbol table
ldr r1,=valid // in the symbol table
str r0,[r1]
● when the code generator sees this
valid = setFontColor;
it does this
ldr r0,=setFontColor // in symol table
ldr r1,=valid // in symbol table
str r0,[r1]
Quiz
● what's wrong with this?
unsigned int w;
// find the absolute value
if (w<0)
w = -w;
● and this?
void doubleMe(int r) {
r = 2*r;
}
int x = 14;
doubleMe(x);
Data Areas
int a; // Global in .bss
int b = 1; // Initialized global .data
static int c; // Static External in .bss
// (only for this file)
static int d = 2; // Init’d static in .data
while(1) {
PCOUT = (i & 7); // ldr r1,=i (somewhere in RAM)
// ldr r0,[r1]
// and r0,r0,#7
// ldr r2,=PCOUT (0x4001.100c)
// strh r0,[r2]
i++; // ldr r0,[r1]; add r0,r0,#1
// str r0,[r1]
delay(); // bl delay
} // b pc-22 similar to sub r15,#22
Memory Layout
char b = 4; // goes into .data 0x0800.0000 .vectors
.text
char s; // goes into .bss
const char f = 12; // goes in .text
0x2000.0000
.data
.bss
stack
0x2000.2000
0x4000.0000
i/o
Symbol Table
int aVariable;
void one_sec_delay(void) {
int i;
for (i=0; i<3000000; i++)
;
}
int main(void) {
int r;
PCDDR_HI = 0x11111111;
for (r=0; r<8; r++) {
PCOUT = (r<<8);
delay();
}
}
Symbol Table
Name Value Type Size Section Content
aVariable 0x2000.0000 int 4 .bss 0
one_sec_delay 0x0800.0040 function void(void) 0 .text
one_sec_delay.i stack 0(sp) int 4 stack
main 0x0800.0068 function int(void) 0 .text
main.r stack 0(sp) int 4 stack
int main(void) {
PCDDR_HIGH = 0x11111111;
188: 4b07 ldr r3, [pc, #28] ; (1a8 <test+0x20>)
18a: f04f 3211 mov.w r2, #286331153 ; 0x11111111
18e: b510 push {r4, lr}
190: 605a str r2, [r3, #4]
for (r=0; r<8; r++) {
192: 2400 movs r4, #0 ; use r4 to hold r
PCOUT = (r<<8);
194: 4b04 ldr r3, [pc, #16] ; (1a8 <test+0x20>)
196: 0222 lsls r2, r4, #8
198: 60da str r2, [r3, #12]
19a: 3401 adds r4, #1 ; r++
19c: f7ff fffe bl 0 <delay>
19c: R_ARM_THM_CALL delay
1a0: 2c08 cmp r4, #8 ; if (r<8)
1a2: d1f7 bne.n 194 <test+0xc>
1a4: bd10 pop {r4, pc}
1a6: bf00 nop
1a8: 40011000 .word 0x40011000
Pointers
● a pointer is another type of variable
● can address any type of variable
– byte, int, array, function....anything
● a pointer is always 4 bytes long
char b; // is 1 byte
&b; // the address of 'b'
char* pb; // is four bytes
pb = &b; // write 4 bytes to pb
b = 4; // write to the byte 'b'
*pb = 12; // write to the SAME byte
int c; // is 4 bytes
int* pc = &c; // is 4 bytes, just a coincidence
// dereferencing
aData[2]; // is one byte long ==7
*(aData + 2); // is the same ==7
// assigning a pointer
pData = aData; // 4 bytes are written
*(pData + 5); // is a byte == 6
pData[5]; // is the same == 6
Pointers and Arrays
● arrays names go into the symbol table and take
up n bytes in memory
● pointers go into the symbol table and take up 4
bytes in memory
char aData[12] = {4,5,7,3,4, 6,5,4,3,2, 0,1};
char* pData; // is 4 bytes
// or
pString = szHelloWorld; // put 0x0800.0144 into pString
while (*pString) // use it as a pointer
doSomething(*pString++); // fetch through pString then
// increment it to 0x0800.0144/5/6...
Static
● the static keyword has several meanings
– static on a function
● visible only to this file
– static on a variable outside a function
● visible only to this file
– static on a variable inside a function
● variable exists even when the function exits
● it lives in .bss or .data, has permanent address
MAIN_OUT |= BEEPER_ON_BIT;
delay_ms(2);
MAIN_OUT &= BEEPER_OFF_BIT;
//------------------or------------- I like this one best
#define BEEPER_ON PAOUT |= 0x0800
#define BEEPER_OFF PAOUT &= ~0x0800
BEEPER_ON;
delay_ms(2);
BEEPER_OFF;
Startup Code
● contained in crt1.c
● copy .data from flash to ram
– ram contents are unknown/random at power-up
time
● clear .bss
– immediately after the .data section
● C++ constructors; initialize the library
● jump to main()
while(1) {
do stuff
}
Idioms (cont'd)
● you are allowed to add extra statements to
the 1st and 3rd sections of an if statement
for (count=0, i=0; i<100; i++) {
do stuff;
count += 2;
}
● or even
for (count=0, i=0; i<100; i++, count+=2) {
do stuff;
}
Idioms (cont'd)
● you can use the "break" statement to get out
of one loop, but you need to use a variable to
break out of a deep loop
int done = 0;
#include “motors.h”
#include “motors.h” #include “lcd.h”
#include “lcd.h”
void setDcMotor(int) { void lcdSetTextColor(int) {
main(void) {
…...... //body ….... //body
…...//body
} }
lcdSetTextColor(LCD_WHITE);
a = fColor;
void lcdPutStr(char*, int, int) {
void step(int d) { setDCMotor(-1);
….......//body
…..... // body }
}
}
unsigned char fColor;
add entry
generate
code
to symbol
table .h File
motors.h lcd.h assign31.h
#include “motors.h”
#include “motors.h” #include “lcd.h”
#include “lcd.h”
void setDcMotor(int) { void lcdSetTextColor(int) {
main(void) {
…...... //body ….... //body
…...//body
} }
lcdSetTextColor(LCD_WHITE);
a = fColor;
void lcdPutStr(char*, int, int) {
void step(int d) { setDCMotor(-1);
….......//body
…..... // body }
}
}
unsigned char fColor;
.h File
motors.h lcd.h assign31.h
#include “motors.h”
#include “motors.h” #include “lcd.h”
#include “lcd.h”
void setDcMotor(int) { void lcdSetTextColor(int) {
main(void) {
…...... //body ….... //body
…...//body
} }
lcdSetTextColor(LCD_WHITE);
a = fColor;
void lcdPutStr(char*, int, int) {
void step(int d) { setDCMotor(-1);
….......//body
…..... // body }
}
}
unsigned char fColor;
.h File
motors.h lcd.h assign31.h
#include “motors.h”
#include “motors.h” #include “lcd.h”
#include “lcd.h”
void setDcMotor(int) { void lcdSetTextColor(int) {
main(void) {
…...... //body ….... //body
…...//body
} }
lcdSetTextColor(LCD_WHITE);
a = fColor;
void lcdPutStr(char*, int, int) {
void step(int d) { setDCMotor(-1);
….......//body
…..... // body }
}
}
unsigned char fColor;