4.19k likes | 4.42k Views
Embedded System Overview. 1. RockOn! 2008. Why an Embedded System?. General Purpose computer Usually has a human in the loop Can be reconfigured to do any number of tasks (excel, email, music) Embedded Systems (RSW Board) Doesn’t require human input all the time Must meet real-time goals
E N D
Embedded System Overview 1 RockOn! 2008
Why an Embedded System? • General Purpose computer • Usually has a human in the loop • Can be reconfigured to do any number of tasks (excel, email, music) • Embedded Systems (RSW Board) • Doesn’t require human input all the time • Must meet real-time goals • Heart monitor • Automatic braking systems (ABS) • Takes specific inputs and computes outputs for a very specific application 2
Why an Embedded System? • General Purpose computer • Usually has a human in the loop • Can be reconfigured to do any number of tasks (excel, email, music) • Embedded Systems (RSW Board) • Doesn’t require human input all the time • Must meet real-time goals • Heart monitor • Automatic braking systems (ABS) • Takes specific inputs and computes outputs for a very specific application 3
Signal Types - Analog • - Continuous function • - Measures real world value and represents it as a time varying voltage • voice, sun brightness and temperature trends • Can’t store Analog signal. Storage has to be represented as “0” and “1”s on a computer system 4
Signal Types - Digital • Non-continuous, discreet and quantized steps • 1V, 2V, 3V, 4V….90V • Binary information • Individual bits, button push, “there or not there” • Only method for storage of information with a computer system • Serial cables is an example of digital communication 5
Accuracy v. Precision • Accuracy • How close you are to the true value of the object being measured • How often do you hit the bull’s eye? • Capable of accurately measuring the earth’s gravity every time • Precision • The smaller the division, the smaller change which can be observed. The ruler. • Capable of sensing the change of .001g’s High A Low P High P Low A 6
Precision and Recording Data • A state is one unique combination of bits • 1 bit – 0 or 1 = 2 states = 21 • 2 bits – 00, 01, 10, 11 = 4 states = 22 • 4 bits – 0000, 0001….1111 = 16 States = 24 • 8bits = 28= 256 states • 16bits = 216 = 65,536 states • More bits provides more precision over a given voltage range • If it is necessary to record small changes, more precision (bits), is required • 8 bits is a byte 7
Sensor & Storage 50C = 5V • Item to be measured • Real world units • Degrees Celsius • Temperature Sensor • Converts temp to Analog Voltage • 42.0 C to 4.20V • Analog to Digital Converter • Converts 4.20V to Digital value to be stored • as binary • Input voltage range 0-5V • Output Count range 0-255 (8 bits) • Linearly scaled • 4.20V / 5.0V * 256counts = 215 42.0 C temp 0C = 0V 4.20V 5V = 255 0V = 0 215 counts = 11010111 binary Storage for later use 8
Analog to Digital Converter Analog Signal (volts) Quantization of an Analog Signal into a Digital Signal. Digitally converted signals Black line – 4 bits more info Red line – 2 bits less info Digital Conversion 3 2 1 Quantization of an Analog Signal into a Digital Signal 9
What does this all look like on the AVR board? • ATmega 32 FBD AVR Pinout 10
Interfacing with the real world • Ports A, B, C, D • Pins 0-7 • The ATmega 32 has four Ports which each has 8 pins. • Each pin can be individually configured. • Analog in • Digital in or out • Software sets up these pins, reads sensors, stores data 11
C Programming Review 12 RockOn! 2008
History of C • C is a general purpose, block structured, procedural computer programming language • Created in 1972 by Dennis Ritchie at the Bell Telephone Laboratories • Standardized in the early 1980s • The original C programming language is considered to be the language of choice for embedded systems • C is used for the workshop 13
History of C++ • C++ is a middle level programming language that supports the ability to create classes • C++ was created by Bjarne Stroustrup to be a “better C” • First standardized in 1998 • C++ is a spin on the usage of the ++ syntax in programming, and literally means “C + 1”, which implies a programming language a level above C 14
Variables • Variables are name-declared regions for storage • Unlike real numbers, the values of a variable can change depending on the operations done on it • For example, you can declare the letter ‘a’ to be a variable, and then later equate that letter with some value • Example a=4 • The value of ‘a’ has been equated to the number 4 • This value can change through an operation • Example a=4; • a=a+2; • The value of ‘a’ has been updated to a = 4+2, or 6 15
Variables • All variables must be declared before they are used • General form for variable declaration: • <variable type> <name of variable> • A variable name can be anything that is not already used by the c program • Common variable types are int (integer), char (character), and float • Example int foo; 16
Operators • The values of a variable can change depending on the operation used on the variable. • Basic Operators Example (using int a=4) • + (addition) a+2 = 6 • - (subtraction) a -3 = 1 • * (multiplication) a*2 = 8 • / (division) a/4=1 • Uncommon Operators • ++ (increment) a++; a=5 • -- (decrement) a--; a=3 17
Conditional Control • The ability to control the flow of your code through decision-making • Allows the program to skip or execute a section of code if some stated condition is met. 18
Conditional Control • Conditional statements are created using relational operators • > greater than • < less than • >= greater than or equal to • <= less than or equal to • != not equal to • == equal to 19
If Statements • General form • if (conditional statement) • { • execute all commands inside the brackets • when above conditional statement is true • } • Example • if(5>4) • { printf(“Five is greater than four.”) } 20
Else Statements • Used in order to force the program to execute a different section of code should the original IF statement prove false • General form • else • { execute all commands inside the brackets } 21
If/Else Statements • Example • if(3>4) • { • printf(“Three is greater than four.”) • } • else • { • printf(“Three is less than four.”) • } • In this example, only the content of the else statement executes 22
Else If Statements • Used should more than 2 conditions be required. • ELSE IF statements are placed between the initial IF statement and the final ELSE statement • General form else if(statement inside is true) { execute all commands inside the brackets } 23
If / Else If / Else Statements • Example • int a = 3; if(a>3) { printf(“a is greater than three.”) } else if (a==3) { printf(“a is equal to three.”) } else { printf(“a is less than three.”) } • In this case, only the ELSE IF statement will occur. 24
For Loops • For loops will execute a statement a defined number of times, and stop execution once the condition is declared false • Used to avoid writing the same line of code multiple times • General form for ( variable initialization; condition; variable update ) { code to execute while the condition is true } 25
For Loops • Example for(int x = 0; x < 10; x++) { printf(“Hello World!”) } • The above example will print the phrase 10 times, from x = 0 being the first count, to x=9 being the last count • x++ increments x by 1 after each execution of the loop, because x++ is a post increment 26
While Loops • While loops will execute a statement as long as the condition is met • Used to avoid writing the same line of code multiple times • General form while (condition) { code to execute while the condition is true } 27
While Loops • Example while(1) { printf(“Infinite loop”) } • The above example is an infinite loop that will print “Infinite loop” forever • The “1” statement is equivalent to always TRUE, and so the condition is always met 28
Functions • A function is a block of code that, when called, executes a set of pre-defined commands • Some functions come included in a library or in other reference codes, and some have to be written out from start 29
Functions • A function is a block of code that, when called, executes a set of pre-defined commands • Some functions come included in a library or in other reference codes, and some have to be written out from start 30
Function Advantages • The main advantages of a function are: • Organization; allows a programmer to organize different commands under different function names, thus making a program easier to follow • Code Simplification; the programmer can call the same function repetitively in order to avoid repeating lines • Flexibility; allows other programmers to use a set of commands without needing to recreate all the content within the function 31
Function Format • Functions have a two general forms: a return and non-return • All functions must have a return-type, such as void (used for non-return functions), int, char, etc… • Most functions have parameters or arguments - values of which the pre-defined code will work with; some functions do not • There is no real limit to the number of parameters, but the smaller the amount the better 32
Return Functions • A function with a return command will output a value depending on the inputs, or parameters, given to the function • Example int a = content(int x, int y) • The output of the content function will be set to the variable a • The output depends on the commands in the function content and the value of the parameters x and y – the inputs of the function 33
Non-Return Functions • A function without a return command will simply execute the pre-defined commands within its body without any output given • Example greaterthanone(int x) • The greaterthanone function may compare the value of its parameter, x, with 1, and execute a set of commands if the value of x is actually larger than 1 • It does not generate an output, or return value 34
Creating a Function • Define the return type • Define the parameters and their variable types • In order to return a value, include a return statement at the end of the code • Example return foo • The function will return the stored value of the variable foo • Provide comments describing the function functionality 35
Creating a Function • General form • return-type function_name(arg_type1 arg1, arg_type2 arg2,…,arg_typen argn) • { commands to execute in the function return statement if needed } 36
Creating a Function • Example of a Simple Function: int checkequal(int x, int y) //function name and type { //with 2 parameters if(x == y) { return 1 //if parameters equal } //function returns TRUE else { return 0 //else, functions returns } //false } 37
Calling a Function • Call the Function int a = 5; //define one variable for function int b = 4; //define another variable int valid = checkequal(a,b) //call function and check //if equal values • The variable ‘valid’ will, in this case, hold the value 0, or FALSE, because variable a does not equal variable b 38
#include • The directive #include <file name> tells the C compiler to enter the contents of the specified file in that location • This allows for a complicated program to spread on more than 1 .h or .c file, which allows better organization • #include may be needed to access pre-defined values by the program • Example #include <math.h> • Allows access to the math library, which contains many important math functions and variables 39
#define • Can be used to declare values for constants • Example #define MAXSIZE 256 • The value MAXSIZE always refers to the number 256 • Can be used for argument declarations, which is slightly similar to basic function declaration • Example #define DOUBLE(x) 2*x • Every time DOUBLE() is called with some number in its parenthesis, it will take that number or variable and multiply it by 2 40
C Review Quiz – Question #1 • Which of the following C statements declares an integer variable named my_int and assigns it a value of 42? • integer my_int(42); • int my_int = 42; • my_int = 42; • my_int integer = 42; EXPLANATION: all variables must be declared a type, and all declarations must occur before name assignments. To declare an integer, write ‘int’. 41
C Review Quiz – Question #2 What is the value of output after the loop is executed? int i; int output; output = 1; for (i = 0; i < 30; i = i + 3) { if (i < 15) { output = output * 2; } else { output = output – 2; } } a) 52 b) 42 c) 32 d) 22 EXPLANATION: The output is effectively doubled 5 times, and then subtracted from 5 times. So, (2^5)-10 is the mathematical solution 42
C Review Quiz – Question #3 • The next 2 questions refer to the function foo, defined below: int foo(double a, char b, long c); What is the return type of foo? a) int b) double c) char d) long EXPLANATION: The return type of a function is the type of the function, not the type of its parameters. 43
C Review Quiz – Question #4 Which of the following statements correctly calls foo and assigns the return value to the variable x (ignore the type of x for this question)? a) x = foo(1, 2.2, 5); b) x = foo(1.5, ‘a’, 1000); c) x.foo(1.5, ‘b’, 1000); d) x(foo(1, ‘c’, 0.25)); EXPLANATION: Return values must be written into an equal statement, and the function must have the correct parameter types for its inputs. 44
C Review Quiz – Question #5 What value is contained in x[3] after the following code is executed? int x[10]; for (int i = 0; i < 10; ++i) { x[10-i] = i + 1; } a) 5 b) 6 c) 7 d) 8 EXPLANATION: The statement ++i is a pre increment. 1 is added to the value of i before i is used in the body. In this case, the first value of i used in the body of the loop is actually 1. 45
C Review Quiz – Question #6 What is the value of output after the following code is executed? int output; int a = 10, b = 1, c = 4, d = 7; if ((a < b) || ((b < c) && (d >= b))) { output = 1; } else { output = 0; } a) 0 b) 1 EXPLANATION: And operations (&&) are analyzed first. Thus, d is greater than b and c is greater than b is true. If one condition of an or (||) statement is true, the entire or statement is true. 46
C Review Quiz – Question #7 Which of the following logical expressions evaluates to 1 for the given values? A = 2; B = 0; C = 0; D = 1; E = 3; a) !(D || (E < A)) b) ((D < A) || (C == B) && E) c) ((E || F) && C) d) ((C == D) || ((D < E) && (A < B))) EXPLANATION: D<A in choice (b) evaluates to 1, which makes the entire statement evaluate to 1 because or statements are analyzed after and statements. 47
C Review Quiz – Question #8 Given a function void foo(int x), which of the following correctly calls foo? a) int x = 3; void foo(int x); b) void foo(3); c) foo(-3); d) int x = 3; foo(int x); EXPLANATION: The function only requires an integer declared parameter, or an integer placed inside the parenthesis. To call a function, simply write the name followed by filling the parameter positions. 48
C Review Quiz – Question #9 Find the error in the following code: char test; scanf(test); 1. switch (test) { 2. case a: printf(“Hello!”); break; 3. case ‘b’: printf(“Goodbye!”); break; 4. default: printf(“Input is not a or b”); break; }; EXPLANATION: Test is defined as a character, and thus all values must be defined under quotes. 49
C Review Quiz – Question #10 Which of the following is not a valid variable declaration? a) int a[10]; b) void b; c) long c; d) char d = 0; EXPLANATION: To declare a variable void is not possible. Void is used for non-return functions 50