EE 308: Microcontrollers Introduction Aly El-Osery Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA January 6, 2018 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 1 / 23
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. Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 2 / 23
Differences between microcontrollers and microprocessor Microcontroller Microprocessor CPU SRAM EEPROM/ Flash Timer/ Counter CPU SRAM EEPROM/ Flash Timer/ Counter Digital I/O module Serial interface ADC Other peripherals General Purpose Microprocessor Serial interface ADC Digital I/O module Microcontroller Contains a processor Has memory and I/O modules integrated on the same chip Compact Cheap Small power consumption Processor is standalone, memory and I/O modules are seperate Designer can choose components for memory and I/O High processing power General purpose Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 3 / 23
Variety of microcontroller manufacturers 68HC11/12 Freescale Semiconductors s (Formerly Motorola) 8051 Intel AVR Atmel Z8 Zilog PIC Microchip Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 4 / 23
Evaluation boards BigAVR6 Dragon12 Plus ATtiny85 Zilog Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 5 / 23
Embedded systems Control unit performed by a microcontroller is integrated into the system. Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 6 / 23
Mechatronics Integration for mechanical systems with electronics and information processing. Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 7 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 8 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 9 / 23
Von Neumann vs Harvard Von Neumann Harvard Code memory Data memory data bus data bus CPU data bus address bus control bus Code memory control bus CPU control bus Data memory address bus address bus Von Neumann architecture Harvard 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 Separate buses for accessing code and data Faster Less delays Requires more hardware Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 10 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 11 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 12 / 23
Atmega1284 PDIP Package This will be the chip used through out the semester. Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 13 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 14 / 23
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. INCLUDE " M1284DEF. INC " LDI R16, hi8 ( RAMEND ) OUT SPH, R16 LDI R16, lo8 ( RAMEND ) 5 OUT SPL, R16 ;initialize stack pointer SBI DDRC, 0 ;set bit 0 of DDRC HERE : SBI PORTC, 0 10 CALL DELAY ;call DELAY subroutine CBI PORTC, 0 CALL DELAY RJMP HERE 15 DELAY : LDI R20, 255 DL1 : DEC R20 BRNE DL1 RET Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 15 / 23
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. INCLUDE " M1284DEF. INC " LDI R16, hi8 ( RAMEND ) OUT SPH, R16 LDI R16, lo8 ( RAMEND ) 5 OUT SPL, R16 ;initialize stack pointer SBI DDRC, 0 ;set bit 0 of DDRC HERE : SBI PORTC, 0 10 CALL DELAY ;call DELAY subroutine CBI PORTC, 0 CALL DELAY RJMP HERE 15 DELAY : LDI R20, 255 DL1 : DEC R20 BRNE DL1 RET Directive Label Comment Instruction Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 15 / 23
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 Aly 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 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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 17 / 23
Intel Hex vs S19 Intel Hex :1000000000E40EBF0FEF0DBF389A409A0E940C001B :1000100040980E940C00F9CF4FEF4A95F1F70895F0 :00000001FF Motorola S19 S01100006578616D706C65345F332E686578C9 S113000000E40EBF0FEF0DBF389A409A0E940C0017 S113001040980E940C00F9CF4FEF4A95F1F70895EC S9030000FC 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) 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) Aly 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 Aly 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 list you need to consult the documentation but below are some of the frequently used ones. avr-as.equ DDRA, 0x01.asciz hello.section.data.section.text hi8 lo8 avrasm2.equ DDRA = 0x01.db hello,0.dseg.cseg high low Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 20 / 23
Programmer Prototyping area Programming Interfaces SPI High Voltage Serial Parallel JTAG PDI awire Debugging Interfaces JTAG debugwire PDI awire Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 21 / 23
Makefile FILENAME MCU PARTNO ARCH PORT PROGRAMMER AS LD OBJCOPY PROG ASFLAGS LDFLAGS OBJFLAGS PRFLAGS = example = atmega1284 = m1284 = avr51 = usb = dragon_jtag = avr-as = avr-ld = avr-objcopy = avrdude = -mmcu=$(mcu) --gstabs = -m$(arch) = -j.text -j.data -O ihex = -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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 22 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23
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. Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 6, 2018 23 / 23