ee 308: microcontrollers - introductionelosery/spring_2018/ee308/lectures/intro.pres.pdf · ee 308:...

26
EE 308: Microcontrollers Introduction Aly El-Osery Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA January 6, 2018 Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C Toolchain Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 1 / 23

Upload: hoangcong

Post on 11-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

EE 308: MicrocontrollersIntroduction

Aly El-Osery

Electrical Engineering DepartmentNew Mexico Institute of Mining and Technology

Socorro, New Mexico, USA

January 6, 2018

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 1 / 23

What are microcontrollers?

A microcontroller is a processor with memory and severalother components integrated on a a single chip making itperfect 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 ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 2 / 23

Differences between microcontrollers and microprocessor

Microcontroller

CPU SRAMEEPROM/

FlashTimer/

Counter

Digital I/Omodule

Serialinterface ADC Other

peripherals

Microcontroller

Contains a processorHas memory and I/O modules integrated onthe same chipCompactCheapSmall power consumption

Microprocessor

CPU SRAMEEPROM/

FlashTimer/

Counter

Serialinterface ADC Digital I/O

moduleGeneral PurposeMicroprocessor

Processor is standalone, memory and I/Omodules are seperateDesigner can choose components for memoryand I/OHigh processing powerGeneral purpose

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 3 / 23

Variety of microcontroller manufacturers

68HC11/12 — Freescale Semiconductors’s (Formerly Motorola)8051 — IntelAVR — AtmelZ8 — ZilogPIC — Microchip

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 4 / 23

Evaluation boards

BigAVR6

ATtiny85

Dragon12 Plus

Zilog

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 5 / 23

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 ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 6 / 23

Mechatronics

Integration for mechanical systems with electronics and information processing.

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 7 / 23

Factors to consider

Capability of meeting the specification of the task at handSpeedPackagingPower consumptionAmount of memoryNumber of I/O pinsUpgradabilityCost

Development toolsAvailability

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 8 / 23

CISC vs RISC

Complex Instruction Set Computer (CISC)Larger architectureVariable code sizePowerful instruction setRequires several clock cycles to execute instructions

Reduced Instruction Set Computer (RISC)Simple architectureSmall set of instructionsOne or few clock cycles to execute instructions

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 9 / 23

Von Neumann vs Harvard

Von Neumann

CPU

Codememory

Datamemory

Von Neumann architecture

data bus

address bus

control bus

Code and data are accessed through thesame busResults in a bottle neck since we can’taccess code and data at the same timeMore delays

Harvard

CPUCode

memoryData

memory

address bus

data bus

control bus

address bus

data bus

control bus

Harvard architecture

Separate buses for accessing code anddataFasterLess delaysRequires more hardware

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 10 / 23

AVR Core

RISC with Harvard architectureCode ROMData RAMData EEPROMTimersI/O portsADCPWMCommunication interfaces: USART, SPI,I2C (TWI), CAN, USB

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 11 / 23

Atmega1284

RISC architecture131 instructions (most executes insingle clock cycle)128K bytes code ROM4K bytes EEPROM16K bytes SRAMJTAGTimers/counters, PWM, 8-channel10bit ADCWatchdog timerUSART, SPI, I2C

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 12 / 23

Atmega1284 PDIP Package

This will be the chip usedthrough out the semester.

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 13 / 23

Reasons for using assembly

More efficient use of computing power and memoryCloser to the hardwareIf mass production is the goal, then there will be money saving by going to smallercheaper parts

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 14 / 23

Assembly language

Assembly language consists of, instructions referred to as mnemonics, directives andlabels.

.INCLUDE "M1284DEF.INC"LDI R16 , hi8(RAMEND)OUT SPH , R16LDI R16 , lo8(RAMEND)

5 OUT SPL , R16 ;initialize stack pointer

SBI DDRC , 0 ;set bit 0 of DDRCHERE:

SBI PORTC , 010 CALL DELAY ;call DELAY subroutine

CBI PORTC , 0CALL DELAYRJMP HERE

15 DELAY:LDI R20 , 255

DL1: DEC R20BRNE DL1RET

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 15 / 23

Assembly language

Assembly language consists of, instructions referred to as mnemonics, directives andlabels.

