L4 Math Operations

L4 Math Operations - Math ECET 209 – Lecture 4...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Math ECET 209 – Lecture 4 Introduction to Microcontrollers C Operators • How Math gets done!! • Several types: – – – – – Relational Arithmetic Assignment Logical Bitwise ECET 209 Purdue University 2 Assignment Operator • Mechanism to place values into variables or registers unsigned char old_value = 10; ECET 209 Purdue University 3 Arithmetic Operators • Allow basic mathematical operations – – – – Addition Subtraction Multiplication Division ECET 209 Purdue University 4 Additional Arithmetic Operators • Modulus • Increment • Decrement ECET 209 Purdue University 5 Basic Math 250 + 10 = 260 Right? 5 / 2 = 2.5 0 – 1 = -1 ECET 209 WRONG!! Purdue University 6 250 + 10 = 4 5/2=2 0 – 1 = 255 How can this be ???? ECET 209 Purdue University 7 Answer lies in the Architecture & Variables • 8 Bit Processor • Non-Floating Point • Signed vs. Unsigned ECET 209 Purdue University 8 ECET 209 Purdue University 1 2 4 8 16 32 64 128 8 Bits 9 ECET 209 Purdue University 1 2 4 8 16 32 64 128 8 Bits 10 128 ECET 209 32 16 8 4 2 1 1 1 64 8 Bits 1 1 1 1 0 1 0 250 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 + 10 260 Purdue University 11 128 ECET 209 32 16 8 4 2 1 1 1 64 8 Bits 1 1 1 1 0 1 0 250 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 + 10 260 Purdue University 12 64 32 16 8 4 2 1 1 1 128 256 8 Bits 1 1 1 1 0 1 0 250 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 + 10 260 4 ECET 209 Purdue University 13 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 65536 16 Bits 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 250 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 + 10 260 ECET 209 Purdue University 14 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 65536 16 Bits 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 250 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 + 10 260 ECET 209 Purdue University YEP!! 15 Next Example 5 / 2 = 2 not 2.5 Integer Math!! The answer gets truncated ( there is no decimal ). ECET 209 Purdue University 16 However • We can use decimals, they just don’t appear in the answer. • For example 2 * 2.5 = 5 ECET 209 Purdue University 17 Signed vs. Unsigned • Remember, we said that 0 – 1 = 255 not -1 ECET 209 Purdue University 18 Representing Signed Numbers • The 2’s complement system is used to represent signed numbers • Perform subtraction by actually performing addition • Use same hardware to both add and subtract • Sign bit is placed in front of the MSB – 0 for positive, 1 for negative ECET 209 Purdue University 19 ECET 209 Purdue University 1 2 4 8 16 32 64 Sign Bit Signed vs. Unsigned 20 Sign Bit 64 32 16 8 4 2 1 Signed vs. Unsigned 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 -1 -1 C will gladly let you refer to this as an unsigned character type variable, thus the answer becomes 255 not –1. ECET 209 Purdue University 21 Variables • Used to store information • Allow the programmer to refer to information through meaningful names • Must be declared before they can be used ECET 209 Purdue University 22 Variables ECET 209 Purdue University 23 Atmel AVR Overview • 8-Bit RISC Microcontroller • Non-Floating Point Processor ECET 209 Purdue University 24 House Rules for Variables • Use unsigned char as default – unless size or function dictates otherwise – avoid floating point unless absolutely required * – We must keep track of intermediate values ECET 209 Purdue University 25 Variable Declaration • To declare a variable – Provide the data type – Provide the variable a name For Example: unsigned char number_of_dogs; ECET 209 Purdue University // … 26 Variable Naming • Variable names are critical in code readability – C is Case sensitive – Lower case letters are generally preferred – Start with a letter • Can contain numbers & underscores • Be descriptive number_of_dogs ECET 209 Purdue University 27 Variable Naming (cont) • Declare at the beginning of a code block • Before any executable statement main () { unsigned char number_of_dogs; DDRC = 0xFF; } ECET 209 Purdue University 28 Multiple Declarations • Multiple variables of the same type can be declared on the same line unsigned char old_value, new_value; ECET 209 Purdue University 29 Variable Initialization • Variables can be initialized when declared unsigned char last_count = 25; ECET 209 Purdue University 30 Initializing Multiple Variables • Avoid declaring and initializing multiple variables on the same line unsigned char old_value = 10, new_value = 15; (instead) unsigned char old_value = 10; unsigned char new_value = 15; ECET 209 Purdue University 31 Scope of Variables • Global Variables vs. Local Variables – Save for our upcoming discussion on functions ECET 209 Purdue University 32 Intermediate Values • Be aware of the intermediate values!! • For instance – Convert degrees F to C C = 5 / 9 * ( F – 32 ) ECET 209 Purdue University 33 Intermediate Values C = 5 / 9 * ( F – 32 ) The intermediate value of 5/9 will be 0 and thus, the answer will be 0. Why? Integer Math ECET 209 Purdue University 34 Intermediate Values C = 5 / 9 * ( F – 32 ) The intermediate value of 5/9 will be 0 and thus, the answer will be 0. Why? C = 0 * ( F – 32 ) Anything multiplied by 0 is 0. ECET 209 Purdue University 35 Intermediate Values • To overcome this problem – Could rearrange the equation C = 5 * ( F –32 ) / 9 900 decimal => 0x384 could become 900 ECET 209 0x84 14 0x84 => 132 decimal 132 / 9 = 14.667 Purdue University 36 Intermediate Values • To overcome this problem – Could rearrange the equation C = 5 * ( F –32 ) / 9 – Force Floating Point Math C = 5.0 / 9 * ( F – 32 ) ECET 209 Purdue University 37 Modulus • To get the modulus, perform long division and take the remainder ECET 209 Purdue University 38 Increment / Decrement • Only “short cut” that I use • Just like adding or subtracting one i++; // same as i = i + 1 j--; // same as j = j - 1 ECET 209 Purdue University 39 Using printf() For example unsigned char degree_C; unsigned int degree_F; degree_F = PINA; degree_C = 5 * (degree_F – 32) / 9; printf(“The temp in C is %d”, degree_C); ECET 209 Purdue University 40 Digital Logic Review A 0 0 1 1 & ECET 209 A0 B0 Y=A&B 0 Purdue University B 0 1 0 1 Y=A*B 0 0 0 1 0 1 0 1 0 0 1 1 1 41 Digital Logic Review | ~ ^ ECET 209 Purdue University 42 Digital Logic Review ECET 209 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 1 Purdue University << 1 0 >> 1 43 Order of Precedence Name: Operator(s): Grouping: Primary () . -> Left to Right Unary ! ~ - (type) * & ++ -- sizeof Right to Left Binary */% Left to Right Arithmetic +- Left to Right Shift << >> Left to Right Relational < <= > >= Left to Right Equality == != Left to Right Bitwise & Left to Right Bitwise ^ Left to Right Bitwise | Left to Right Logical && Left to Right Logical || Left to Right Conditional ?: Right to Left Assignment = += -= /= *= %= <<= >>= &= ^= |= Right to Left Source: Barnett, Cox, and O'Cull Embedded C Programming and the Atmel AVR ECET 209 Purdue University 44 Enough for Math Today • We’ll revisit this again and again – Subject of Lab 4 – Future HW / Simulation Assignment ECET 209 Purdue University 45 ...
View Full Document

This note was uploaded on 02/22/2012 for the course ECET 209 taught by Professor Richardson during the Fall '08 term at Purdue.

Ask a homework question - tutors are online