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

Post on 28-Dec-2015

222 views 1 download

Tags:

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

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?

ArchitectureVon Neumann vs. Harvard

Von Neumann:

• Program and data memory share the same internal bus.

• Reduces space and complexity?

• But reduce instruction throughput

ArchitectureVon Neumann vs. Harvard

Harvard:

• One data bus for data and one for program memory

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

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

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)

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)

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

ArchitectureProgram Counter (PC)

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

ArchitectureSTATUS flags

• Status flags depends on the MCU

• Are used to evaluate results from instructions

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

PIC Instructions

Timings

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

• 1 instruction 1 cycle

• Instructions that change the PC takes 2cy

Data Memory

RAM• General purpose• Special Function

ROM• EEPROM memory

(Does often need to go trough RAM to access)

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

Interrupts

InterruptsWhy use?

• Better use of CPU resource

• Faster response time

• Multi-tasking

• Fixed or known interval handler

• Exception handler

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

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?

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

Watchdog timer

• Resets the processor when it overflows

• Needs to be reset continuously

• Good to prevent crashes

Peripherals

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

Peripherals

• Used to relieve the CPU from unnecessary processing

• Peripherals are interrupt sources but,

• can be more convenient to poll the int. flag

Assembler

• Midrange PIC MCU - 35 single word instructions

• Enhanced PIC MCU - ~60 instructions

mostly single word

• Atmel AVR - ~120 instructions mostly single word

AssemblerPIC16 Instructions

The instruction set is divided into 3 categories

• Byte-oriented

• Bit-oriented

• Literal and control operations

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;

}

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

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

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

JEDEC

Joint Electron Device Engineering Council

• Physical standards (chip manufacturers)

• Size and shape of devices

• Quality specifications

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?

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.