EE 109 Unit 4. Microcontrollers (Arduino) Overview

Similar documents
Introduction. Unit 4. Numbers in Other Bases in C/C++ BIT FIDDLING. Microcontrollers (Arduino) Overview Digital I/O

Embedded programming, AVR intro

The EE 109 LCD Shield

12.1. Unit 12. Exceptions & Interrupts

The Atmel ATmega328P Microcontroller

(Embedded) Systems Programming Overview

6.1. EE 109 Unit 6. LCD Interfacing

Embedded Systems and Software

AVR Board Setup General Purpose Digital Output

Robosoft Systems in association with JNCE presents. Swarm Robotics

READING SOURCE MATERIAL

INTERFACING HARDWARE WITH MICROCONTROLLER

Module 2: Introduction to AVR ATmega 32 Architecture

Layman definition: Gadgets and devices Technical definition: Self-controlled devices Usually, such systems consist of I/O (input/output) devices such

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

CN310 Microprocessor Systems Design

Microcontrollers. Outline. Class 1: Serial and Digital I/O. March 7, Quick Tour of the Board. Pins, Ports, and Their Registers

The Atmel ATmega168A Microcontroller

Introduction to Micro-controllers. Anurag Dwivedi

Submit this lab and the extended learning (optional) to TEACH by Monday at 11:59pm. You must get checked off by a TA by the beginning of lab 7.

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Buses and Parallel Input/Output

Objectives. I/O Ports in AVR. Topics. ATmega16/mega32 pinout. AVR pin out The structure of I/O pins I/O programming Bit manipulating 22/09/2017

Embedded Systems and Software

ECE2049 E17 Lecture 4 MSP430 Architecture & Intro to Digital I/O

Software debouncing of buttons


CSCI 2212: Intermediate Programming / C Chapter 15

Lab Course Microcontroller Programming

Programming Microcontroller Assembly and C

Goal: We want to build an autonomous vehicle (robot)

REQUIRED MATERIALS Epiphany-DAQ board Wire Jumpers Switch LED Resistors Breadboard Multimeter (if needed)

Introduction to Embedded Systems

The EE 109 LCD Shield

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

Introduction to Assembly language

IME-100 Interdisciplinary Design and Manufacturing

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

Chapter 9. Input/Output (I/O) Ports and Interfacing. Updated: 3/13/12

ECE 353 Lab 4. General MIDI Explorer. Professor Daniel Holcomb Fall 2015

COMP2121: Microprocessors and Interfacing. I/O Devices (I)

SIGNED AND UNSIGNED SYSTEMS

