EE 308: Microcontrollers Introduction
Aly El-Osery
Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA
January 17, 2019
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 1 / 25 What are microcontrollers?
A microcontroller is a processor with memory and several other components integrated on a a single chip making it perfect for a variety of applications covering a wide range, e.g., coffee machines, automotive industry, communications, defense systems, satellites, etc.
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 2 / 25 Differences between microcontrollers and microprocessor
Microcontroller Microprocessor
EEPROM/ Timer/ CPU SRAM Flash Counter EEPROM/ Timer/ CPU SRAM Flash Counter
Digital I/O Serial Other Serial Digital I/O ADC ADC module interface peripherals General Purpose interface module Microprocessor Microcontroller
Contains a processor Processor is standalone, memory and I/O Has memory and I/O modules integrated on modules are seperate the same chip Designer can choose components for memory Compact and I/O Cheap High processing power Small power consumption General purpose
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 3 / 25 Variety of microcontroller manufacturers
68HC11/12 — Freescale Semiconductors’s (Formerly Motorola) 8051 — Intel AVR — Atmel Z8 — Zilog PIC — Microchip
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 4 / 25 Evaluation boards
BigAVR6 Dragon12 Plus ATtiny85
Zilog
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 5 / 25 Embedded systems
Control unit performed by a microcontroller is integrated into the system.
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 6 / 25 Mechatronics
Integration for mechanical systems with electronics and information processing.
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 7 / 25 Factors to consider
Capability of meeting the specification of the task at hand Speed Packaging Power consumption Amount of memory Number of I/O pins Upgradability Cost Development tools Availability
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 8 / 25 CISC vs RISC
Complex Instruction Set Computer (CISC) Larger architecture Variable code size Powerful instruction set Requires several clock cycles to execute instructions
Reduced Instruction Set Computer (RISC) Simple architecture Small set of instructions One or few clock cycles to execute instructions
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 9 / 25 Von Neumann vs Harvard
Von Neumann Harvard
Code Data data bus data bus memory memory
Code Data data bus CPU memory control bus control bus memory address bus CPU control bus address bus address bus Harvard architecture Von Neumann architecture
Separate buses for accessing code and Code and data are accessed through the data same bus Faster Results in a bottle neck since we can’t Less delays access code and data at the same time Requires more hardware More delays Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 10 / 25 AVR Core
RISC with Harvard architecture Code ROM Data RAM Data EEPROM Timers I/O ports ADC PWM Communication interfaces: USART, SPI, I2C (TWI), CAN, USB
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 11 / 25 Atmega1284
RISC architecture 131 instructions (most executes in single clock cycle) 128K bytes code ROM 4K bytes EEPROM 16K bytes SRAM JTAG Timers/counters, PWM, 8-channel 10bit ADC Watchdog timer USART, SPI, I2C
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 12 / 25 Atmega1284 PDIP Package
This will be the chip used through out the semester.
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 13 / 25 Reasons for using assembly
More efficient use of computing power and memory Closer to the hardware If mass production is the goal, then there will be money saving by going to smaller cheaper parts
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 14 / 25 Assembly language
Assembly language consists of, instructions referred to as mnemonics, directives and labels. .INCLUDE "M1284DEF.INC" LDI R16, hi8(RAMEND) OUTSPH, R16 LDI R16, lo8(RAMEND) 5 OUTSPL, R16 ;initialize stack pointer
SBIDDRC, 0 ;set bit0 ofDDRC HERE: SBIPORTC, 0 10 CALLDELAY ;callDELAY subroutine CBIPORTC, 0 CALLDELAY RJMPHERE
15 DELAY: LDI R20, 255 DL1 :DEC R20 BRNE DL1 RET
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 15 / 25 Assembly language
Assembly language consists of, instructions referred to as mnemonics, directives and labels. .INCLUDE "M1284DEF.INC" Directive LDI R16, hi8(RAMEND) OUTSPH, R16 LDI R16, lo8(RAMEND) 5 OUTSPL, R16 ;initialize stack pointer
SBIDDRC, 0 ;set bit0 ofDDRC HERE: Label SBIPORTC, 0 10 CALLDELAY ;callDELAY subroutine CBIPORTC, 0 CALLDELAY RJMPHERE Comment
15 DELAY: LDI R20, 255 DL1 :DEC R20 Instruction BRNE DL1 RET
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 15 / 25 Software tools
GNU AVR toolchain (will be used in lectures) Free Open source Linux and Windows (WinAVR) Arduino IDE is based on it Requires a bit more effort to make it work Atmel Studio (mostly used in the lab) Free Supports all their products Runs only on Windows Has and additional assembler (used by default): avrasm2 Can’t handle mixed C and assembly, and relies on GCC to do that
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 16 / 25 What to download on the microcontroller
Assembler — replaces mnemonics by their opcodes, resolves include directives, translates register names to addresses, removes comments, etc. avr-as -mmcu=atmega1284 -o ex.o ex.s
Linker — links multiple files avr-ld -mavr51 -o ex.elf ex.o
Translate object files avr-objcopy -j .text -j .data -O ihex -o ex.hex ex.elf
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 17 / 25 Intel Hex vs S19
Motorola S19 Intel Hex S01100006578616D706C65345F332E686578C9 :1000000000E40EBF0FEF0DBF389A409A0E940C001B S113000000E40EBF0FEF0DBF389A409A0E940C0017 :1000100040980E940C00F9CF4FEF4A95F1F70895F0 S113001040980E940C00F9CF4FEF4A95F1F70895EC :00000001FF S9030000FC
Start code (:) Record type (2 characters) Byte count data only (2 characters) S0 Block header Address (4 characters) S1-3 Data records Record type (2 characters) S5 Record count 00 data record S7-9 End of block 01 end of file Byte count (2 characters) Address + Data + Checksum Data (2k characters) Address (4 characters) Checksum (2 characters) Data (2k characters) Checksum (2 characters)
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 18 / 25 Downloading the hex file
Using Dragon Board avrdude -v -c dragon_jtag -p m1284 -Pusb -U flash:w:ex.hex
Using ArduinoISP avrdude -v -c arduino -p m1284 -Pusb -b 19200 -U flash:w:ex.hex
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 19 / 25 avr-as vs avrasm2
There are differences between the GCC assembler and the one used by Atmel. For full list you need to consult the documentation but below are some of the frequently used ones.
avr-as avrasm2 .equ DDRA, 0x01 .equ DDRA = 0x01 .asciz “hello” .db “hello”,0 .section .data .dseg .section .text .cseg hi8 high lo8 low
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 20 / 25 Dragon Board Programmer
Interfaces SPI JTAG PDI aWire Full source-level debugging in Atmel Studio Supports ARM Cortex
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 21 / 25 Atmel ICE Programmer
Prototyping area Programming Interfaces SPI High Voltage Serial Parallel JTAG PDI aWire Debugging Interfaces JTAG debugWIRE PDI aWire
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 22 / 25 ArduinoISP Programmer
Uses ISP interface Cheap Board can be used as a regular microcontroller
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 23 / 25 Makefile
FILENAME = example
MCU = atmega1284 PARTNO = m1284 ARCH = avr51 PORT = usb PROGRAMMER = dragon_jtag
AS = avr-as LD = avr-ld OBJCOPY = avr-objcopy PROG = avrdude
ASFLAGS = -mmcu=$(MCU) --gstabs LDFLAGS = -m$(ARCH) OBJFLAGS = -j .text -j .data -O ihex PRFLAGS = -v -c $(PROGRAMMER) -p $(PARTNO) -P$(PORT)
.PHONY: all install clean
all: $(FILENAME).hex $(FILENAME).o $(FILENAME).elf
%.o: %.s $(AS) $(ASFLAGS) -o $@ $<
%.elf: %.o $(LD) $(LDFLAGS) -o $@ $<
%.hex: %.elf $(OBJCOPY) $(OBJFLAGS) $< $@
install: $(FILENAME).hex $(PROG) $(PRFLAGS) -U flash:w:$<
clean: rm -f $(FILENAME).hex $(FILENAME).elf $(FILENAME).o
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 24 / 25 As an engineer, specifically an embedded systems engineer, you must understand enough about what you are designing, standards and specifications, and have the ability to validate and verify your design. The Arduino environment may not give you the ability to do so.
Very useful to non-technical users, hobbyists, students (K-12), but highly questionable for critical engineering designs
Arduino environment
Free package Arduino language implemented in C/C++ Lots of libraries A lot of copy/paste Hides a lot of the details and therefore Makes you lazy Optimization requires extensive redesign of code Resulting code may not operate under strict timing, memory, power requirements
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 25 / 25 Very useful to non-technical users, hobbyists, students (K-12), but highly questionable for critical engineering designs
Arduino environment
Free package Arduino language implemented in C/C++ Lots of libraries A lot of copy/paste Hides a lot of the details and therefore Makes you lazy Optimization requires extensive redesign of code Resulting code may not operate under strict timing, memory, power requirements As an engineer, specifically an embedded systems engineer, you must understand enough about what you are designing, standards and specifications, and have the ability to validate and verify your design. The Arduino environment may not give you the ability to do so.
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 25 / 25 Arduino environment
Free package Arduino language implemented in C/C++ Lots of libraries A lot of copy/paste Hides a lot of the details and therefore Makes you lazy Optimization requires extensive redesign of code Resulting code may not operate under strict timing, memory, power requirements As an engineer, specifically an embedded systems engineer, you must understand enough about what you are designing, standards and specifications, and have the ability to validate and verify your design. The Arduino environment may not give you the ability to do so.
Very useful to non-technical users, hobbyists, students (K-12), but highly questionable for critical engineering designs
Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 17, 2019 25 / 25