lect2_notes

lect2_notes - Foundations of Embedded Systems A Term Spring...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Foundations of Embedded Systems A Term Spring 2008 Lecture #2: Introduction to Programming in C for Embedded Systems Reading for Today: Your C—reference, on—line C programming links Reading for Next Class: Notes, on-line links (see Useful Links) Homework #1 is on Web: Due 9/5/2008 (in class!) Last class: Course organization and policies (including Academic Honesty) —— An introduction to embedded systems (they're EVERYWHERE!) This Class: A brief overview of C language programming Rule #1: COMPUTERS ALWAYS D0 EXACTLY WHAT YOU TELL THEM TO DO! A simple C program... What does it do? Is it correct? Is it useful? #include <stdlib.h> /* include the C standard library */ void main() { float degF, degC, degK; degF = 45.7; degC = 5 * (degF—32)/9; degK = degC + 273.15; } Converié F0 40 C0 CLAA keivih :>“[\nus Cole Ls SgnJrorcfir‘xcafluj Correct 50+ \5 “0+ vans 052Cul -—> OAij marks ~Cor “hare; C0494" UCbiU'C deg F: £1577 —-> “Doesq— use FeSuH inanflwcug Basic Types of Programming Instructions — 1) Variable declarations — Defining what your data is int numl, sum, myArray[lO]; I - - . char initial, name[l6]; L'qphql : ‘8 Y\0\W\€EC’35U"+‘£ float myReal, flt_arr[5]; mRSReGJ : ‘7], (pOZLI. .mtawn-LSHF'LQ . + 0 30560“ M mamfl [31 : ‘4’ (Army tdexes darts 5L B 2) Assignment statements — Most are straight forward and “calculator like” 7 int a, b, a: Leg: Q:30 b = lS' Claw/moo; float d, e, f; char bytel, byte2; We“ 0 = a + b; C : qg : QJNS S e = C * e chl\ Dse f—‘\ (“AKIQJAS f ='>Cos(e), 4‘: byte2 = bytel & 15; 1“ Ema”) 3% oomomo inf—“w oooo cot O \o z. 2 3) Decision and Control tempOK = 1; degF = get_degF( ); while (tempOK > 0) { degF = degF — 10; degC = 5 * (degF-32)/9; degK = degC + 273.15; if (degK < O) { printf(“ERROR — Temp < O K\n"); printf(“Breaking Multiple Laws of Physics!\n"); tempOK = 0; } else printf(“%10.4f F = %10.4f C = %lO.4f K \n", degF, degC, degK); } . #200? 4O UQ\'\s\‘e OK) 0) General Framework of a C program for MSP430... /* Compiler directives (includes & defines) come first */ // Code you write in this class will need these 2 includes #include "msp430x44x.h" // IAR header for msp430F449 #include <stdlib.h> // standard library /* defines are used to declare constants */ #define MAX_ITER 10 // max number of iterations /* Then function prototypes */ /* Function prototypes should be used but are not strictly required if full function definition appears above main */ int someFunction(char opt, unsigned int scale); /* Global variables are visible everywhere in code */ /* Try to use local variables as much as possible */ int exitFlag = 0; const float pi = 3.14156; /* Function implementations */ int someFunction(char opt, unsigned int scale) { int a, b, ret_val; /* local integer variables */ scale = scale*opt + a; ret_val = scale*b return(ret_val); // returns an integer /* There can be only one main() function */ /* Execution of program always starts in main () */ void main() { :har choice; int test_num, stuff; stuff = someFunction('a',test_num); Basic Instructions and Syntax in more detail After writing a C program you need to compile and link it to create an Executable file The C OMPILER defines the size of the different data types and how they should be stored in the computer's memory Most general purpose compilers MS Dev Studio or gcc)assume default data size of 32 bits but the IAR Kickstart Compiler is designed for the MSP430 >> Here are some data types and sizes (as they are defined in IAR Kickstart) int float char unsigned int long int double a; b; C; // 16 bit signed integer (2's comp) -_3;27498 +0 // 32 bit IEEE floating point K t , stand-K cam/H dfib 14$ 39757 a, é“~7 // 8 bits (unsigned) 4,) usukllta CTkAv‘aC‘iQVS d; // 16 bit unsigned jélteiecf égggf e; // 32 bit signed integer f; // 32 bit FP (selectable to 64) >> First task in programming is creating variables of suitable size and type for the data in your problem! >> Standard Operators -— These you can find in any C reference 3&gl0 ~+ r ea *5 Z \A<é Q 8041*, / Math: all] i ((Cloctg'é/yo) Cn‘f ;\O€&.+ LL Argus; E) Uu + - * / = % (modulo) J U:(¢<+z)*c§/g/t 4L; (1 1 2/10) Z‘ZIO‘ ‘ViAéfi “id 3 '7 J #22147 //tj=‘1 // CLZLi-7 Unary and assignment: ++ —— += -= *= /= (really just short hand) L++ // L: C + I J~~ n;=3-' Dz; // t: a +2 4% *z‘l // 4K: 45“?“ Relational: > >= < <= == != > " -- » cu (5p (jg->53 (Aw/Me (M1,- 2: Zfl/XJ‘ //u3kk)€ U95“) else a = 2 ‘5; L ' l: && AND H OR 2: E UIVALENCE Oglca ‘( ) ( ) (Q . ) Be care a” ( (53310» H > H700)» “0+ +0 use mm. (c s a m X Mwmassn) .- .- Sinfihe +0 compare values .e' , ‘fixese {0T . . ’ & (,L A CoatiBlthse & (AND) |(OR) A(XOR) ~ (NOT) >>(R1ghtsh1ft) v“ Wage << (Left shift) . em _ U“ L 2 C'XOOC‘SQI // L‘: $5hex :. oooooooo nooo one: -\\ J :OK FOFO‘J //.3—'F¢F¢hey’: HM 0000 “U 0000 u?“ \L , : . c C ‘ 1 ‘ V \VU _, ‘OI’VI\ C X J j H JR 1 0006000 Icoo 0000 :. QOhex :3"? 6K. ‘ _ j a ' Z, LC" [AA 1 L )) Z // m 1 0900000 0 OCHD 0000 ‘-— U Hex Brackets: { } Denotes a multi—line block of code -—> MUST match! >> Statements for making decisions or looping... IF ELSE statements: -iaxu‘hevx k k '> mo ' > 1f k; :ko. 100 ) se;¥ k.k :c) auxé SXQJAEL$E Mack else { ~4eujbievd l<i< < =|0€3 kk++; si<;p IF \o‘ocli auxé } CASE statements: =3 choke switch (choice) E'ye¢;)+e C056 L case 1: /* do something */ , A 5 l<\p e'A break; gfig sqjutch case 2: /* do something else */ break; case 4: /* do something else again*/ break; default: /* for all other values */ break } :53) COV\“’LV\UQ .-_ WHMEMWS SQW\=OQ _ ___ _ eflé‘p€: (O‘ i = strt; // initialize loop counter . J while (i < end_pt) 6’: 54wr+u { . . /* Body of loop */ vhfle (i4 dné,p+) SUM +1 C‘schciesi‘dj- i++; // increment loop count I } avg: Savw/en4_p+. / , . . . r A e :r I 0‘) FOR loops — Really Just a different syntax for a Simple loop 8 A ‘ P SLJWA 2-CE for (i = strt; i < end t; i++) n . . '. ‘ { “p +OK(L:OJ “ed-th C”) /* Bod of 100 */ \ , y p 50M+ijralesl¢3> } // end for i (Xx/cs: gum/ené,‘pf) >> This for loop accomplishes exactly the same thing as the while loop above The “Forever Loop” -- Never Ends >> Endless loops are not desirable (errors!) in most “general purpose” PC—style programs but are used all the time in the main() of embedded code int main() { /"< Declare and initialize variables */ while (1) { /* update variable values, call functions and do everything my device has to do */ } // end while (1) Ex I: What might a simplistic main loop for my car key thing—a—ma—bob look like?? ——> 3 Buttons = Lock All, Unlock (driver then the rest), Panic Assume buttons are ACTIVE HIGH = Logic 1 when pressed and 0 otherwise (Ll/\CLW 130 l, kum l) {0A Z, buffon 3) ‘l' ‘1 mes Presseé : 0. J l botficufl // i t“ buthwq_l.::—TRU; <2 ” the bottom (:5 l 3 XCLK Ly) \mes "Pw‘fieJLOJ i it, if ( but to” Z} \ L‘s (E‘xw\e$?re‘$5‘€,:;o> ‘3, {f fiLmé‘S Pressd‘,‘ on l0LK~Dp\\/€((\‘ J € \fic DA \ock (lid? (3') [1 [b at£on33 \JEE‘ P \(\5l\ An'l ‘ r‘ 7 r [flat/Q t) 9 ‘5u1,»='h\.«‘/2 “CO V\ C ‘K’KOA S” k CC“ J O n like; k “2/ L ' be“ PM E, t SE U.) ‘1 Q r” ‘e “ ‘ t , i "i 0.44 2:) {,lrv‘cz‘s'PY’tGSéecl “is us-eA 4-0 Keep lra‘k a? hl’w m (a s, . I , ft '_7-’ time—S +he urxlack bu++om L503v6$S<¢ C is a Structured or Modular Programming Language >> A C main( ) calls a series of smaller functions to complete a task ——> These functions usual declared and implemented in the file before main() ,/ l// /;;;j\\\ #define MAX_BUTTONS 4 // only 4 buttons on board a X ‘ l}% -555 int checkButton(char buttonNum); ‘ +Ol, p6 1 39° void lighLED(char ledNum); ¥XH\¢4\CW\ Pro ‘3 S 45‘ void wait awile( ); by“ We — g X , 3 h /* Later in file implement checkButton */ . 4t ; int checkButton(char buttonNum) Lflelf’“eYVla;‘OM { P a++o a /* define local variables */ 0 ChékJ{E “ R int buttonState = 0; COV\C+\V3“ /* Body of function goes here */ /* Return */ return(buttonState); } /* Need to implement other functions too! */ /* Now we get to main() */ void main( ) //////”\\\ { /* define local variables */ W int i, b_stat; char button; while (1) /* loop forever */ { for (i = 0; i < MAX_BUTTONS ; i++) { . button = i + 1; . Cd“ C heckEuHau b_stat = checkButton(button); 54~5f C ‘¥CO if (b_stat == 1) km" A 1ightLED(button); ( wait_awhi1e(); b xlioxw LED (Busfsoclcdeé } \ balk 49K?— \bo'++O\/\ // Do other stuff 6 £ x } 0 name Cohe>becarfl looPW\c‘3 be.ch *0 pol/\Hfi IAR Embedded Workbench has “look and feel” of full SW development environment >> BUT it uses a 16 bit compiler that is specifically for MSP430! >> IAR Kickstart C includes virtually all standard C library functions but MSP430 has only 27 machine instructions! (unlike 80x86 family) -- .110 divide, HW multiplier is a peripheral, complex operations including $9 FLOATING POINT math need to be emulated in software a -— There are sqrt() and sin() functions but they are EXPENSIVE! How?? These. :0»\c‘i\0V\3 +0¥l<€ "L‘a‘mtl’m‘w5 er mace-tqu Znsiruchons 4‘0 \LVHPX‘QVWQH'i’ 1n Lnit’fg =>Tal<e up ?fec\ou5 MQW‘OMW Si (6 V—> L/J'\\ new, MOM lime +0 mewirerelal‘we To Simple oporcdrtoms <+ at X: >>3 *** For efficiency you need to THINK in terms of integer math and simple math and logic operations >> MSP430 not a processor for serious number crunching! —— It takes ingenuity (and knowing some tricks) to work well within the bounds of a small microcontroller Rule #2: Keep it simple! Rule #3: Always Remember Rules 1&2 ...
View Full Document

This note was uploaded on 10/27/2008 for the course ECE 2801 taught by Professor Jarvis during the Fall '08 term at WPI.

Page1 / 10

lect2_notes - Foundations of Embedded Systems A Term Spring...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online