.INCLUDE "M1284DEF.INC"LDI R16 , hi8(RAMEND)OUT SPH , R16LDI R16 , lo8(RAMEND)

5 OUT SPL , R16 ;initialize stack pointer

SBI DDRC , 0 ;set bit 0 of DDRCHERE:

SBI PORTC , 010 CALL DELAY ;call DELAY subroutine

CBI PORTC , 0CALL DELAYRJMP HERE

15 DELAY:LDI R20 , 255

DL1: DEC R20BRNE DL1RET

Directive

Label

Instruction

Comment

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 15 / 23

Software tools

GNU AVR toolchain (will be used in lectures)FreeOpen sourceLinux and Windows (WinAVR)Arduino IDE is based on itRequires a bit more effort to make it work

Atmel Studio (mostly used in the lab)FreeSupports all their productsRuns only on WindowsHas and additional assembler (used by default): avrasm2Can’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 ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 16 / 23

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 filesavr-ld -mavr51 -o ex.elf ex.o

Translate object filesavr-objcopy -j .text -j .data -O ihex -o ex.hex ex.elf

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 17 / 23

Intel Hex vs S19

Intel Hex

:1000000000E40EBF0FEF0DBF389A409A0E940C001B:1000100040980E940C00F9CF4FEF4A95F1F70895F0:00000001FF

Start code (:)Byte count data only (2 characters)Address (4 characters)Record type (2 characters)00 data record01 end of file

Data (2k characters)Checksum (2 characters)

Motorola S19

S01100006578616D706C65345F332E686578C9S113000000E40EBF0FEF0DBF389A409A0E940C0017S113001040980E940C00F9CF4FEF4A95F1F70895ECS9030000FC

Record type (2 characters)S0 Block headerS1-3 Data recordsS5 Record countS7-9 End of block

Byte count (2 characters) Address + Data + ChecksumAddress (4 characters)Data (2k characters)Checksum (2 characters)

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 18 / 23

Downloading the hex file

avrdude -v -c dragon_jtag -p m1284 -Pusb -U flash:w:ex.hex

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 19 / 23

avr-as vs avrasm2

There are differences between the GCC assembler and the one used by Atmel. For full listyou 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 .cseghi8 highlo8 low

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 20 / 23

Programmer

Prototyping areaProgramming Interfaces

SPIHigh Voltage SerialParallelJTAGPDIaWire

Debugging InterfacesJTAGdebugWIREPDIaWire

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 21 / 23

Makefile

FILENAME = example

MCU = atmega1284PARTNO = m1284ARCH = avr51PORT = usbPROGRAMMER = dragon_jtag

AS = avr-asLD = avr-ldOBJCOPY = avr-objcopyPROG = avrdude

ASFLAGS = -mmcu=$(MCU) --gstabsLDFLAGS = -m$(ARCH)OBJFLAGS = -j .text -j .data -O ihexPRFLAGS = -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 ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 22 / 23

Arduino environment

Free packageArduino language implemented in C/C++Lots of librariesA lot of copy/pasteHides a lot of the details and therefore

Makes you lazyOptimization requires extensive redesign of codeResulting code may not operate under strict timing, memory, power requirements

As an engineer, specifically an embedded systems engineer, you must understand enough aboutwhat you are designing, standards and specifications, and have the ability to validate and verifyyour 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 forcritical engineering designs

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23

Arduino environment

Free packageArduino language implemented in C/C++Lots of librariesA lot of copy/pasteHides a lot of the details and therefore

Makes you lazyOptimization requires extensive redesign of codeResulting code may not operate under strict timing, memory, power requirements

As an engineer, specifically an embedded systems engineer, you must understand enough aboutwhat you are designing, standards and specifications, and have the ability to validate and verifyyour 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 forcritical engineering designs

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23

Arduino environment

Free packageArduino language implemented in C/C++Lots of librariesA lot of copy/pasteHides a lot of the details and therefore

Makes you lazyOptimization requires extensive redesign of codeResulting code may not operate under strict timing, memory, power requirements

As an engineer, specifically an embedded systems engineer, you must understand enough aboutwhat you are designing, standards and specifications, and have the ability to validate and verifyyour 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 forcritical engineering designs

Overview Choosing a Microcontroller Microcontroller Architectures AVR Microcontroller Atmega1284 Assembly vs C ToolchainAly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23