C and Embedded Systems. So Why Learn Assembly Language? C Compilation. PICC Lite C Compiler. PICC Lite C Optimization Results (Lab #13)

IAS0430 MICROPROCESSOR SYSTEMS

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

Adafruit Metro Mini. Created by lady ada. Last updated on :12:28 PM UTC

ECE2049-E18 Lecture 6 Notes 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

CONTENTS BIGAVR2 KEY FEATURES 4 CONNECTING THE SYSTEM 5 INTRODUCTION 6

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Review on Lecture-1. ICT 6641: Advanced Embedded System. Lecture 2 Branch, Call and Delay Loops, AVR I/O port programming

Lab 2 - Powering the Fubarino. Fubarino,, Intro to Serial, Functions and Variables

Pre-Lab: Part 1 Using The Development Environment. Purpose: Minimum Parts Required: References: Handouts:

2. Tutorial ESC Programming myavr MK2 USB UFO Doctor, June 5 rd, 2010

Introduction to Arduino

Using Arduino Boards in Atmel Studio 7

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #9: Exam Review w/ Solutions

Survey. Motivation 29.5 / 40 class is required

Figure 1: Pushbutton without Pull-up.

16.1. Unit 16. Computer Organization Design of a Simple Processor

CHAPTER 1 - World of microcontrollers

Designing and Building A Cable Tester. Elliot Maude

AGH University of Science and Technology Cracow Department of Electronics

EK307 Lab: Microcontrollers

An FTDI connection: The ATtiny microcontrollers don t have a hardware UART External Crystal header pins for an optional crystal

ELCT708 MicroLab Session #1 Introduction to Embedded Systems and Microcontrollers. Eng. Salma Hesham

Variables and Data Representation

Lab 2 - Powering the Fubarino, Intro to Serial, Functions and Variables

MICROPROCESSORS A (17.383) Fall Lecture Outline

Overview. The C programming model. The C programming model. The C programming model. The C programming model 1/23/2009. Real-time Systems D0003E

ET-BASE AVR ATmega64/128

Project 17 Shift Register 8-Bit Binary Counter

Stand-alone programming AVRs using CircuitPython

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

SH69P48A EVB. Application Notes for SH69P48A EVB SH69V48A JP2 J4(ICE_J4) S1 IDD TEST JP1 74HC273 JP4 JP3 74HC273 JP6 STKOVE JP7 SW1 J5(ICE_J5)

Sanguino TSB. Introduction: Features:

Introduction to the MC9S12 Hardware Subsystems

EE251: Thursday September 20

Arduino 05: Digital I/O. Jeffrey A. Meunier University of Connecticut

538 Lecture Notes Week 1

DEV-1 HamStack Development Board

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

COMP2121: Microprocessors and Interfacing. I/O Devices (II)

ECE2049-E17 Lecture 6 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

Why embedded systems?

Lesson 5 Arduino Prototype Development Platforms. Chapter-8 L05: "Internet of Things ", Raj Kamal, Publs.: McGraw-Hill Education

Thursday, September 15, electronic components

SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

ARDUINO UNO REV3 Code: A000066

Bits and Bytes. Here is a sort of glossary of computer buzzwords you will encounter in computer use:

Microcontroller Overview

Getting Started Guide

Alessandra de Vitis. Arduino

Microprocessors & Interfacing

ARDUINO UNO REV3 SMD Code: A The board everybody gets started with, based on the ATmega328 (SMD).

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

EE 109 Unit 12 Computer Organization

EE 308: Microcontrollers

Embedded Systems. Introduction. The C Language. Introduction. Why C instead ASM. Introduction to C Embedded Programming language

Lab 1 An Introduction to Assembly

Transcription:

1 EE 109 Unit 4 Microcontrollers (Arduino) Overview

2 Using software to perform logic on individual (or groups) of bits BIT FIDDLING

3 Numbers in Other Bases in C/C++ Suppose we want to place the binary value 00111010 into a char variable, v [i.e. char v;] We could convert to decimal on our own (58 10 ) v = 58; All compilers support hexadecimal using the '0x' prefix v = 0x3a; Our Arduino compiler supports binary using the '0b' prefix v = 0b00111010; Important note: Compilers convert EVERYTHING to equivalent binary. The 3 alternatives above are equivalent because the compiler will take all 3 and place 00111010 in memory. Use whichever base makes the most sense in any given situation It is your (the programmer's) choice the compiler will end up converting to binary once it is compiled

4 Modifying Individual Bits Suppose we want to change only a single bit (or a few bits) in a variable [i.e. char v;] without changing the other bits Set the LSB of v to 1 w/o affecting other bits Would this work? v = 1; Set the upper 4 bits of v to 1111 w/o affecting other bits Would this work? v = 0xf0; Clear the lower 2 bits of v to 00 w/o affecting other bits Would this work? v = 0; No!!! Assignment changes ALL bits in a variable Because the smallest unit of data in C is a byte, manipulating individual bits requires us to use BITWISE LOGICAL OPERATIONS. Use AND operations to clear individual bits to 0 Use OR operations to set individual bits to 1 Use XOR operations to invert bits Use AND to isolate a bit(s) value from the others in the register????????? 1? Original v?????? 1 Desired v (change LSB to 1) 1 1 1???? Desired v (change upper 4 bits to 1111)????? 0 0 Desired v (change lower 2 bits to 00)

Pass Force '0' Force '1' Invert Pass Pass 5 Bitwise Logical Operations Bit X Ctrl Y ANDs can be used to control whether a bit passes changed or a '0' is produced (i.e. AND's can force a bit to '0') 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 '1') XORs can be used to control whether a bit passes unchanged or is inverted/flipped AND F Bit X Ctrl Y OR F Bit X Ctrl Y XOR F Z Ctrl Bit F 0 0 0 0 1 0 1 0 0 1 1 1 0 AND x = 0 1 AND x = x x AND x = x Ctrl Bit F 0 0 0 0 1 1 1 0 1 1 1 1 0 OR x = x 1 OR x = 1 x OR x = x Ctrl Bit F 0 0 0 0 1 1 1 0 1 1 1 0 0 XOR x = x 1 XOR x = NOT x x XOR x = 0

6 Logical Operations Logic operations on numbers means performing the operation on each pair of bits 0xF0 AND 0x3C 0x30 1111 0000 AND 0011 1100 0011 0000 0xF0 OR 0x3C 0xFC 1111 0000 OR 0011 1100 1111 1100 0xF0 XOR 0x3C 0xCC 1111 0000 XOR 0011 1100 1100 1100

7 Logical Operations The C language has two types of logic operations Logical and Bitwise Logical Operators (&&,,!) Operate on the logical value of a FULL variable (char, int, etc.) interpreting that value as either True (non-zero) or False (zero) char x = 1, y = 2, z; z = x && y; Result is z = 1; Why? Bitwise Logical Operators (&,, ^, ~) Operate on the logical value of INDIVIDUAL bits in a variable char x = 1, y = 2, z; z = x & y; Result is z = 0; Why?

8 Logical Operations Bitwise logic operations are often used for "bit fiddling" Change the value of a bit in a register w/o affecting other bits C operators: & = AND, = OR, ^ = XOR, ~ = NOT Examples (Assume an 8-bit variable, v) Clear the LSB to '0' w/o affecting other bits v = v & 0xfe; or equivalently v = v & ~(0x01); Set the MSB to '1' w/o affecting other bits v = v 0x80; Flip the LS 4-bits w/o affecting other bits v = v ^ 0x0f; 7 Bit # 6 5 4 3 2 1 0 v???????? & v??????? 0 v???????? v 1??????? v???????? ^ 0 0 0 0 1 1 1 1 v????????

9 Changing Register Bits 7 6 5 4 3 2 1 0 Bitwise logic operations can be used to change the values of individual bits in registers without affecting the other bits in the register. Set bit 0 of v to a 1 v = v 0x01; Clear the 4 upper bits in v to 0 s v = v & 0x0f; Flip bits 4 and 5 in v v = v ^ 0b00110000; v???????? v???????? & v???????? ^

10 Checking Register Bits 7 6 5 4 3 2 1 0 To check for a given set of bits we use a bitwise-and to isolate just those bits The result will then have 0's in the bit locations not of interest The result will keep the bit values of interest Examples Check if bit 7 of v = '1' if (v & 0x80 == 0x80) { code } if (v & 0x80) { code } Check if bit 2 of v = '0' if (v & 0x04 == 0x00) { code } if (! (v & 0x04) ) { code } Check if bit 2:0 of v = "101" if ( (v & 0b00000111) == 0b00000101) { code } Check if bit 5-4 of v = "01" if ( (v & 0x30) == 0x10) { code } or or v???????? & 1 0 0 0 0 0 0 0 v???????? & 0 0 0 0 0 1 0 0 v???????? & 0 0 0 0 0 1 1 1 v???????? & 0 0 1 1 0 0 0 0

11 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 v = 0x01; v &= 0x0f; v ^= 0b00110000;

ARDUINO BOARD INTRO 12

13 Arduino Uno The Arduino Uno is a microcomputer development board based on the Atmel ATmega328P 8-bit processor. Most microcomputer manufacturers (Atmel, Freescale, etc.) produce small PC boards with their chips on them for engineers to experiment with and hopefully generate sales of the product. Printed circuit (PC) board with processor and other circuits for programming the system and interfacing other devices http://arduino.cc/en/main/arduinoboarduno Atmega328P 8-bit processor

14 Arduino Arduino Uno An Italian company They make numerous boards with different processors Hardware and software are open source. Very popular with hobbyists, due in a large part to their low cost. http://arduino.cc/en/main/products

15 Arduino Uno What s on an Arduino Uno board? Reset button Connectors for I/O lines D0 D13 USB interface 16MHz oscillator (i.e. clock signal generator) Atmel ATmega328P microcontroller Power connector (can also be powered if connected to USB) Power and ground pins I/O lines A0 A5

Arduino Uno Arduino Unos can be stacked with "shield" boards to add additional capabilities (Ethernet, wireless, D/A, LCDs, sensors, motor control, etc.) 16

ARDUINO PORTS AND PINS 17

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 Video Interface 800 A D C FE may signify a white dot at a particular location 800 FE 01 FE WRITE Keyboard Interface Data Bus connecting all components 400 61

19 Atmel ATmega328P The Arduino Uno is based on an Atmel ATmega328P 8-bit microcontroller 32kb of FLASH ROM 2048 bytes of RAM 23 I/O lines 3 timer/counters Serial/SPI/I 2 C interfaces Mem. Processor A/D converter Data Bus

20 Where Does It All Go The program you write and compile on your laptop is downloaded into the microcontroller on the UNO board The code resides in the FLASH memory while the CPU fetches one instruction at a time and executes it. Data sits in the RAM (SRAM). #include <avr/io.h> int main() { while(true){ if(portb[7] == 1) PortB[5] = 1; else PortB[5] = 0; } return 0; } Code Data Your program controls external inputs and outputs primarily through PORTs B, C, and D which effectively control the values of the I/O pins. I/O Pins PORTs

21 Digital I/O Example This program Checks if the button is being pressed (i.e. the value on Port B bit 7 is '1'. If so, it sets the value on Port B bit 5 to '1' (which is a high voltage) and connects to an LED to make it light up Otherwise it sets PB5 to '0' (low voltage) and the LED does NOT light up Software code to control the microcontroller #include <avr/io.h> int main() { while(true){ if(portb[7] == 1) PortB[5] = 1; else PortB[5] = 0; } return 0; } Disclaimer: This code & connections are an approximation and should not just be copied. Logic 1 = 5V Logic 0 = 0V An LED A button Pin associated w/ PB7 Pin associated w/ PB5 Main Point: What happens to the hardware (button and LED) is controlled by the software

22 Arduino Digital I/O ATmega328P has 23 pins on the chip that can be connected to other devices (switches, LEDs, motors, etc.) Other members of the ATmega family may have more or less lines. The Arduino Uno can make use of only 20 of these lines. Each pin can be used as a digital input or a digital output For output pins: Your code determines what value ('1' or '0') appears For input pins: Your code senses/reads what value another device is putting on the pin Main Point: Individual pins on the Arduino can be used as inputs OR outputs

23 Arduino Port/Pin Mapping Since computers usually deal with groups of 8-bits (a.k.a. a byte), all of the 20 I/O pins are split into three 8-bit I/O ports (B, C and D) The avr-gcc software (SW) and the Arduino hardware use different names to refer to the bits within each port SW Arduino SW Arduino SW Arduino PortB[0] DIG8 PortC[0] AN0 PortD[0] DIG0 PortB[1] DIG9 PortC[1] AN1 PortD[1] DIG1 PortB[2] DIG10 PortC[2] AN2 PortD[2] DIG2 PortB[3] DIG11 PortC[3] AN3 PortD[3] DIG3 PortB[4] DIG12 PortC[4] AN4 PortD[4] DIG4 PortB[5] DIG13 PortC[5] AN5 PortD[5] DIG5 PortB[6] Clock1 (don't use) PortC[6] Reset (don't use) PortD[6] DIG6 PortB[7] Clock2 (don't use) PortD[7] DIG7 Main Point: Each pin has a name the software uses (Portx) and a name used on the Arduino circuit board (Anx or DIGx)

24 Arduino Digital I/O The I/O ports (i.e. groups of pins) are the middle men between your software program and the physical devices connected to the chip. Your program is responsible for managing these ports (groups of I/O pins) in order to make things happen on the outside Most I/O pins in a port can be directly controlled by your software for "digital I/O" OR be used for other specific HW functionality integrated on chip PORTC0 can be used as a digital I/O OR as the Analog-to-Digital Conversion input: ADC0 PORTD0 can be used as digital I/O OR the serial communication receive input: RXD We will discuss these other HW functions later focus on digital I/O

25 How to connect LEDs and Switches/Pushbuttons INPUT AND OUTPUT DEVICES

26 What Do we Do Now Great! We have this Arduino microcontroller with all these pins what should we connect to them? Outputs: LED's, LCD screens, wired/wireless communication Inputs: Buttons, Switches, temperature sensors, rotary encoders, etc. We'll start simple and try to attach an LED (output) and a pushbutton/switch (input)

27 (Light-Emitting) Diodes The simplest output we can control is an LED (Lightemitting diode) which is like a tiny light bulb An LED glows ('on') when the voltage across it is greater than 1.7-2V and is 'off' otherwise Voltage/Current Relationship: For a resistor, current flowing through a resistor is proportional to the voltage across it (I = 1/R * V) For an LED, current grows exponentially with voltage [I Ae v ] Since a small change in voltage could cause a large increase in current and possibly blow-out the LED, we need to limit current with a resistor LEDs are polarized meaning they only work in one orientation (longer leg must be at higher voltage) Longer leg connects to the side with the higher voltage Shorter leg connects to the side with the lower voltage http://www.custobots.com/sites/def ault/files/imagecache/product_full/p roducts/solarbotics-redled.gif

28 LED Connection Approaches Below are some options for connecting an LED We need a series resistor to limit current Choose value based on amount of current you want Amount of current will determine brightness of LED i = V1/R1 = (Vs-V LED ) / R1 Usually R1 is a few hundred ohms (330 or 470 ohms) Longer leg + V LED - LED Schematic Symbol Shorter leg No current limitation BAD Choose resistor to limit current An Arduino output will serve as our voltage source that can be either '0' (0V) or '1' (5V) Doesn't matter where resistor is placed as long as it is in series Breadboard view

29 LED Connection Approaches When letting a digital output control an LED, the value (i.e. '0' or '1') that causes the LED to light up depends on how the circuit is wired Note: Gates can often "sink" (take in) more current than they can "source" (push out), so option 2 may be preferred but let's not worry about this now let's use option 1 Can be discrete gate or Arduino output pin Option 1 Option 2 LED is on when gate outputs '1' LED is on when gate outputs '0' Main Point: LED's should always be connected in series with a current-limiting resistor Can be discrete gate or Arduino output pin

30 Switches and Pushbuttons Switches and pushbuttons can be in one of two configurations: open or closed Switches can be opened or closed and then stay in that position until changed Pushbuttons are open by default and require you to push them to close the circuit (they then open when you release) Important Note 1: When open a SW/PB looks like an infinite resistance (no current can flow) When closed a SW/PB looks like a wire (R=0) and no voltage drops across it Important Note 2: SW or PBs don't produce digital 0's or 1's on their own, they control what voltage (PWR/GND) is connected to your device SW = Switch (Open => R=inf.) PB = Pushbutton (Open => R=inf) A closed SW or PB looks like a wire (R=0)

31 Power & Ground Connections Easy mistake when you're just learning to wire up circuits: Wire the inputs & outputs but forget to connect power and ground All circuits and chips require a connection to a power source and ground Gates Switches Buttons Actual connection will be drawn like this

32 Connecting a Switch Switches do not produce a 0 (GND) or 1 (VDD) by itself Option 1: Attach one side to GND and the other side to the device When the switch=open, nothing is connected to the device (a.k.a. floating ) A floating input may sometimes appears as zero, and other times as a one. We need the inputs to logic gates to be in either the 0 or 1 state not floating Option 2: SW open => Input = VDD = 1 SW closed => Direct wire from both VDD and GND to input = Short Circuit = unknown voltage and possibly LARGE current flow BAD!!! Vin = floating = unknown SW Vin Arduino input model R inf. Option 1: Bad (floating) Unlimited current flow when closed SW Vdd Arduino input model R inf. Option 2: Connect GND on one side & PWR on the other

33 Using a Pull-up Resistor Solution: Put GND on the far side and a "pull-up" resistor at the input side "Pull-up resistor" used to hold the input high unless something is forcing it to a zero SW open => Arduino input looks like inf. Resistance in series with Rp. Thus no current through Rp and thus no voltage drop across Rp input = VDD = 1 SW closed => Direct wire from GND to input input = GND = 0 Also current flowing from Vdd to GND is limited by Rp preventing a short circuit. Usually Rp is large (10k ohms) to limit current SW Rp Vin Vdd Arduino input model R inf. Preferred: Use a pullup resistor To calculate Vin: Vin = Vdd V RP Vin = Vdd i RP *Rp i RP =0 since in series with inf. resistance of Arduino input Thus, Vin = Vdd Main Point: Buttons & switches should have GND connected to one side & a pull-up resistor on the other

34 Alternative Switch/Resistor Connections Consider the options to connect PWR & GND to a SW/PB Note: A gate input "looks like" an inf. resistance Option 1 Option 3 I/O Pin Arduino Arduino Arduino Preferred Gate input always '1' Option 2 Option 4 Arduino Less Preferred (just take our word) Arduino Gate input always '0'

35 Controlling the pins of the Arduino to be digital inputs and outputs ARDUINO DIGITAL I/O

36 Overview In the next few slides you will learn What your software needs to do to setup the pins for use as digital inputs and/or outputs To set bits (to 1) and clear bits (to 0) using bitwise operations (AND, OR, NOT) to control individual I/O pins How to do it in a readable syntax using shift operators (<<, >>) Don't be worried if it doesn't make sense the first time listen, try to make sense of it, and ask a lot of questions.

37 Controlling I/O Ports Each port (B, C, and D) has 3 registers in the µc associated with it that control the operation Each bit in the register controls something about the corresponding I/O bit. Data Direction Register (DDRB, DDRC, DDRD) Port Output Register (PORTB, PORTC, PORTD) Port Input Register (PINB, PINC, PIND) You'll write a program that sets these bits to 1's or 0's as necessary DDRD DDRB What you store in the register bits below affect how the pins on the chip operates DDRD7 DDRD6 DDRD5 DDRD4 DDRD3 DDRD2 DDRD1 DDRD0 DDRB7 DDRB5 DDRB4 DDRB3 DDRD2 DDRD1 DDRD0 PORTD PORTB PORT D7 PORT D6 PORT D5 PORT D4 PORT D3 PORT D2 PORT D1 PORT D0 PORT B7 PORT B5 PORT B4 PORT B3 PORT D2 PORT D1 PORT D0 PIND PINB PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 PINB7 PINB5 PINB4 PINB3 PIND2 PIND1 PIND0

38 Register 1: Data Direction Register DDRx (Data direction register) [x=b,c,d DDRB, DDRC, DDRD] DDRD Controls whether pins on the chip act as inputs or outputs. Example: If DDRB[5] = 0 -> PB5 (Port B bit 5 = DIG13 pin) will be used as input Example: If DDRB[5] = 1 -> PB5 (Port B bit 5) will be used as output All I/O lines start out as inputs when the µc is reset or powered up. 0 0 0 0 1 1 1 1 DDRB DDRD7 DDRD6 DDRD5 DDRD4 DDRD3 DDRD2 DDRD1 DDRD0 DDRB7 DDRB5 DDRB4 DDRB3 DDRB2 DDRB1 DDRB0 0 1 0 0 0 0 0 PD[7:4] = INPUT PD[3:0] = OUTPUT PB[5] = OUTPUT PB[4:0] = INPUT Consider a leaf BLOWER / VACCUM. There must be a switch to select if you want it to blow (output) or produce suction (input) DDR register is that "switch" PD[7:4] PD[3:0] PB[5] http://www.toro.com/enus/homeowner/yardtools/blowersvacs/pages/series.aspx?sid =gasblowervacsseries

39 Register 2: PORT Register PORTx (Primarily used if port X is configured as an output) When a pin is used as an output (DDRx[n] = 1), the corresponding bit in PORTx[n] determines the value/voltage of that pin. E.g. By placing a '1' in port B bit 5, pin PB5 will output a high voltage DDRD DDRB DDRD7 DDRD6 DDRD5 DDRD4 DDRD3 DDRD2 DDRD1 DDRD0 DDRB7 DDRB5 DDRB4 DDRB3 DDRB2 DDRB1 DDRB0 0 0 0 0 1 1 1 1 0 1 0 0 0 0 0 PORTD PORTB PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 1 1 0 0 1 0 0 1 PORTD1 PORTD0 PORTB7 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 1 0 0 0 0 1 0 PD[7:4] PD[3:0] PB[5] 1001 0 Main Point: For pins configured as outputs, the values you put in the PORT register will be the output voltages

40 Register 3: PIN Register PINx[n] (Used if PORT is configured as an input) When a bit is an input (DDxn=0), getting the bit from PINxn reflects the current value at the corresponding input pin The program doesn t have to do anything special to read the digital signals into the PIN register, just use the register name The action of referencing PINx causes all the signals to be acquired. if(pind == 0x00) // check if all the signals coming into port D are 0's char val = PINB; // read and save all 8 signals coming into port B in a variable 'val'. Programs must read the full eight bits in the PIN register, but can then use bitwise logical operations to check individual bits If a port is an input but has no signal connected to it, it will float and could be read as either zero or one. Main Point: For pins configured as inputs, referencing the PINx register samples the input voltages at all the pins

41 Review of Accessing Control Registers in C Control registers have names and act just like variables in a C program To put values into a control register you can assign to them like any C variable or perform bitwise operations DDRD = 0xff; // 0b11111111 or 255 DDRB = 255; PORTD = 0xc0; // 0b11000000 or 192 PORTD = 0b01110000; To read the value of a control register you can write expressions with them unsigned char myvar = PIND; // grabs all 8-inputs on the port D myvar = PINB & 0x0f; // you will see this grabs just the lower 4 inputs

42 Practice: Changing Register Bits Use your knowledge of the bitwise logic operations to change the values of individual bits in registers without affecting the other bits in the register. Set DDRB, bit 3 to a '1' DDRB = DDRB 0b00001000; // DDRB = 0x08; Clear the 2 upper bits in PORTC to 0 s PORTC = PORTC & 0x3f; // PORTC &= ~(0b11000000) Flip bits 7 and 1 in DDRC DDRC = DDRC ^ 0b10000010; // DDRC ^= 0x82; Check if PIND, bit 4 = '1' if (PIND & 0x10) { code } DDRB???????? PORTC???????? & DDRC???????? ^ PIND???????? &

EXAMPLES 43

44 Review To use a pin(s) as output: If you want to use Port B, bit 5 as an output, make DDRB, bit 5 = 1 Ex. DDRB = 0b00100000; Then perform operations on PORTB register to place the desired output value into bit 5 Ex. PORTB = 0b00100000; // make pin on B5 = Vdd (5V)

45 Blinking an LED Hardware and software to make an LED connected to D7 blink #include<avr/io.h> #include<util/delay.h> int main() { // Init. D7 to output DDRD = 0x80; DDRD???????? } // Repeat forever while(1){ // PD7 = 1 (LED on) PORTD = 0x80; _delay_ms(500); // PD7 = 0 (LED off) PORTD &= ~(0x80); _delay_ms(500); } // Never reached return 0; 1??????? PORTD???????? 1??????? & 0???????

46 Turning an LED on/off with PB Hardware to turn an LED connected to D7 on/off when pressing a pushbutton connected to D4

47 Turning on an LED from a Button Note: When the button is pressed a '0' is produced at the PD4 input #include<avr/io.h> DDRD (starts at 0's on reset) int main() { // Init. D7 to output DDRD = 0x80; // All pins start as input // on reset, so no need to // clear DDRD bit 4 0 0 0 0 0 0 0 0 1?? 0???? } // Repeat forever while(1){ // Is PD4 pressed? if( (PIND & 0x10) == 0){ // PD7 = 1 (LED on) PORTD = 0x80; } else { // PD7 = 0 (LED off) PORTD &= ~(0x80); } } // Never reached return 0; PIND???????? & 0 0 0? 0 0 0 0

48 Pull Up Resistors Adding and wiring pull-up resistors for input buttons can be time consuming Thankfully, each Arduino input bit has an internal optional pullup resistor associated with it. If the pull-up is enabled, in the absence of an input signal, the input bit will be pulled up to a logical one. The pull-up has no effect on the input if an active signal is attached. This pull-up resistor can be built separately on your circuit board OR there is one on each pin of the Arduino that can be enabled Arduino Arduino Arduino Built Separately Enabled in the Arduino

49 Enabling Pull Up Resistors When DDRx[n] is '0' (i.e. a pin is used as input), the value in the PORTx[n] registers determines whether the internal pull-up is enabled Remember, the PORT register is normally used when a pin is an output, but here its value helps enable the internal pull-up resistor DDRD DDRD7 DDRD6 DDRD5 DDRD4 DDRD3 DDRD2 DDRD1 DDRD0 0 0 0 0 1 1 1 1 Inputs Outputs A pin being used as an input (DDR bits = 0) whose corresponding PORT bit = 1 will enable the pull up resistors on the PIN bit PORTD PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 1 1 1 1 0 0 1 1 Enable Pull-Up Resistors Actual output values from PD3-0 PIND PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 0 1 1 0???? Made-up values read from the pushbuttons (which don't require you to wire up external pull-up resistors) PD[7:4] (connected to buttons) PD[3:0] 0011

50 Using Internal Pull-up Resistors Let's simplify our wiring and use the internal pull-up resistors Arduino Arduino Built Separately Arduino Enabled in the Arduino

51 Turning on an LED from a Button Note: When the button is pressed a '0' is produced at the PD4 input #include<avr/io.h> DDRD (starts at 0's on reset) int main() { // Init. D7 to output DDRD = 0x80; 0 0 0 0 0 0 0 0 // Enable pull-up on PD4 PORTD = 0x10; 1?? 0???? } // Repeat forever while(1){ // Is PD4 pressed? if( (PIND & 0x10) == 0){ // PD7 = 1 (LED on) PORTD = 0x80; } else { // PD7 = 0 (LED off) PORTD &= ~(0x80); } } // Never reached return 0; PORTD???????? 0 0 0 1 0 0 0 0

52 Using "good" syntax/style when performing logic operations FIDDLING WITH STYLE!

53 Code Read-ability Tip #1 Try to replace hex and binary constants with shifted constants #include<avr/io.h> int main() { // Init. D7 to output DDRD = 0x80; // Enable pull-up on PD4 PORTD = 0x10; #include<avr/io.h> int main() { // Init. D7 to output DDRD = (1 << DD7); // Enable pull-up on PD4 PORTD = (1 << PD4); This syntax tells us we are putting a '1' in bit 7 (DD7) or bit 4 (PD4) } // Repeat forever while(1){ // Is PD4 pressed? if( (PIND & 0x10) == 0){ // PD7 = 1 (LED on) PORTD = 0x80; } else { // PD7 = 0 (LED off) PORTD &= ~(0x80); } } // Never reached return 0; } // Repeat forever while(1){ // Is PD4 pressed? if( (PIND & (1 << PD4)) == 0){ // PD7 = 1 (LED on) PORTD = (1 << PD7); } else { // PD7 = 0 (LED off) PORTD &= ~(1 << PD7); } } // Never reached return 0; We will teach you what all this means in the next slides

54 Shift Operations In C, operators '<<' and '>>' are the shift operators << = Left shift >> = Right shift Format: data << bit_places_to_shift_by Bits shifted out and dropped on one side Usually (but not always) 0 s are shifted in on the other side x x = x >> 2; (Right Shift by 2 bits_ 0 0 0 0 1 1 0 0 Original x 0 s shifted in 0 s shifted in 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 x x Shifted by 2 bits x = x << 2; Left Shift by 2 bits: 0 0 0 0 1 0 1 0 Original x x Shifted by 2 bits x x

55 Another Example To get a 1 in a particular bit location it is easier to shift the constant 1 some number of places than try to think of the hex or binary constant 0 x 0 1 0 0 0 0 0 0 0 1 = +1 1 << 3 0 0 0 0 1 0 0 0 0 x 0 8 0 s shifted in Suppose we want a 1 in bit location 3. Just take the value 1 and shift it 3 spots to the left 1 << 5 0 s shifted in 0 0 1 0 0 0 0 0 0 x 2 0 Suppose we want a 1 in bit location 5. Shift 1 5 spots to the left. Easier than coming up with 0x20

56 #define macros Can be used for simple find/replace scenarios #define find_pat replace_pat Makes constants more readable and easier to change (if you have the same constant in 10 places and you realize you need to change it, just change the one #define statement) #define MAX_VALUE 100 int counter = MAX_VALUE; Original Code int counter = 100; Compiler sees #define DD7 7 #define PD4 4... DDRD = (1 << DD7); PORTD = (1 << PD4); Original Code #define DD7 7 #define PD4 4... DDRD = (1 << 7); PORTD = (1 << 4); Compiler sees...

57 Register Bit Names Symbolic names for the positions of bits in each register are defined as constants in <avr/io.h> #include <avr/io.h> Each PORTx register has their own constants Constant 7 6 5 4 3 2 1 0 PORTB PB5 PB4 PB3 PB2 PB1 PB0 PORTC PC5 PC4 PC3 PC2 PC1 PC0 PORTD PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 All DDRx registers share the same constants Constant 7 6 5 4 3 2 1 0 DDRB,C,D DD7 DD6 DD5 DD4 DD3 DD2 DD1 DD0 All PINx registers share the same constants Constant 7 6 5 4 3 2 1 0 PINB,C,D PIN7 PIN6 PIN5 PIN4 PIN3 PIN2 PIN1 PIN0

58 Putting it All Together Values for working with bits can be made using the << shift operator OK: PORTB = 0x20; Better: PORTB = (1 << PB5) ; OK: DDRD = 0x04; Better: DDRD = (1 << 2); This makes the code more readable and your intention easier to understand More examples DDRC = (1 << DD5) set DDRC, bit 5 PORTB ^= (1 << PB2) invert PORTB, bit 2 PORTD &= (1 << PD3) clear PORTD, bit 3 <- WRONG! Why?

59 Clearing Bits A Common Mistake When using the &= operation to clear bits, remember to invert the bits. This won t work to clear PD3 to 0 PORTD &= (1 << PD3); is the same as PORTD &= 0b0001000; which clears everything but bit PD3 Use the ~ operator to complement the bits. PORTD &= ~(1 << PD3); is the same as PORTD &= 0b11110111; and now PD3 gets cleared.

60 Defining Your Own Symbolic Names You can make your own more meaningful symbolic names #define LEDBIT (1 << PB6) #define CLKBIT (1 << PC3) PORTB = LEDBIT; PORTC = CLKBIT; PORTC &= ~CLKBIT; Can combine multiple bits into one defined value #define MULTIBITS ((1 << PB3) (1 << PB4) (1 << PB5)) is the same as #define MULTIBITS 0b00111000 1 << PB3 00001000 1 << PB4 00010000 1 << PB5 00100000 00111000

COPYING BITS 61

62 Copying Multiple Bits Suppose we want to copy a portion of a variable or register into another BUT WITHOUT affecting the other bits Example: Copy the lower 4 bits of X into the lower 4-bits of PORTB but leave the upper 4-bits of PORTB UNAFFECTED Assignment doesn't work since it will overwrite ALL bits of PORTB PORTB = x; // changes all bits of PORTB x 0 1 0 0 0 0 1 1 PORTB???????? PORTB???? 0 0 1 1 Desired Result PORTB 0 1 0 0 0 0 1 1 PORTB = x; Result

63 Copying Into a Register Solution use these steps: Step 1: Define a mask that has 1 s where the bits are to be copied #define MASKBITS 0x0f Step 2: Clear those bits in the destination register using the MASK PORTB &= ~MASKBITS Step 3: Mask the appropriate field of x and then OR it with the destination, PORTB PORTB = (x & MASKBITS); Step 1 Step 2 Step 3 x 0 1 0 0 0 0 1 1 MASKBITS = 0 0 0 0 1 1 1 1 PORTB???????? & 1 1 1 1 0 0 0 0 PORTB???? 0 0 0 0 x 0 1 0 0 0 0 1 1 & MASKBITS 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 PORTB???? 0 0 0 0 Result PORTB???? 0 0 1 1

64 Do We Need Step 2 Yes!!! Can't we just do step 1 and 3 and OR the bits of x into PORTB #define MASKBITS 0x0f PORTB = (x & MASKBITS); No, because what if the destination (PORTB) already had some 1's where wanted 0's to go Just OR'ing wouldn't change the bits to 0 That's why we need step 2 Step 2: Clear those bits in the destination register using the MASK PORTB &= ~MASKBITS; Step 1 & 3 Result PORTB???? 1 1 1 0 What if PORTB just happened to have these bits initially x 0 1 0 0 0 0 1 1 & MASKBITS 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 PORTB???? 1 1 1 0 PORTB???? 1 1 1 1

65 Copying To Different Bit Locations What if the source bits are in a different location than the destination Ex. Copy lower 4 bits of x to upper 4 bits of PORTB Step 1: Define a mask that has 1 s where the bits are to be copied #define MASKBITS 0xf0 Step 2: Clear those bits in the destination register using the MASK PORTB &= ~MASKBITS Step 3: Shift the bits of x to align them appropriately, then perform the regular step 3 PORTB = ((x<<4) & MASKBITS); Step 1 Step 2 Step 3 Result x 0 1 0 0 0 0 1 1 MASKBITS = 1 1 1 1 0 0 0 0 PORTB???????? & 0 0 0 0 1 1 1 1 PORTB 0 0 0 0???? x 0 1 0 0 0 0 1 1 x << 4 0 0 1 1 0 0 0 0 & MASKBITS 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 PORTB 0 0 0 0???? PORTB 0 0 1 1????

DEBOUNCING SWITCHES 66

67 Counting Presses Consider trying to build a system that counted button presses on PC2 (increment once per button press) We can write code to check if the button is pressed (==0) and then increment 'cnt' But remember, your code executes extremely fast what will happen? #include<avr/io.h> int main() { PORTC = (1 << PC2); int cnt = 0; while(1){ char pressed = (PINC & 0x04); if( pressed == 0 ){ cnt++; } } return 0; } PC2 Arduino PC2 cnt 0 0 1 2 3 3

68 Waiting Through a Press Consider trying to build a system that counted button presses on PC2 (increment once per button press) We can write code to check if the button is pressed (==0) and then increment 'cnt' But remember, your code executes extremely fast what will happen? #include<avr/io.h> int main() { PORTC = (1 << PC2); int cnt = 0; while(1){ char pressed = (PINC & 0x04); if( pressed == 0 ){ while( (PINC & 0x04) == 0 ) {} cnt++; } } return 0; } if if if while while while if PC2 Arduino PC2 cnt 0 0 0 0 0 1 1

69 Interfacing Mechanical Switches/Buttons Mechanical switches and buttons do not make solid, steady contact immediately after being pressed/changed For a short (few ms) time, bouncing will ensue and can cause spurious SW operation (one press of a button may look like multiple presses) Need to debounce switches with your software Usually waiting around 5 ms from the first detection of of a press will get you past the bouncing and into the stable period Stable Bouncing Stable Bouncing Stable Arduino Button Press Button Release

bouncing bouncing 70 Waiting Through a Press Consider trying to build a system that counted button presses on PC2 (increment once per button press) We can write code to check if the button is pressed (==0) and then increment 'cnt' But remember, your code executes extremely fast what will happen? #include<avr/io.h> int main() { PORTC = (1 << PC2); int cnt = 0; while(1){ char pressed = (PINC & 0x04); if( pressed == 0 ){ _delay_ms(5); while( (PINC & 0x04) == 0 ) {} _delay_ms(5); cnt++; } } return 0; } if if if while while while if PC2 Arduino PC2 cnt 0 0 0 0 0 1 1

71 What's Your Function Because there is a fair amount of work to do just to recognize a button press, you may want to extract those to functions you can call over and over again #include<avr/io.h> char pc2pressed() { char pressed = (PINC & 0x04); if( pressed == 0 ){ _delay_ms(5); while( (PINC & 0x04) == 0 ) { } _delay_ms(5); return 1; } else return 0; } int main() { PORTC = (1 << PC2); int cnt = 0; while(1){ if( pc2pressed() ) cnt++; } return 0; }