MICROCONTROLLERS Maurício Féo [email protected] CERN/Nikhef OBJECTIVES Understand what are microcontrollers. What are they? What are they used for? How do they work? How can I use them? Are they suitable for my project? Example of applications. Have an overview of the lab 10. 2 Maurício Féo – ISOTDAQ 2019 WHAT IS A MICROCONTROLLER? Tiny computer integrated in the same chip. Microcontroller CPU Input CPU Output Memories I/O Interfaces Input Output Etc. Input Output Memory 3 Maurício Féo – ISOTDAQ 2019 WHAT IS A MICROCONTROLLER? Tiny computers integrated in a single chip CPU, Memories and Peripherals in the same chip. Main differences w.r.t. a computer: Suitable for embedded applications. Low cost (ATtiny: ~$0.27) Low power consumption (ATtiny43U: 0.15uA in Sleep Mode) Reduced clock frequency (~ dozens of MHz) Stand-alone devices (Some require only power to work) Low level control of your application. 4 Maurício Féo – ISOTDAQ 2019 WHAT ARE THEY USED FOR? Monitoring Data Acqusition Control Applications where: High performance is not required. Other devices are inadequate (overkill) due to: High power consumption need of external memories and peripherals cost etc. 5 Maurício Féo – ISOTDAQ 2019 WHERE ARE THEY USED? Everywhere! Consumer electronics, home appliances, toys, vehicles, computers, hobbyist projects, etc. According to wikipedia trusted sources, a typical mid-range automobile has as many as 30 or more microcontrollers. According to me an even more trusted source, you have at least one in your pocket right now. 6 Maurício Féo – ISOTDAQ 2019 WHERE ARE THEY USED? 7 Maurício Féo – ISOTDAQ 2019 8 Maurício Féo – ISOTDAQ 2019 9 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) The one used on the lab. 10 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) The one used on the lab. 11 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE 8 bits architecture (ATMEGA328P) 32kB Flash program memory 2kB RAM 2kB EEPROM Max 20MHz 6 x PWM 6 x ADC channels (10bits) 23 I/O pins 3 timers (2x8 bits 1x16 bits) The one used on the lab. 1x USART 1x SPI 1x TWI (I2C) 0.6mA/MHz 12 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 13 Maurício Féo – ISOTDAQ 2019 AVR CPU Data Program CPU Memory Harvard Architecture Memory 8 bits architecture (with 16bits for instructions) Instructions executed 8 bits by 8 bits Reduced Instruction Set Computing (RISC) (~130 instructions) Up to 20 MIPS at 20 MHz (1 instruction / clock cycle) 14 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 15 Maurício Féo – ISOTDAQ 2019 GENERAL PURPOSE INPUT/OUTPUT (GPIO) Pins programmable as Input and Output DDR reg D Read / Write digital signals A ‘0’ = 0V (Gnd), ‘1’ = 5V (Vcc) T A PORT reg Pin Controlled by 3 registers: B U DDR (Data Direction Register) PIN reg S PORT (Where you write when it’s output) PIN (Where you read when it’s input) 16 Maurício Féo – ISOTDAQ 2019 INTERRUPT Interrupts break the program Main program flow to handle some event. instruction 1 instruction 2 ISR() It may be triggered by: instruction 3 inst 1 Pin change (rise/fall/toggle) instruction 4 Timers / Counters inst 2 instruction 5 Analog Comparator inst 3 instruction 6 ADC reading done inst 4 instruction 7 Serial interfaces (Rx/Tx done) instruction 8 It allows the program to handle instruction 9 an event "right after" its occurrence, regardless of where the program is and without the need of polling constantly. 17 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 18 Maurício Féo – ISOTDAQ 2019 TIMERS / COUNTERS Internal registers that increment triggered by: A clock source: Timer An external event: Counter May be used to: Measure time Raise interruption on: Overflow Ultrasonic distance sensor Reach a certain value (OCR) Measures distance based on the time to echo of an Create waveform ultrasonic pulse. PWM 19 Maurício Féo – ISOTDAQ 2019 PULSE WIDTH MODULATION (PWM) You can create an output signal which value depends on the status of the timer. Outputs a train of periodic digital pulses with controlled width. (Can be used to "mimic" an analog signal) 191 (75%) 75% 25% 20 Maurício Féo – ISOTDAQ 2019 PULSE WIDTH MODULATION (PWM) You can create an output signal which value depends on the status of the timer. Outputs a train of periodic digital pulses with controlled width. (Can be used to "mimic" an analog signal) 21 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 22 Maurício Féo – ISOTDAQ 2019 ANALOG COMPARATOR Tells whether positive pin AIN0 voltage is higher than negative pin AIN1. Output is the internal bit ACO* of reg ACSR*. V1 > V2 Vout = 1 V1 < V2 Vout = 0 Can be used to: Compare two analog signals Trigger a Timer/Counter Trigger an interrupt (rise, fall or toggle) * ACO = Analog Comparator Output ACSR = Analog Comp. Control and Status Register 23 Maurício Féo – ISOTDAQ 2019 ANALOG TO DIGITAL CONVERTER (ADC) 10-bit resolution 0V-Vref → 0-1023 Vref can be: Vcc (Power source) 1.1V internal ref. (from bandgap) External ref. on pin ‘AREF’ Successive approximation 13-260 us Conversion time Interrupt on complete 6 multiplexed channels on DIP package (internal Temp sensor on ch8) 24 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 25 Maurício Féo – ISOTDAQ 2019 SERIAL INTERFACES: USART UNIVERSAL SYNCHRONOUS-ASYNCHRONOUS RECEIVER TRANSMITTER A simple protocol Widely used to communicate with PCs due to compability with RS232 protocol. (RS232 is not used anymore in most PCs but it’s still very easy to find USB-Serial converters) Up to 250kbps May trigger interrupts: Tx complete Rx complete Data reg empty 26 Maurício Féo – ISOTDAQ 2019 SERIAL INTERFACES: SPI SERIAL PERIPHERAL INTERFACE Differently from the USART, SPI can talk to multiple devices on the same bus, but needs a Slave Select signal per Slave Device Up to 10Mbps! (clk/2) Slaves do not "talk" autonomously. Must be querried (and clocked) by master. All SD cards Uses the SPI serial interface and can be easily accessed from a uC. 27 Maurício Féo – ISOTDAQ 2018 Maurício Féo – ISOTDAQ 2019 SERIAL INTERFACES: TWI (I2C) TWO WIRE INTERFACE (INTER-INTEGRATED CIRCUIT) I2C allows multiple Masters and Slaves on the same bus. (up to 128) Up to 400kbps (on the Atmega328) Used in a variety of digital sensors. 28 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 29 Maurício Féo – ISOTDAQ 2019 WATCHDOG TIMER (WDT) A Watchdog Timer is a timer clocked by an on-chip oscillator Once the counter reaches a certain value, the microcontroller may: Trigger an interrupt Reset the microcontroller Used to prevent your program from getting stuck in any part of the code. You use it by enabling the WDT and spreading WDT reset instructions on particular places of your code. If it gets stuck in an infinite loop, for ex., the counter won’t be reset and the microcontroller will be reset. 30 Maurício Féo – ISOTDAQ 2019 CLOCK CIRCUIT Up to 20MHz from: External clock from a pin External crystal oscillator Internal RC oscillator 7.3-8.1 MHz 128kHz Internal oscillator 128 kHz System Clock Prescaler Divides the clock if needed Keep in mind: Power consumption is proportional to clock frequency. 31 Maurício Féo – ISOTDAQ 2019 AVR ARCHITECTURE (ATMEGA328P) 32 Maurício Féo – ISOTDAQ 2019 SLEEP MODES There are multiples Sleep Modes available. Each turns off certain parts of the microcontroller to save power and can only be waken up by certain sources. 33 Maurício Féo – ISOTDAQ 2019 POWER AND DEBUG Brown-Out Detector (BOD) Resets the device whenever Vcc is below a certain threshold. Power-on Reset (POR) Ensures the device is reset from Power On. DebugWIRE On-chip debug tool from AVR. 34 Maurício Féo – ISOTDAQ 2019 REVIEW 35 Maurício Féo – ISOTDAQ 2019 ATMEGA328 MINIMUM REQUIRED CIRCUIT NEEDED CIRCUITRY: USING AN EXTERNAL CRYSTAL 36 Maurício Féo – ISOTDAQ 2019 ATMEGA328 MINIMUM REQUIRED CIRCUIT NEEDED CIRCUITRY: USING THE INTERNAL OSCILLATOR Atmega328 comes with an internal 8MHz oscillator that can minimize the required circuit to a single battery. 37 Maurício Féo – ISOTDAQ 2019 USAGE OF MICROCONTROLLERS DEVELOPMENT CYCLE Write your code. From Assembly to C. Or even higher level: Compile it. (debug) Upload to the uC memory (On Chip debug) Parallel programming Serial downloading (SPI) Self-programming (With bootloader) (Burn the fuses) 38 Maurício Féo – ISOTDAQ 2019 SELF-PROGRAMMING: BOOTLOADER The AVR core can write to it’s own program memory. The Bootloader Section can be locked from rewritting. This way developers can allow users to write their own programs without compromising the bootloader section. This can also be used to ease the programming of the memory, eliminating the need for an external programmer. USB-Serial 39 Maurício Féo – ISOTDAQ 2019 ARDUINO 40 Maurício Féo – ISOTDAQ 2019 ARDUINO Open-source platform created for "makers" who have no knowledge in electronics but still want their creations to interact with the environment. Custom IDE + libraries Inexpensive and really easy to use (Almost plug and play) Huge community all over the world creating libraries, compatible hardware and sharing projects Stackable addons called shields 41 Maurício Féo – ISOTDAQ 2019 SETTING PWM TO 25% DUTY CYCLE THE MEDIEVAL ORIGINAL WAY Registers: 25% 75% DDR = Data Direction Register TTCR0A = Timer/Counter Control Register A TTCR0B = Timer/Counter Control Register B OCR0A = Output Compare Register 0 A Set direction of pin 6 from PORTD to output. Set WGM to Fast PWM. Set COM to «Clear On Compare». Set OCR to 25% (0x3F of 0xFF) Set the clock source to timer. Bits: WGM0[1..0] = Waveform Generator Mode 0 COM0A[1..0] = Compare Output Mode 0 A CS0[2..0] = Clock Select 0 42 Maurício Féo – ISOTDAQ 2019 SETTING PWM TO 25% DUTY CYCLE THE CHEATING ARDUINO WAY 25% 75% Look at the board which pin you want to use. 25 푥 = Find x.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages65 Page
-
File Size-