Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set...

35
Microcontrollers An introduction

Transcript of Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set...

Page 1: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Microcontrollers

An introduction

Page 2: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Outline

• Architecture

• Timers

• Other Peripherals

• Instruction set

• Design tips

Page 3: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

What is an MCU

• A simple (cheap) processor with everything on a single chip?

• Program memory (hard disc)

• Data memory (RAM, ROM)

• Hardware peripherals

Microcontroller vs. Microprocessor?

Page 4: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureVon Neumann vs. Harvard

Von Neumann:

• Program and data memory share the same internal bus.

• Reduces space and complexity?

• But reduce instruction throughput

Page 5: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureVon Neumann vs. Harvard

Harvard:

• One data bus for data and one for program memory

Page 6: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureHarvard vs. Von Neumann

• Harvard can’t use self-modifying code

• Harvard allows two simultaneous memory fetches

• Most DSPs use Harvard architecture for streaming data:– greater memory bandwidth– more predictable bandwidth

Page 7: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Architecture

Complex Instruction Set Computer (CISC)• Many instructions (~700 for an Intel x86 or ~200

for Motorola HC08)• Very complex (powerful instructions)• Argument: Fever machine instructions implies

shorter execution times• Execution time varies hard to predict timings• Compilers have a hard time using all instructions

efficiently

Page 8: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Architecture

Reduced Instruction Set Computer (RISC)

• Fever instructions (~30-100 MCHIP PIC or Atmel AVR)

• Easier to learn just a few instructions and to write optimizing compilers

• Less silicon space (leaves room for other stuff)

Page 9: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureCISC vs. RISC

The Motorola instruction:

MOV Ms, Md

takes 5 cycles to execute and is equivalent to the Microchip instructions:

movf Ms, W

movwf Md

which takes 2 cycles (1 instruction 1 cycle)

Page 10: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.
Page 11: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.
Page 12: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureWreg/Accumulator

• The data has to go trough the Wreg (with a few exceptions)

c = b + a; a += 5;

movfa, W movlw 5

addwf b, W addwf a, F

movwf c

Page 13: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureProgram Counter (PC)

Counter that points to the program memory address that is to be fetched and executed.

Page 14: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

ArchitectureSTATUS flags

• Status flags depends on the MCU

• Are used to evaluate results from instructions

• Most common flags: Z (Zero) and C (Carry)

Page 15: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

PIC Instructions

Timings

• Tcy = 1 / fcy, fcy = fosc / 4

• 1 instruction 1 cycle

• Instructions that change the PC takes 2cy

Page 16: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Data Memory

RAM• General purpose• Special Function

ROM• EEPROM memory

(Does often need to go trough RAM to access)

Page 17: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Interrupts

• Features in (almost) all MCU:s

• Push PC+1 onto TOS and jumps to the interrupt vector (0x04 for PIC:s)

• Single Int. vectors preserves simplicity

• Multiple Int. vectors can reduce code size and prioritize interrupts

Page 18: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Interrupts

Page 19: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

InterruptsWhy use?

• Better use of CPU resource

• Faster response time

• Multi-tasking

• Fixed or known interval handler

• Exception handler

Page 20: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Timers

• Up or Down Counters dependent on vendor

• Issues an output pulse flag upon under/overflow

• Prescalers and postscalers are used for longer output periods

Page 21: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Timers

Example:

Calculate reload and the prescaler for Timer0 in a PIC16F877A to interrupt every 0.05s, 4MHz x-tal

Prescale value: 256xPrescale > 50 000 Prescaler = 256

Reload value is 50 000 / 256 = 195 Interrupt every 195x256us = 0.4992s

Try the simulator to test the timings

Good or bad result?

Page 22: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Timers

• Better accuracy if the timer doesn’t have to be reloaded

• Better resolution with lower prescaler

• Can be better to use external clock source

Page 23: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Watchdog timer

• Resets the processor when it overflows

• Needs to be reset continuously

• Good to prevent crashes

Page 24: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Peripherals

• Timers• ADC• UART• SPI• I2C• CAN• Comparators• Etc.

Page 25: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Peripherals

• Used to relieve the CPU from unnecessary processing

• Peripherals are interrupt sources but,

• can be more convenient to poll the int. flag

Page 26: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Assembler

• Midrange PIC MCU - 35 single word instructions

• Enhanced PIC MCU - ~60 instructions

mostly single word

• Atmel AVR - ~120 instructions mostly single word

Page 27: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

AssemblerPIC16 Instructions

The instruction set is divided into 3 categories

• Byte-oriented

• Bit-oriented

• Literal and control operations

Page 28: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Assembler

Example:Translate the following C code into assembler:Unsigned int var, var2;while(1){

if(var < 1000) //1000 = 0x3E8var2 = 0;

else if(var == 1000)var2 = 500; //500 = 0x1F4

elsevar2 = 10;

}

Page 29: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Code tips

• Use if, else if, else if, rather than switch

• Play with the compiler optimizations

• Use appropriate data types

• If “true-locals” then try static

• Use one and only one WDT Reset!!

• Use interrupts

• Read the F***ING manual

Page 30: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Design tips

• Don’t trust auto routing blindly• A track under a X-tal is not a good route• Avoid relays and large transistors• ESD and transients cause damage!• ESD and transients cause Latch-up!• Cap values given for X-tals is only for guidance,

it is not the law• Do not Re-invent the wheel!• Find the best tool for the job

Page 31: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

IEEE

Institute of Electrical and Electronics Engineers (Eye-Triple-E)

• Approves and develops standards

• Most importantly standards for how devices interact with each other

• Prevents duplication and overlaps of effort

Page 32: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

JEDEC

Joint Electron Device Engineering Council

• Physical standards (chip manufacturers)

• Size and shape of devices

• Quality specifications

Page 34: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Voluntary assignment

Download the datasheet for a PIC16F877A and write some C or assembler code that implements a watch with a resolution of 1/100s. The processor frequency is 4MHz i.e. the timer increments every us if the source is internal. The variables h, min, s & hundredths shall be updated.

Hint: Use Timer2 to get a perfect interrupt frequency. How can the PR2 register be used in collaboration with pre/postscalers?

Page 35: Microcontrollers An introduction. Outline Architecture Timers Other Peripherals Instruction set Design tips.

Regeardin Re-Inventing the wheel:

I forgot to mention that the manufacturers wants to sell as many parts as possible so they have to show how simple their parts is. This means that almost all manufacturers have published some really good “Application Notes” on their websites, the contents of these app. notes varies from how to write good code for their parts and how to design their PCB:s how to complete design solutions so a good tip is to browse through websites and see if you can find a document that has partly or fully solved your problem.