Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Microcontrollers

 Microcontrollers

My slides for my lecture on microcontrollers, which was given as part of the fourth year Robot Chemistry course.

Tom Phillips

January 24, 2014
Tweet

More Decks by Tom Phillips

Other Decks in Technology

Transcript

  1. Microcontrollers Thomas W. Phillips and Prof. John de Mello Plastic

    Electronics Doctoral Training Centre Imperial College London 24th January 2014 [email protected] http://www3.imperial.ac.uk/people/thomas.phillips07
  2. Lecture outline • recap: microcontrollers • digital IO • outputs:

    high and low • inputs: pull up and pull down resistors • analog IO • inputs: analog-to-digital conversion (ADC) • outputs: pulse width modulation (PWM) • communication protocols • RS232 • inter-intergrated circuit (I2C)
  3. Microcontrollers • MCs are… • small computers consisting of a

    processor, memory, inputs and outputs. • programmed to serve a dedicated role in a larger electrical or mechanical system. • not personal computers.
  4. Microcontrollers inputs outputs e.g. sensors, timers, other MCs e.g. LEDs,

    motors, valves, other MCs microcontroller (does computation)
  5. Arduino Uno Official: http://arduino.cc Intro comic: http://bit.ly/1eWHtfb digital inputs/outputs Atmel

    ATmega328 analog inputs USB power in power out • ~£10 • open source
  6. Arduino: getting started 1. Get an Arduino 2. Download the

    Arduino IDE from the Arduino web site 3. Connect to your computer using a USB cable 4. Open a sketch (e.g. blink) 5. Select serial port • on Windows, e.g. COM3 • on Mac/Linux, e.g. /dev/tty.usbmodem262471 6. Select board type, e.g. Uno 7. Upload!
  7. Arduino “sketches” void setup() { // put your setup code

    here, to run once } void loop() { // put your main code here, to run repeatedly } compiled on a computer then uploaded over USB
  8. Digital IO binary V high 1 5 low 0 0

    • a digital pin can be an input or an output inputs: read voltage consume little current outputs: supply voltage supply current when high Arduino Uno: max. 40 mA/pin pinMode(pin,INPUT); value = digitalRead(pin); pinMode(pin,OUTPUT); digitalWrite(pin,HIGH); digitalWrite(pin,LOW); http://arduino.cc/en/Tutorial/DigitalPins
  9. Digital inputs: pull up/down resistors pull up 5 V switch

    digitalRead(pin) 5 V high GND low neither high use to steer inputs to known state when no input is present pinMode(pin,INPUT_PULLUP) Arduino has internal pull up resistors: switch digitalRead(pin) 5 V high GND low neither low GND input pin 10 kΩ pull down input pin 10 kΩ
  10. Blink void setup() { pinMode(6, OUTPUT); // set pin 6

    to digital output } void loop() { digitalWrite(6, HIGH); // turn the LED on delay(1000); // wait for a second digitalWrite(6, LOW); // turn the LED off delay(1000); // wait for a second } digital pin 6 GND
  11. Analog inputs: ADC • analog signal is a continuously varying

    voltage • convert to a discrete digital signal to be useful to the MC • analog-to-digital conversion: 1. sample the analog signal at regular intervals sampling rate (max. 10 kHz) 2. measure the voltage at each sample n-bit ADC: 2n values (max. 10-bit) 3. assign it a discrete value
  12. Analog inputs: ADC time 0 5 analog signal: continuously varying

    voltage input (V) Worked example: 2-bit ADC
  13. Analog inputs: ADC time 0 5 measure (sample) voltage at

    regular intervals input (V) Worked example: 2-bit ADC
  14. Analog inputs: ADC time 0 5 compare to discrete levels

    input (V) 1.7 3.3 (2-bit, therefore 22 = 4 possible values) Worked example: 2-bit ADC
  15. Analog inputs: ADC time 0 5 choose closest discrete value

    1.7 3.3 input (V) Worked example: 2-bit ADC
  16. Analog inputs: ADC time 0 5 digital signal produced input

    (V) 1.7 3.3 Worked example: 2-bit ADC
  17. Analog inputs: ADC time 0 5 00 01 10 11

    binary input (V) 00 01 10 10 10 10 01 00 00 00 00 01 10 11 11 11 1.7 3.3
  18. Analog outputs: pulse width modulation t V 0 5 ~2

    ms duty cycle 0 % voltage out 0 V 25 % 1.25 V 50 % 2.5 V 100 % 5 V 0 5 V t 0 5 V t 0 5 V t http://www.arduino.cc/en/Tutorial/PWM
  19. Dimming an LED with PWM int brightness = 0; //

    brightness of LED void setup() { pinMode(6, OUTPUT); // set pin to output } void loop() { analogWrite(6, brightness); // set PWM value brightness = brightness + 5; // brighten if (brightness == 255) { // reach max value brightness = 0; // go back to 0 } delay(30); // 30 ms delay }
  20. Communication: RS232 TX RX GND device 1 e.g. computer RX

    TX GND device 2 e.g. Arduino • 3 wires: 1. transmit (TX) 2. receive (RX) 3. ground (GND) • voltage of TX and RX goes high/low to transmit binary digits (bits) • bits transmitted at specific rate called baud rate (e.g. 9600 bps) logic state VTX/RX (V) high 1 -3 > V > -15 low 0 3 < V < 15 Note that (for historical reasons) the HIGH logic state corresponds to the low/negative voltage and vice versa Tutorial: http://tinyurl.com/nf86hmh More advanced: http://tinyurl.com/putd5dp
  21. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    low high VTX is negative (logic state high) when no data is being transmitted
  22. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    VTX goes positive (logic state low) to send a start bit to denote start of byte start bit low high
  23. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    now transmit bit by bit for 8 bits start bit 0 low high
  24. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 now transmit bit by bit for 8 bits low high
  25. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 now transmit bit by bit for 8 bits low high
  26. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 1 now transmit bit by bit for 8 bits low high
  27. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 1 0 now transmit bit by bit for 8 bits low high
  28. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 1 0 0 now transmit bit by bit for 8 bits low high
  29. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 1 0 0 1 now transmit bit by bit for 8 bits low high
  30. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    start bit 0 1 0 1 0 0 1 0 now transmit bit by bit for 8 bits low high
  31. Communication: RS232 0 15 3 -3 -15 VTX (V) time

    stop bit: after 8 bits, VTX goes negative (logic state high) to wait for next start bit start bit 0 1 0 1 0 0 1 0 stop bit 8 bits = 1 byte low high
  32. RS232: ASCII conversion 01010010? Each letter, number and symbol encoded

    using ASCII: American Standard Code for Information Interchange Look up this in reference tables, e.g. ascii-code.com (but in practice MCs/computers do this for you) 01101000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00100001 = hello, world! 01010010 = R Decimal Binary Glyph 79 01001111 O 80 01010000 P 81 01010001 Q 82 01010010 R 83 01010011 S
  33. Setting LED blink rate over RS232 const int ledPin =

    6; int blinkRate=0; void setup() { Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud pinMode(ledPin, OUTPUT); // set this pin as output } void loop() { if (Serial.available()) { char ch = Serial.read(); if (isDigit(ch)) { blinkRate = (ch - '0'); // ASCII value converted to numeric value blinkRate = blinkRate * 100; // actual rate is 100ms times received digit } } blink(); } void blink() { digitalWrite(ledPin,HIGH); delay(blinkRate); digitalWrite(ledPin,LOW); delay(blinkRate); }
  34. Other interfaces • inter-integrated circuit (I2C) • http://arduino.cc/en/reference/wire • serial

    peripheral interface • http://arduino.cc/en/Reference/SPI • one wire • http://playground.arduino.cc/Learning/OneWire