Lecture Introduction EE 308:

Lecture Notes Update on January 17, 2019

Aly El-Osery, Electrical Engineering Dept., New Mexico Tech

.1

1 Overview

What are microcontrollers? A is a 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.

.2

Differences between microcontrollers and

Microcontroller

EEPROM/ Timer/ CPU SRAM Flash

Digital I/O Serial Other ADC module interface peripherals

Microcontroller

• Contains a processor • Has memory and I/O modules integrated on the same chip • Compact • Cheap • Small power consumption

Microprocessor

EEPROM/ Timer/ CPU SRAM Flash Counter

Serial Digital I/O ADC General Purpose interface module Microprocessor

• Processor is standalone, memory and I/O modules are seperate • Designer can choose components for memory and I/O

1 • High processing power • General purpose .3

Variety of microcontroller manufacturers • 68HC11/12 — Freescale Semiconductors’s (Formerly Motorola) • 8051 — Intel • AVR — • Z8 — Zilog • PIC — Microchip .4

Evaluation boards

BigAVR6 Dragon12 Plus

ATtiny85

Zilog .5

Embedded systems performed by a microcontroller is integrated into the system. .6

Mechatronics Integration for mechanical systems with electronics and information processing. .7

2 Choosing a Microcontroller

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 .8

2 3 Microcontroller Architectures

CISC vs RISC Complex Instruction Set (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 .9

Von Neumann vs Harvard

Von Neumann

Code Data memory memory

data address bus CPU control bus

Von Neumann architecture

• Code and data are accessed through the same bus • Results in a bottle neck since we can’t access code and data at the same time • More delays

Harvard

data bus data bus

Code Data CPU memory control bus control bus memory

address bus address bus

Harvard architecture

• Separate buses for accessing code and data • Faster • Less delays • Requires more hardware .10

4 AVR Microcontroller

AVR Core • RISC with Harvard architecture • Code ROM • Data RAM • Data EEPROM

3 • Timers • I/O ports • ADC • PWM • Communication interfaces: USART, SPI, I2C (TWI), CAN, USB

.11

5 Atmega1284

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

.12

4 Atmega1284 PDIP Package This will be the chip used through out the semester.

.13

6 Assembly vs C

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 .14

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 BRNE DL1 Instruction RET .15

7 Toolchain

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

5 • 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 .16

What to download on the microcontroller • Assembler — replaces mnemonics by their opcodes, resolves include directives, trans- lates 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

.17

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 record 01 end of file • Data (2k characters) • Checksum (2 characters) Motorola S19 S01100006578616D706C65345F332E686578C9 S113000000E40EBF0FEF0DBF389A409A0E940C0017 S113001040980E940C00F9CF4FEF4A95F1F70895EC S9030000FC

• Record type (2 characters) S0 Block header S1-3 Data records S5 Record count S7-9 End of block • Byte count (2 characters) Address + Data + Checksum • Address (4 characters) • Data (2k characters) • Checksum (2 characters) .18

6 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

.19 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 .20

Dragon Board Programmer

• Interfaces – SPI – JTAG – PDI – aWire • Full source-level debugging in Atmel Studio • Supports ARM Cortex .21

Atmel ICE Programmer

• Prototyping area • Programming Interfaces – SPI – High Voltage Serial – Parallel – JTAG – PDI – aWire • Debugging Interfaces – JTAG – debugWIRE – PDI

– aWire .22

7 ArduinoISP Programmer

• Uses ISP interface • Cheap • Board can be used as a regular microcontroller .23

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

.24

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

8 – 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 .25

9