27
ENGR 355 Take Home Lecture 2

ENGR 355 Take Home Lecture 2 WRITING CODE Introductory Comments /* Disgruntled Avians Lab 6 ENGR355 by Tim Kyle Rev 01.45 1/1/2014 LM-3551 Tilt Sensor

Embed Size (px)

Citation preview

  • Slide 1
  • Slide 2
  • ENGR 355 Take Home Lecture 2
  • Slide 3
  • WRITING CODE Introductory Comments /* Disgruntled Avians Lab 6 ENGR355 by Tim Kyle Rev 01.45 1/1/2014 LM-3551 Tilt Sensor attached at SPI port 1. 16x2 LCD parallel interface p5-p12 Reset switch p2 */
  • Slide 4
  • WRITING CODE Include, Global Definitions, Interface Assignments and Constants #include mbed.h const float pi=3.1415; //constant declaration, does not ever change DigitalOut BlueLED p6; //defines pin 6 as BlueLED int program_cycles=0; //global variable declaration A list of the official libraries can be found at: http://developer.mbed.org/handbook/Homepage http://developer.mbed.org/handbook/Homepage
  • Slide 5
  • WRITING CODE Functions and/or Function Prototypes void porcine_splash() {} //Displays lots of advertisements so my program will make lots of money. void display_lots_of_advertisements{ insert_lots_of_code_here(); collect_the_money_here(); }
  • Slide 6
  • WRITING CODE Main function Your code should never end. Often this means it will end with a while(1) or similar unending command. int main() { porcine_splash(); while(1) { display_lots_of_advertisements(); } //if program execution continues down here, uninitialized program memory is executed and BAD things might happen.
  • Slide 7
  • GPIO GENERAL PURPOSE INPUT/OUTPUT Pins that can be used as input, output or a combination of both. Most processor pins function as GPIO some have additional functions. On the mbed, the pins capable of GPIO are labelled p5-p30 and p33-p36. An alternative labelling is the processor labelling that includes the port number (ie. P0_8, for Port 0 Pin 8)
  • Slide 8
  • GPIO GENERAL PURPOSE INPUT Measures the voltage of a given pin to determine its state. Pin 2.0V is a logic high. DigitalIn enables an automatic pull down resistor Without a pull up or pull down resistor, the logic input can float randomly between states when the switch is open. HIGH IMPEDIENCE LOGIC INPUT ON MICROCONTROLLER 3.3V
  • Slide 9
  • GPIO GENERAL PURPOSE INPUT The pull down resistor will pull the logic input low when the switch is open. When the switch is closed the logic input transitions high. HIGH IMPEDIENCE LOGIC INPUT ON MICROCONTROLLER 3.3V
  • Slide 10
  • GPIO GENERAL PURPOSE OUTPUT 0 = Logic Low (V OL 0.4V)pin will sink current 1 = Logic High (V OH V DD -0.4V) pin will source current The guaranteed minimum source/sink current is 4mA/pin. This is low compared to other microprocessors. More typical is 20mA/pin Not all pins are created equally. P21 is the high current output pin and is guaranteed to drive at least 20mA P27 and P28 are the open drain pins and are guaranteed to sink at least 20mA It is safe to source or sink more current (20mA/pin) in the prototype phase.
  • Slide 11
  • GPIO GENERAL PURPOSE INPUT & OUTPUT A pin or group of pins, called a bus, can switch roles between inputs and outputs. This is useful when the pins are driving a bidirectional bus. In the diagram below, data flows back and forth between the LCD and the Microprocessor on this 4 bit parallel data bus. mbed d1 d2d3d4 d5d6d7d8 p1 p2 p3 p4 p5 p6 p7 p8 LCD
  • Slide 12
  • GPIO mbed d1 d2d3d4 d5d6d7d8 p1 p2 p3p4 p5p6p7p8 LCD GENERAL PURPOSE INPUT & OUTPUT When the mbed is sending commands to the LCD the mbed bus on p1-p8 they are acting as outputs.
  • Slide 13
  • GPIO mbed d1 d2d3d4 d5d6d7d8 p1 p2 p3 p4 p5 p6 p7 p8 LCD GENERAL PURPOSE INPUT & OUTPUT When the LCD is sending a response the mbed bus must act as inputs to receive the reply.
  • Slide 14
  • GPIO Switches A common device connected to a GPIO input is a switch. Switches come in various styles with various features. Tactile Switch Knife Switch Missile Switch Light Switch Easy Button
  • Slide 15
  • GPIO Switches Bounce When a switch is moved from one state to another, after the contacts smash together or apart, they bounce before making a solid logic level.
  • Slide 16
  • GPIO Debouncing Switch Methods Off-Processor Methods RC Filter Flip Flops Dedicated IC Software Methods State Machine Software Low Pass Filter Edge detect followed by a time delay DebounceIn interface (mbed specific)
  • Slide 17
  • GPIO LEDs driven by a GPIO output. LEDs are handy but without proper current limiting, they can runaway and over heat. Runaway Forward voltage decreases with an increase in temperature, increasing the drive current, increasing the temperature until they overheat and fail. Prevent Runaway with a current limiting resistor.
  • Slide 18
  • GPIO Indication LED Specifications Significant Specifications for Indication Grade LEDs Forward Voltage (between 1V and 5V) Generally varies by color Forward Current (between 1mA and 20mA) They are related and typically shown as a graph on the datasheet.
  • Slide 19
  • LED Resistor Sizing
  • Slide 20
  • LED Resistor Sizing
  • Slide 21
  • LED Resistor Sizing
  • Slide 22
  • LED Resistor Sizing 3.3V GPIO OUTPUT R Current Source GPIO OUTPUT ALMOST 0V R Current Sink 3.3V Inverted Logic
  • Slide 23
  • LED Resistor Sizing 3.3V GPIO OUTPUT R Current Source i=10mA
  • Slide 24
  • LED Resistor Sizing R Current Source i=10mA V 1.8V V 3.3V + + _ _
  • Slide 25
  • LED Resistor Sizing R Current Source i=10mA V 1.8V V 3.3V + + _ _
  • Slide 26
  • LED Resistor Sizing R Current Source i=10mA V 1.8V V 3.3V + + _ _ 3.3V - 1.8V - (0.010A*R)=0 1.5V/0.010A = R R=150
  • Slide 27
  • LED An LED only works in one direction. Current normally flows from the longer leg (anode) to the shorter leg (cathode). Cathode is usually marked by a flat spot on the plastic housing.
  • Slide 28
  • The End