ee 109 unit 4
play

EE 109 Unit 4 Microcontrollers (Arduino) Overview Using software to - PowerPoint PPT Presentation

4.1 4.2 EE 109 Unit 4 Microcontrollers (Arduino) Overview Using software to perform logic on individual (or groups) of bits BIT FIDDLING 4.3 4.4 Numbers in Other Bases in C/C++ Modifying Individual Bits Suppose we want to change only


  1. 4.1 4.2 EE 109 Unit 4 Microcontrollers (Arduino) Overview Using software to perform logic on individual (or groups) of bits BIT FIDDLING 4.3 4.4 Numbers in Other Bases in C/C++ Modifying Individual Bits • Suppose we want to change only a single bit (or a few bits) • Suppose we want to place the binary value 00111010 into a in a variable [i.e. char v; ] without changing the other bits ? ? ? ? ? ? ? ? char variable, v [i.e. char v; ] Original v – Set the LSB of v to 1 w/o affecting other bits – We could convert to decimal on our own (58 10 ) • Would this work? v = 1; v = 58; – Set the upper 4 bits of v to 1111 w/o affecting other bits ? ? ? ? ? ? ? 1 • Would this work? v = 0xf0; – All compilers support hexadecimal using the _____ prefix Desired v – Clear the lower 2 bits of v to 00 w/o affecting other bits v = 0x3a; (change LSB to 1) • Would this work? v = 0; – Our Arduino compiler supports binary using the _____ prefix – ____!!! Assignment changes _________ bits in a variable v = 0b00111010; 1 1 1 1 ? ? ? ? • Because the smallest unit of data in C is a byte, Desired v • Important note: Compilers convert EVERYTHING to equivalent manipulating individual bits requires us to use BITWISE (change upper 4 bits to ________. The 3 alternatives above are equivalent because the LOGICAL OPERATIONS. 1111) compiler will take all 3 and place 00111010 in memory. – Use ______ operations to clear individual bits to 0 – Use ______ operations to set individual bits to 1 – Use whichever base makes the most sense in any given situation ? ? ? ? ? ? 0 0 – Use XOR operations to invert bits Desired v – It is your (the programmer's) ______________... compiler will end up – Use AND to isolate a bit(s) value from the others in the (change lower 2 bits to converting to binary once it is compiled 00) register

  2. 4.5 4.6 Bitwise Logical Operations Logical Operations • ANDs can be used to control whether a bit passes changed or a '0' is • Logic operations on numbers means performing the produced (i.e. AND's can force a bit to _____) operation on each pair of bits • ORs can be used to control whether a bit passes unchanged or a '1' is produced (i.e. OR's can force a bit to ______) • XORs can be used to control whether a bit passes unchanged or is 0xF0 1111 0000 inverted/flipped AND 0x3C AND 0011 1100 X Bit Bit Bit � XOR F Z OR Ctrl AND � Y Ctrl Ctrl � 0xF0 1111 0000 OR 0x3C OR 0011 1100 Ctrl Bit F Ctrl Bit F Ctrl Bit F Force Pass Pass '0' 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 1 0xF0 1111 0000 Invert 1 0 1 1 0 0 Pass 1 0 1 Force XOR 0x3C XOR 0011 1100 '1' 1 1 0 1 1 1 1 1 1 0 AND x = __ 0 OR x = __ 0 XOR x = __ 1 AND x = __ 1 OR x = __ 1 XOR x = _____ x AND x = __ x OR x = __ x XOR x = __ 4.7 4.8 Logical Operations Logical Operations Bit # • Bitwise logic operations are often used 7 6 5 4 3 2 1 0 • The C language has two types of logic operations for "bit fiddling" v ? ? ? ? ? ? ? ? – Logical and Bitwise – Change the value of a bit in a register w/o & _________________ • Logical Operators (&&, ||, !) affecting other bits v – Operate on the logical value of a FULL variable (char, int, etc.) – C operators: & = AND, | = OR, ? ? ? ? ? ? ? 0 interpreting that value as either True (non-zero) or False (zero) ^ = XOR, ~ = NOT char x = 1, y = 2, z; • Examples (Assume an 8-bit variable, v) v ? ? ? ? ? ? ? ? 0000 0001 z = x && y; && 0000 0010 – Clear the LSB to '0' w/o affecting other bits – Result is z = ______; Why? | _________________ • Bitwise Logical Operators (&, |, ^, ~) • v = v & 0xfe; or equivalently v 1 ? ? ? ? ? ? ? • v = v & ~(0x01); – Operate on the logical value of INDIVIDUAL bits in a variable char x = 1, y = 2, z; – Set the MSB to '1' w/o affecting other bits v ? ? ? ? ? ? ? ? z = x & y; 0000 0001 • v = v | 0x80; & 0000 0010 – Result is z = ____; Why? ^ – Flip the LS 4-bits w/o affecting other bits 0 0 0 0 1 1 1 1 • v = v ^ 0x0f; v ? ? ? ? ? ? ? ?

  3. 4.9 4.10 Changing Register Bits Checking Register Bits 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 v v ? ? ? ? ? ? ? ? • To check for a given set of bits we use a ? ? ? ? ? ? ? ? • Bitwise logic operations can be used to | bitwise-AND to isolate just those bits & _________________ _________________ change the values of individual bits in – The result will then have 0's in the bit locations not registers without affecting the other of interest bits in the register. v – The result will keep the bit values of interest v ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? • Examples – Set bit 0 of v to a ‘1’ & _________________ & _________________ – Check if bit 7 of v = '1' v = v | ________; if (v & 0x80 == 0x80) { code } or – Clear the 4 upper bits in v to ‘0’s v v if (v & 0x80) { code } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? v = v & ________; – Check if bit 2 of v = '0' ^ _________________ & _________________ – Flip bits 4 and 5 in v if (v & 0x04 == 0x00) { code } or v = v ^ ______________; if ( ! (v & 0x04) ) { code } – Check if bit 2:0 of v = "101" v ? ? ? ? ? ? ? ? if ( (v & 0b00000111) == 0b00000101) { code } & _________________ – Check if bit 5-4 of v = "01" if ( (v & 0x30) == 0x10) { code } 4.11 4.12 Short Notation for Operations • In C, assignment statements of the form – x = x op y; • Can be shortened to – x op= y; • Example: – x = x + 1; can be written as x += 1; • The preceding operations can be written as ARDUINO BOARD INTRO – v|= 0x01; – v &= 0x0f; – v ^= 0b00110000;

  4. 4.13 4.14 Arduino Uno Arduino Uno • Arduino • The Arduino Uno is a Printed circuit (PC) board with processor and other circuits for – An Italian company microcomputer development programming the system and interfacing other devices – They make numerous boards with different processors board based on the Atmel – Hardware and software are open source. ATmega328P 8-bit processor. – Very popular with hobbyists, due in a large part to their low cost. • Most microcomputer manufacturers (Atmel, Freescale, etc.) produce small PC boards with their chips on them for engineers to experiment with and http://arduino.cc/en/Main/ArduinoBoardUno hopefully generate sales of Atmega328P 8-bit the product. processor http://arduino.cc/en/Main/Products 4.15 4.16 Arduino Uno Arduino Uno • What’s on an Arduino Uno board? • Arduino Unos can be stacked with "shield" boards to add additional capabilities (Ethernet, Connectors for I/O lines D0 – D13 wireless, D/A, LCDs, sensors, motor control, etc.) Reset button USB interface Atmel ATmega328P 16MHz oscillator microcontroller (i.e. clock signal generator) Power connector (can also be I/O lines A0 – A5 powered if Power and connected to USB) ground pins

  5. 4.17 4.18 Flashback to Week 1 • Recall the computer interacts with any input or output (I/O) device by simply doing reads/writes to the memory locations (often called registers) in the I/O interfaces… • The Arduino has many of these I/O interfaces all connected via the data bus Processor Memory 0 … 3FF A D C Video ARDUINO PORTS AND PINS Interface 800 FE may FE 800 FE signify a WRITE … white dot at 01 a particular location Keyboard Interface 400 61 Data Bus connecting all components 4.19 4.20 Atmel ATmega328P Where Does It All Go Code Data The program you write #include <avr/io.h> and compile on your • The Arduino Uno is int main() laptop is downloaded { based on an Atmel into the microcontroller while(true){ if(PortB[7] == 1) on the UNO board PortB[5] = 1; Mem. ATmega328P 8-bit else The code resides in the PortB[5] = 0; microcontroller FLASH memory while } Processor the CPU fetches one return 0; instruction at a time } – 32kb of FLASH ROM and executes it. Data sits in the RAM – ______ bytes of RAM (SRAM). – ___ I/O lines Your program controls external inputs and – 3 timer/counters outputs primarily through PORTs B, C, PORTs – Serial/SPI/I 2 C interfaces and D which effectively control the values of – A/D converter the I/O pins. Data Bus I/O Pins

Recommend


More recommend