Introduction to Microcontrollers Lecture Notes

lecture notes for microprocessors and microcontrollers and what are registers in microcontrollers and what devices use microcontrollers pdf free
JuliyaMadenta Profile Pic
JuliyaMadenta,Philippines,Researcher
Published Date:15-07-2017
Your Website URL(Optional)
Comment
CprE 211 – Introduction to Microcontrollers Lecture Notes © Aaron Striegel & Diane Rover Iowa State University 2001-2002 1 Introduction 1.1 Cpr E 210 Review Basic logic – AND, OR, NOT, XOR, NAND, NOR OR NOT NOR AND NAND XOR Combinatorial logic – logic combines to give a result Logic can be simplified by either algebraic reduction or other techniques 1.1.1 Karnaugh Maps (K-Maps) Technique to simplify logic Map output onto grid based on input Left, right, up, down, change by only one input One K-Map per output equation desired Produce Sum of Products (SOP) by circling ones To produce a SOP: Circle each 1 at least once For each circle that circles multiple 1’s, a term is dropped Must be circled in powers of 2 (1, 2, 4, 8) Cannot circle diagonally CprE 211 Lecture Notes - 6 - 2001-2002 1.1.1.1 Example 3 input (A,B,C) yields a single output (X) A B C X 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 1 K-Map BC 00 01 11 10 A 0 0 1 1 0 1 0 1 0 1 Circle all 1’s at least once BC 00 01 11 10 A 0 0 1 1 0 1 0 1 0 1 A=0 Row C changes between 0 and 1 Drop C and take the other two inputs st 1 term = A B BC 00 01 11 10 A 0 0 1 1 0 1 0 1 0 1 B=1, C=1 column A changes between 0 and 1 Drop A and take the other two inputs nd 2 term = BC CprE 211 Lecture Notes - 7 - 2001-2002 BC 00 01 11 10 A 0 0 1 1 0 1 0 1 0 1 A=1,B=0, C=0 term Only one to circle, no simplification Take term literally rd 3 term = ABC Result X = AB + BC + ABC Required logic 2 2 input AND gates 1 3 input AND gate 1 3 input OR gate 1.1.2 FSM – Finite State Machine • Used to create logic circuits with memory (i.e. they remember what happened previously) • Uses components called flip-flops • Flip-flop holds a value and only changes on a given clock signal (rising edge, falling edge) Q is the value of the flip-flop Q+ = Next value of Q Typically most flip-flops have a Q and Q output Flip-flop examples: D, JK D Flip-Flop Q+ (Next State) = D where D is the input to the flip-flop J-K Flip Flop J is equivalent to the S (Set), when J=1, K=0, Q+ always = 1 K is equivalent to the R (Reset) when J=0, K=1, Q+ always = 0 When J=1, K=1, equivalent to a toggle, Q+ = Q When J=0, K=0, Q stays the same CprE 211 Lecture Notes - 8 - 2001-2002 1.1.2.1 Example Design an FSM to output a 1 when the sequence of 1 0 is seen across the input, hover in the final state Draw the state diagram 1 0 S0 S1 S2 0,1 (00) (01) (10) 0 1 3 states - 2N states can be captured by N flip-flops 3 states therefore we need 2 flip-flops, 2N = 4 which is greater than the 3 required states Draw a truth table 3 inputs – 2 from the flip-flops (current state) (Q , Q ) 0 1 1 input (1 0 sequence) (I) Next state is a combination of the current state and input via combinatorial logic Q Q I Q+ Q+ X 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 0 X X X 1 1 1 X X X X is a don’t care, i.e. it does not matter if it is a 0 or a 1 Why do we have X’s in this truth table? Notice that we do not use State 11, so we don’t care what happens in that situation CprE 211 Lecture Notes - 9 - 2001-2002 State 0 (S0) See a 0 (I=0) State diagram tells us to stay in state 00 Q0+ = 0, Q1+ = 0, X = 0 State 0 (S0) See a 1 (I=1) State diagram transitions to state 01 Q0+ = 0, Q1+ = 1, X = 0 3 outputs – 3 K-Maps K-Map for Q0+ Q Q 0 1 00 01 11 10 I 0 1 X 1 0 0 0 X 1 1 Q + = Q + Q Q 0 1 0 1 K-Map for Q + 1 Q Q 0 1 00 01 11 10 I 0 0 X 0 0 1 1 X 0 1 Q + = Q I 1 0 CprE 211 Lecture Notes - 10 - 2001-2002 K-Map for X Q Q 0 1 00 01 11 10 I 0 0 X 1 0 0 0 X 1 1 X = Q 0 Draw logic diagram for FSM with flip-flops and logic 1.1.3 TTL Review • Transistor-to-Transistor Logic • Operates at +5 V (Digital 1), 0 V (Digital 0) • Further details in EE 333 • Based on the bipolar transistor • First developed by Texas Instruments in 1965 • Simple logic, AND, OR to counters to buffers SN74LS69 SN Series Number 74 L Low Power S Schottky Part number 69 1.1.3.1 TTL Datasheet V Source voltage cc Gnd Ground for transistors in chip 1.1.3.2 Sourcing vs. sinking Sourcing – Current is provided through the chip via the V of the chip cc Sinking – Current is sunk by the chip by driving its output to ground TTL is better at sinking rather than sourcing 1.2 Introduction to Microcontrollers Recall the parts of a computer: CPU, memory, I/O CprE 211 Lecture Notes - 11 - 2001-2002 • Microprocessor - A single chip that contains the CPU or most of the computer • Microcontroller - A single chip used to control other devices Examples: Microprocessor - Pentium, PowerPC chip in your computer Microcontroller - 68HC11, 68332, MPC555 A microcontroller is essentially a microprocessor with several other features embedded onto a single chip Examples of things that use microcontrollers Automobiles, Automatic Cameras, CD player, etc. Why use a microcontroller? Reduce chip count Many applications do not require as much computing power Reduced power consumption Reduced design cost In fact, industry sells 10 times as many microcontrollers as microprocessors What are the parts of a microcontroller? CPU Memory I/O (Input/Output) • CPU Central Processing Unit “Smart part” of the computer that processes data and makes decisions Has all the parts of a normal microprocessor • Memory RAM – Random Access Memory – Storing data while microcontroller is running ROM – Read Only Memory – Store bootup data information EEPROM or EPROM – Persistent storage of data parameters that can be rewritten Example: Alarm clock saving the time when the power goes off • I/O Methods to interact with the world outside the microcontroller A typical CPU takes up only a small portion of the actual silicon real estate of a microcontroller leaving additional space for other features. Examples: CprE 211 Lecture Notes - 12 - 2001-2002 A/D – Analog to Digital Converter Temperature Sensor Display controller Timing circuits Communication circuits Parallel, Serial, Ethernet 1.2.1 Embedded Programming Key Points in Embedded Programming Code Speed - Timing constraints, limited proc. power Code Size - Limited memory, power, physical space Programming Methods Machine Code Low level language - Assembly High level language - C, C++, Java Application level language - Visual Basic, scripts, Access Why use C in embedded programming? Fairly efficient Provides an additional level above assembly programming Supports access to I/O Ease of management of large embedded projects Why use assembly? High speed, low code size However, difficult to do a large project in assembly 1.2.1.1 Real-time software • Software that must read input changes when they occur and change outputs predictably, within a certain time limit • Real-time software programming is typically driven by interrupts • Not necessarily fast code – simply has to meet time constraints • Three classes of real-time systems o Hard real-time = Failure results in a catastrophe, loss of human life Nuclear reactor, airplane o Soft real-time = Failure results in loss of data, loss of value Airline reservation system, stock market o Best-effort = No penalty for missing time Email, web surfing 1.2.2 Binary Review • Smallest unit is a bit CprE 211 Lecture Notes - 13 - 2001-2002 Base 2 notation - Two values 1 (TRUE) 0 (FALSE) • Nibble 4 bits – 16 possible values Ratio of 1 Nibble to 1 Hexadecimal character • Byte, Word, Double Word 8 bits, 16 bits, 32 bits Three most common forms of notation Decimal (base 10) 0,1,2,...,9 Hexadecimal (base 16) 0,1,2,...,9, A,B,C,D,E,F Binary (base 2) 0,1 Another form is octal (base 8) Converting between forms Binary to Hexadecimal Easy, each 4 bits is a hexadecimal character 11000100 1100 0100 0 x C 8 Key Point : Remember, a notation is just a way of representing a specific quantity. A number is not in hex or in decimal or in binary form. Hex, decimal, and binary are just ways of representing a specific quantity. It is up to you to decide how to deal with the variable and how the information is represented. 1.2.2.1 Example Passed in the value 50 in an 8 bit quantity Binary = 00110010 Hex = 0x32 Decimal = 50 Could be the actual number 50 x = x + 50 Could be various bits of information If bit 6 is set, do this Could be a combination If bit 6 is set, x = x + lower nibble of the value CprE 211 Lecture Notes - 14 - 2001-2002 2 C Programming Course Prereqs: Com Sci 207/227 Get the recommended book on C as a brush-up ANSI C – Standard for C compilers across the world 2.1 Variable Names & Types • Can have long variable names X in FORTRAN vs. Area, Graph2, InFile, etc. • No punctuation marks besides underscore • Must start with a letter • Case Sensitive MyVariable is not the same as myvariable • Use long variable names H vs. nHeight Fi vs. InputFile A vs. fArea • Can you remember what a variable was used for 1 year from now, 6 months from now? • For looping variables, use common looping names j, k • Spend a little more time now = savings later on when debugging • Use a naming convention to help quickly identify variables (cover later) Name Bytes Range Char 1 -128 to 127 unsigned 1 0 to 255 char Short 2 -32,768 to 32,767 Int varies may be same as short Long 4 Float 4 7 significant digits Double 8 15 significant digits (pointer) width of Range of memory memory • Floating point = IEEE 754 standard • used infrequently • very expensive versus integer operations • Ex: 68HC11 floating point op - 160 cycles vs. integer 3 cycles • char is one of the most used types in embedded programming • Single byte of memory CprE 211 Lecture Notes - 15 - 2001-2002 • Don’t think of it as an actual character, think of it as the 8 bits required to represent a character 2.1.1 Arrays Sequence of a specific variable type stored in memory Zero-indexed (starts at zero rather than one) Define an array as Type VariableName ArraySize; • Last element is found at N-1 location int nMyIntArray30; nMyIntArray0 / The first element of the array / .. nMyIntArray29 / The last element of the array / nMyIntArray30 / INVALID Beyond the edge of the array / • Be careful of boundaries in C No guard to prevent you from accessing beyond array edge Write beyond array = Disaster • What exactly is an array? Not a specific type Pointer to a block of memory No built-in mechanism for copying arrays 2.1.1.1 Example int nTestArray120; / An array of 20 integers / int nTestArray220; / An array of 20 integers / nTestArray10 = nTestArray20; / This works / nTestArray1 = nTestArray2; / This does not work / 2.1.1.2 Multi-dimensional Arrays Declared the same as normal arrays with an extra set of brackets • Think of it as rowcol • Example: char DblArray 2050; CprE 211 Lecture Notes - 16 - 2001-2002 Declares 20 sets of 50 byte arrays i.e., a 20 row x 50 column array DblArray50 DblArray5 will create a pointer to the 6th row in the array whereas no brackets will create a pointer to the entire block of memory 2.1.1.3 Initializing Arrays • Can initialize an array just like a normal variable • Example: String char szTemp = “Some string”; Values int nTemp = 5,15,20,25; Letters char szTemp = ‘A’,’B’,’C’,’D’; Double Dimensioned char szTemp2 = ‘A’,’B’,’C’,’D’,’E’, ‘U’,’V’,’X’,’Y’,’Z’ ; 2.1.2 Strings • What is a string? Special array of type char that is ended by the NULL (\0) character • Remember to create an array of N+1 characters to allow space for the NULL character 20 character string char szString21; / 20 + 1 / • Why is there a NULL character? Otherwise, how can you tell what is good and bad in a string? More on Variables • What is a variable? Each variable is just a block of memory Block of memory that equates to a certain value Actual value is determined by the programmer Integer, Byte, A few bits, etc. • Example: The ASCII character ‘A’ Actually the numeric value 65 In hex = 0x41 Depending on the debugger, it may appear as ‘A’, 65, or 0x41 • Array Example CprE 211 Lecture Notes - 17 - 2001-2002 The string “CprE211” is represented in memory as ‘C’ ‘ P’ ‘R’ ‘E’ ‘2’ ’1’ ‘1’ ‘\0’ Actual memory contains 0x43 0x50 0x52 0x45 0x32 0x31 0x31 0x00 2.2 Variable manipulation • Standard operators +, -, /, • Specialized operators % Mod • Space-saving operators Combine = with an operator nVal += 10; / Adds 10 to nVal / Increment/decrement operators ++ or Pre-increment and post-increment nVal++; / Adds 1 to nVal / nVal = nTemp++ + 1; / Adds after expression is evaluated / nVal = nTemp 6; / Sub before expression is evaluated / Note: Parens have no effect (nVal++)-4; is the same as nVal++ - 4; 2.2.1 Bitwise Operators Refer back to the Cpr E 210 review. Bit manipulation is a key component of embedded programming. • Why? Space – Instead of using 8 bits to store one value, now we can use individual bits to store information • Operations are done on a bit by bit basis Hence the name bitwise operators/manipulation • Hex notation is the most common form used for bit manipulation 0xFF is 11111111 in binary 0x10 is 00010000 in binary Recall the binary - hex conversion from earlier • AND Operator & – Clear bits and Test bits 0 ANDed with anything will always give a zero 1 ANDed with anything will give the same value CprE 211 Lecture Notes - 18 - 2001-2002 0x10 & 0x10 = 0x10 0x01 & 0x10 = 0x00 0xFF & 0x00 = 0x00 To clear bits: 1. Set the bits you want to clear to zero (Clear) 2. Set all other bits to 1 (Preserve) 2.2.1.1 Example Clear bits 2,3 of an 8 bit number 1 1 1 1 0 0 1 1 0 x F 3 byVal = byVal & 0xF3; • OR Operator (pipe symbol)– Set bits 1 ORed with anything will always give a one 0x10 0x10 = 0x10 0x01 0x10 = 0x11 0xFF 0x00 = 0xFF To set bits: 1. Set the bits you want to make a 1 to 1 (Set) 2. Set all other bits to zero (Preserve) 2.2.1.2 Example Set bits 7,5 of an 8 bit number 1 0 1 0 0 0 0 0 0xA0 byVal = byVal & 0xA0; • Exclusive OR operator - Toggle bits 1 XORed with anything will toggle the bit Not the power sign, to do powers, use the math library 0x10 0x10 = 0x00 0x01 0x10 = 0x10 0xFF 0x00 = 0xFF To toggle bits: 1. Set the bits you want to toggle to 1 (Toggle) 2. Set all other bits to zero (Preserve) CprE 211 Lecture Notes - 19 - 2001-2002 • Inversion operator (0x10) = 0xEF; • Shift operators Used to shift a sequence of bits to the left or right or Syntax Variable/Value Operator of Places nVal = nVal 4; / Shift nVal to the right 4 places / Why use the shift operator? Count the number of ones in a bit Iterate through each bit in a loop Note: The shift operation may be done via an arithmetic shift or by a logical shift Arithmetic – MSb stays the same on a right shift Logical – Always shift in a zero 0x0F 2 = 0x03; 0x0F 2 = 0x3C; 2.2.1.3 Group Exercise Suppose we have the following definition: short nVal; Write the code to do the following: Set bit 13 Clear bit 4 Toggle 15, 14 2.3 Boolean Flow Control Flow Control – Making the program behave in a particular manner depending on the input given to the program Why do we need flow control? Not all program parts are executed all of the time i.e. we want the program to intelligently choose what to do Statements for Boolean flow control if, else if, else Key Point: CprE 211 Lecture Notes - 20 - 2001-2002 The evaluation for Boolean flow control is done on a TRUE / FALSE basis. TRUE / FALSE in the context of a computer is defined as non-zero (TRUE) or zero (FALSE). -1, 5, 15, 225, 325.33 TRUE 0 FALSE 2.3.1 if, else if, else • Must always have if • May/may not have else if or else • Syntax if ( Condition1) … else if (Condition2) … else if (Condition3) … else … • Follows a level hierarchy o else if statements are only evaluated if all previous if and else if conditions have failed for the block o else statements are only executed if all previous conditions have failed • Why is how if statements are evaluated important? o Helps in the design of efficient logic o Know if a condition is evaluated, all previous conditions up to that point have failed - For example, in the above syntax example, the else if (Condition2) will only be executed if Condition1 is false. 2.3.1.1 Example if ( nVal 10) nVal += 5; else if(nVal 5) / If we reach this point, / / nVal must be = than 10 / nVal -= 3; else / If we reach this point, / / nVal must be = than 10 and / nVal = 0; / nVal must be = than 5 / CprE 211 Lecture Notes - 21 - 2001-2002 2.3.2 Comparison (Relational) – Numeric Standard operators = = == Equality = Not Equals • Gives a result of zero (FALSE) or non-zero (TRUE) Key Point: A TRUE result may not necessarily be a 1 • Equality Double equals sign == = Assigns a value == Tests for equality, returns non-zero or zero Consider: if (nVal = = 5) versus if (nVal = 5) The second expression always evaluates to TRUE Why? Comparison – Multiple Conditions • Tie together using Boolean operators && AND OR NOT • Examples: if ( (nVal 0) && (nArea 10)) if( (nVal 3) (nVal 50)) if ( (nVal = 10) ) • Conditions are evaluated using lazy evaluation Lazy evaluation – Once a condition is found that completes the condition, stop Ex. OR any condition is found to be TRUE 1 OR anything = 1 AND any condition is found to be FALSE 0 AND anything = 0 CprE 211 Lecture Notes - 22 - 2001-2002 • Why is lazy evaluation important? Makes code run faster – skips unnecessary code Know condition will/will not evaluate, why evaluate other terms Can use lazy evaluation to guard against unwanted conditions Checking for a NULL pointer before using the pointer 2.4 Expressions & Bitwise Operations Remember, conditions are evaluated on the basis of zero and non-zero The quantity 0x80 is non-zero and therefore TRUE if (3 6) is a valid expression, not very useful but valid 2.4.1 Bit-testing • If we store information in bits, we need to be able to test for bits being a 1 or 0 • Recall use of bit-wise AND to clear bits and test bits • Why does this work? 0 AND anything = 0 Clears bits 1 AND value = same value Tests to see if bits are set, i.e., TRUE If the result is non-zero, that means that at least one of the bits was set since any value ANDed with a 1 is the same value • How do we test? Set the bits we wish to test to a 1 All other bits are set to zero • Test for a single bit 1. Set the bit to a 1 that you wish to test for 2. Do a bitwise AND with the value to be tested 2.4.1.1 Example Find out if bit 7 is set Bit 7 = 0x80 in hex if ( nVal & 0x80 ) … • What happens when we want to test for multiple bits? Remember, an if statement looks only for a non-zero value Bit-wise AND does ops on a bit by bit basis Therefore, a non-zero value means at least one bit is set to TRUE 2.4.1.2 Example See if bits 2 or 3 are true CprE 211 Lecture Notes - 23 - 2001-2002 Bits 2,3 = 0x0C in hex if ( nVal & 0x0C) Some code… Now, let’s take a look at what happens for several values of nVal nVal = 0x04 bit 2 is set Result = 0x04 TRUE nVal = 0x0A bits 3,1 are set Result = 0x08 TRUE nVal = 0x0C bits 2,3 are set Result = 0x0C TRUE Why does this present a problem? What happens if we want to see if both bits 2 and 3 are set, not just to see if one of the bits is set to true? It won’t work without some other type of test Two solutions Test each bit individually if ( nVal & 0x08 && nVal & 0x04) Check the result of the bit-wise AND if ((nVal & 0x0C) == 0x0C) Why do these solutions work? Individual – Easy, individual test Result – The result will only equal 0x0C if bits 2 and 3 are set 2.4.1.3 Group Exercise Consider the following definition: short nValue; Write the if statements to test for the following: Bit 13 is true Bit 7,4, or 0 is true Bits 15 and 0 are true Bits 4 and 2 are false 2.5 Functions Goal – Calculate some value or do some task Subroutines – May/may not return a value Syntax ReturnType FunctionName (Type Parameter1Name, Type Parameter2Name, …) CprE 211 Lecture Notes - 24 - 2001-2002

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.