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

Similar documents
Embedded programming, AVR intro

A practicalintroduction to embedded programming. Brian Plancher 10/17/2018

Embedded Systems and Software

Figure 1: Pushbutton without Pull-up.

Software debouncing of buttons

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

EE 109 Unit 4. Microcontrollers (Arduino) Overview

Physics 364, Fall 2012, Lab #9 (Introduction to microprocessor programming with the Arduino) Lab for Monday, November 5

The Atmel ATmega328P Microcontroller

Physics 335 Intro to MicroControllers and the PIC Microcontroller

Robosoft Systems in association with JNCE presents. Swarm Robotics

The Atmel ATmega168A Microcontroller

1/Build a Mintronics: MintDuino

AVR Board Setup General Purpose Digital Output

CAUTION: TTL Only, Do Not Use ± 12 V RS-232

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

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

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

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

Lab-3: LCDs Serial Communication Analog Inputs Temperature Measurement System

EK307 Lab: Microcontrollers

Blackfin Online Learning & Development

IAS0430 MICROPROCESSOR SYSTEMS

DIRRS+ Digital Infra-Red Ranging System Ideal for robotics projects. Singles (SKU # Pack (SKU #35100)

AVR Simulation with the ATMEL AVR Studio 4 Updated: 8/3/2005

CMPE C Programming & Embedded Systems. Discussion I (Version 2.0) August 31, 2014

CN310 Microprocessor Systems Design

Instruction Manual for BE-SP3 Circuit. 10/21/07

Laboratory 10. Programming a PIC Microcontroller - Part II

Debugging embedded HW/SW systems differs greatly from debugging software or hardware alone. Key steps to debugging: 1

Discharge by touching: BNC coax shield, outlet metal cover plate, wire connected to GND

Adafruit 1-Wire GPIO Breakout - DS2413

Lesson 5: Verifying RAMs with the Fluke 9010A Version 1.03

COOKING WITH TEAM 279

Module 2: Introduction to AVR ATmega 32 Architecture

Cygnos360 V2 Installation Manual

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Lecture 12. Building an LED Display

Halloween Pumpkinusing. Wednesday, October 17, 12

Embedded Systems and Software

Physics 120/220 Lab Equipment, Hints & Tips

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Building your own special-purpose embedded system gadget.

ENGR 40M Project 3c: Switch debouncing

INTERFACING HARDWARE WITH MICROCONTROLLER

CanSat Project Supervisor: Adam Cseh 0931/ Robotics Hall / Office 4

Writing Code and Programming Microcontrollers

Device: LDP This document Version: 1.0. Date: July Description: 80x08 1R1G LED Display Panel

GPS Series. Build a GPS Smart Logger. By Michael Simpson. As seen in November 2008 of Servo Magazine Pick up an issue at

irobot Command Module Owners manual

Project 17 Shift Register 8-Bit Binary Counter

Lab Course Microcontroller Programming

Device: PLT This document Version: 3.0. For hardware Version: 4. For firmware Version: Date: April 2014

CONTENTS BIGAVR2 KEY FEATURES 4 CONNECTING THE SYSTEM 5 INTRODUCTION 6

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

ME2110: Creative Decisions and Design Electromechanical and Pneumatic Kit Manual

Cygnos360 V2 Installation Manual

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Logic Chip Tester User Manual SW Version /8/2012. Chapter 1 Introduction/Background

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

PARTS LIST 1 x PC Board 36 x 5mm Red LED 36 x 12mm LED Standoff 36 x NPN Transistor 36 x 10kΩ Resistor OTHER PARTS YOU MAY NEED

Executive summary. Gather up the materials & tools required. Set up the BT2S for the ProChrono baud rate:

Micro-Controllers. Module 2: Outputs Control and Inputs Monitoring. IAT Curriculum Unit PREPARED BY. August 2008

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

M32 Development Board

Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino. Project 15. Shift Register 8-Bit Binary Counter

The short program to blink the red LED used the delay() function. This is very handy function for introducing a short time delays into programs.

Photoresistor, Transistor, and LED s

E85: Digital Design and Computer Engineering Lab 1: Electrical Characteristics of Logic Gates

SLCD1-IC Serial LCD Processor

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

Input/Output Ports and Interfacing

Butterfly Alternate Pin Uses. Joe Pardue 12/31/06

Lab #3: Keypad Scanning in C Week of 11 February 2019

Manual Version March 2007

T H E I N T E R A C T I V E S H E L L

Arduino Prof. Dr. Magdy M. Abdelhameed

INTERRUPTS in microprocessor systems

Chapter Two - SRAM 1. Introduction to Memories. Static Random Access Memory (SRAM)

ATMEGA32 MUSIC PLAYER

ECGR 4101/5101, Fall 2011: Lab 4 A pseudo physics simulator with remote communication.

HiJack Hello world! Project

CSCI 2212: Intermediate Programming / C Chapter 15

How-To: Make an RGB combination door lock (Part 1)

TMC3in1 Torch & Motion Controller 3in1. Vol. 04: FAQ and Troubleshooting Guide

An open-source hardware+software project. For design files and additional documentation, please visit:

Arduino Uno R3 INTRODUCTION

BV4627 User Guide. Binary Interface

CSCE 436/836: Embedded Systems Lab 1b: Hoverboard Programming Introduction

I2C and SPI Foundation

Introduction to Assembly language

CONTENTS. dspicpro4 KEY FEATURES 4 CONNECTING THE SYSTEM 5 INTRODUCTION 6

Robotics Training Module ABLab Solutions

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

TA0013 ARDUINO RFID UNO STARTER KIT

Lesson 11. Programming a PIC

Wireless Sensor Networks. FireFly 2.2 Datasheet

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

YOU WILL NOT BE ALLOWED INTO YOUR LAB SECTION WITHOUT THE REQUIRED PRE-LAB.

Transcription:

Microcontrollers Class 1: Serial and Digital I/O March 7, 2011 Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

See http://wiki.hacdc.org/index.php/avr2011_kit Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

Hello World Example Blinkies! Last class, showed an example that turned a pin on and off Sections of the C code: preamble includes and defines function definitions (didn t have any) main function (chip initialization and endless loop) The main loop twiddled a bit back and forth in a memory register, and that made Vcc and GND volts appear on a particular pin. But let s flesh that all out a little more... Registers Special memory locations Usually we think of memory as being a place to store info In micros, some special memory regions change the way the chip behaves: Registers DDRx register from initialization of LED blinking demo Writing a one to a bit in the DDRx register sets up a corresponding pin for output There s a similar mapping from the PORTx register to the output of the pins: writing a 1 to a bit in PORTx sets the corresponding pin at Vcc, 0 to GND When the pins are configured for input, the PIN registers read 0 if a low voltage is present on its pin, and 1 for high

Addressing the Pins Writing bits to registers So, say we re working on PORTB, and we want to set pin PB2 and PB6 to 5v (to light up some LEDs) Write a 1 in the 2nd and 6th slots in the PORTB register Write it in binary directly: PORTB = 0b01000100; Write it using its decimal value: PORTB = 68; Write it in hex: PORTB = 0x44; Write it using a bit-value macro: PORTB = BV(2) BV(6); Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

The Math Bit-shift Operators <<: Left shift >>: Right shift Binary logic & : AND : OR ^: XOR ~: NOT Bit-shifting Left shift: << Very handy: say you want a 1 in the pin-3 place: 00001000 Start with 1: 00000001 Shift it over 3: 1 << 3 = 00001000 Or using the pin-name macros: 1 << PD3 #define BV(bit) (1 << (bit)) Right shift: >> Start with 12: 00001100 Shift 2: 12 >> 2 = 00000011 Note that right-shift is like dividing by 2 n : handy (Similarly, left-shift is like multiplying by 2 n )

Using Shifts Practical examples: PORTD = (1 << 3); PORTD = (1 << PD3); PORTD = (1 << (1+2)); j = 3; PORTD = (1 << j); j = 3; PORTD = BV(j); Set Two Pins Addition: Say we want PB3 and PB4 both on Add them together? PORTB = BV(PB3) + BV(PDB); will work After all: 00001000 + 00010000 = 00011000 Works if you re just setting the port using PORT = something; But you never see bitwise addition. Why?

Turning Pins On Why addition won t cut it What if you don t know (or care) what LEDs are already on, but you want to turn on PD3? PORTD = PORTD + BV(PD3);? 00000100 If PD3 is already on: + 00000100 = 00001000 01111100 Could be worse: + 00000100 = 10000000 We need an OR statement Ouch! Turning More Pins On The OR statement: 0b01000010 = BV(1) BV(6), so it s as good as addition OR turns on a bit if this bit or that bit is on PORTD = PORTD BV(PD3); 11000000 If PD3 is not on: 00000100 = 11000100 If PD3 is already on: 11000100 00000100 = 11000100 Yay! Turn on PD3, PD4, PD5? PORTD = PORTD ( BV(PD3) BV(PD4) BV(PD5)); And here s a nice shorthand: X = X + Y X += Y PORTD = BV(PD3) BV(PD4) BV(PD5);

Turning Pins Off The AND statement: & * AND turns on a bit if this bit and that bit are both on 11111110 & 10001101 = 10001100 Can think of AND as masking out the the off bits Use it to turn off PD3: PORTD &= 11110111? PORTD &= ( BV(PD7) BV(PD6)... skip PD3... BV(PD0) ); Works, but it sucks. Turning Pins Off II The NOT statement: There must be a better way to make 11110111 BV(PD3) = 00001000 ~ BV(PD3) = 11110111 Turn off PD3: PORTD &= ~ BV(PD3); Turn off PD3 and PD4: PORTD &= ~( BV(PD3) BV(PD4)); Careful with those parentheses! Also remember this in terms of the fundamentals: PORTD &= ~((1 << PD3) (1 << PD4)); PORTD &= ~((1 << 3) (1 << 4));

Toggling a Pin The XOR statement: ˆ A lot of the time, it s handy to be able to toggle a bit PORTD ^= BV(PD3); 11111001 ˆ 00001000 = 11110001 11110001 ˆ 00001000 = 11111001 Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

One Last Part......then Cylon Eyes So we know how to turn on bits, and how to turn them off How do we make cylon eyes? Start with light 0 on. Turn off the 0th, turn on the 1st, pause turn off the 1st, turn on the 2nd, pause etc PORTD &= ~ BV(PD0); PORTD = BV(PD1); delay PORTD &= ~ BV(PD1); PORTD = BV(PD2); delay etc. {PORTD &= ~ BV(i) ; PORTD = BV(i+1); delay } And make i range from 0 to 7 and back again (being very careful about endpoints) Basic Looping The For loop for(i=0; i < 7; i = i + 1){...} Repeats the block in parentheses a bunch of times. First time, i = 0. Then it checks if i < 7. If not, it skips the block and moves on. If so, it executes the next command and then the block. So in our case, it executes the block with i= 0, 1, 2, 3, 4, 5, 6 and then is done. for(i=7; i > 0; i = i - 1){...} and a different block will bring it back down i= 7, 6, 5, 4, 3, 2, 1

Digital Output: Summary Configure, Write, Done So at this point, we re all set for doing all sorts of cool stuff with digital output First, set up the DDR for output (on pins of your choosing) by writing a 1 to the relevant bit The set the PORT register to set pins high or low, depending Loop, repeat Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

Initializing for Input It almost seems too easy... To initialize for output set bit to one DDRx = BV(whatever) For input, want to set the bit to zero instead. But zero is the default value. Done! Initialization for Input One wrinkle: Initialize a pullup resistor A pullup resistor ties the input pin to 5v (internally) when it s not pulled low from outside Often want a pullup with input Why? Simplest input circuit is a switch from pin to ground AVR s PORTn does double-duty. In output mode, controls output. In input mode, selects the pullup So often set PORTn to one to enable the pullup: PORTB = BV(PB3);

Reading the Input Reading the input register Input values in the PINx register Can read them like readin = PINB; readin will contain an 8-bit number, each bit corresponding to the voltage state of all 8 of its pins. Reading one pin: the most common case PIND & BV(PD3); If PD3 has more than 1.25v on it, we ll get 00001000 If PD3 has less than 1.25v on it, we ll get 00000000 Test of pin state: if((pind & BV(PD3)) == 0){...} or if(!(pind & BV(PD3))){...} See simplepushbutton.c Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

The Real World Switching Noise In reality, switches make/break contact a bunch of times as you press it Two pieces of metal touching, bending, with different resistance all over If you re trying to make a per-button-press device, this can cause troubles Symptom: Get multiple presses for what you thought was a single press Solution: Many Approaches Delay I: turn on after a short delay after first button press Delay II: wait short period of time after first press, test again if it s still pressed Integrate: test N times in a row, with a delay between, decide the button is pressed if more than M hits There are many others. There was even a Hackaday competition recently for favorite debounce algorithms (http://hackaday.com/2010/11/09/ debounce-code-one-post-to-rule-them-all/) http://www.ganssle.com/debouncing.htm I ll send code around for you to experiment with Advertisement for Hardware Timers!

When To Debounce? To Debounce When you re counting events When you need to know how long the button is held down When it s not really a button, but an analog voltage, and it spends a bunch of time in the dreaded 0.8v - 1.5v range Or Not to Debounce When all you care about is on/off, don t mind the bounce When other parts of the code act as a delay Outline Quick Tour of the Board Pins, Ports, and Their Registers Boolean Operations Cylon Eyes Digital Input and Testing Particular Pin States Serial Communications

Simple Serial The easiest way to get rich debugging info The microcontroller really comes into its own as an interface The USART serial port (and a USB serial cable) is the easiest way to get data to and from your computer AVR has a built-in hardware serial machine, all you have to do is load its buffer up This is your first include of a non-standard file: #include USART88.h If you re curious how I wrote them, the USART serial section of the datasheet is a good place to start. Dive in!... or just look at examples and monkey it. Using USART88.h What Do the Functions Do? #define BAUDRATE 9600 inituart(): uses BAUDRATE to set up the baud rate then some bits in the USART config register are set for stop bits and parity transmitbyte(): wait for the USART busy flag to become unset load the data into the transmit buffer register walk away, letting the hardware serial do the rest receivebyte(): once initialized, the hardware USART is always receiving wait for the USART received-data flag to be set return the data

Serial Interfacing For the Big Computer Screen: for terminal emulation screen /dev/ttyusb0 or even screen /dev/ttyusb0 9600 Python: pyserial http://pyserial.sourceforge.net/ for everything else Serial Ideas Things I Have Done With USART Serial Control 4x4x4 LED cube from my desktop Simple menu system for a logging accelerometer GPS datalogger Parallax RFID readers Hook up 2 AVRs (radio, IR LED, wires) Debug, debug, debug!

The End Outline