introduction to microcontrollers -...

33
Introduction to Microcontrollers ECE Senior Design 15 September 2015

Upload: ngokien

Post on 11-Jul-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Introduction to

Microcontrollers

ECE Senior Design

15 September 2015

Page 2: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Popular Microcontrollers

• 8051 – Intel then Everyone (8-bit)

• PIC – Microchip (8, 16 & 32bit)

• AVR – Atmel (8 & 32bit)

• MSP430 – TI (low power 16-bit)

• ARM Cortex – ARM Limited (32 bit)

• 68HCSxx – Motorola / freescale

• PSoC – Cypress (M8C, 8051,ARM)

• Nios II / MicroBlaze – Altera / Xilinx

Page 3: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Microcontroller Application

Requirements

• Microcontroller and Support Circuitry

• Power Supply

• Application Software

• User Input/Output and Device Interfaces

Page 4: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Features found in Microcontrollers

• Central Processor (8, 16 or 32bit)

• Reset Control (Power-on, Brown out, Watchdog, Stack Overflow)

• Internal Oscillators and PLLs (2-Speed CLK, RTC)

• Program Memory (Factory Mask, OTP, EPROM, Flash)

• Static RAM Memory (Volatile Data Storage)

• EEPROM Memory (Non-Volatile User Configuration / Calibration)

• GPIO Pins

• Timers

• Interrupts (Internal, External)

• Parallel Bus Interfaces

• Serial Interfaces (UART, I2C, SPI, USB)

• Analog input and output (ADC, DAC, PWM, Comp, Op Amp, Cap Sense)

• Power Management

• In-Circuit Programming and Debug (self reprogramming)

Page 5: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Harvard vs. Von Neumann

Harvard architecture

• Separate busses for program

memory and data memory

• Improved operating bandwidth

• Allows for different bus widths

• Used in many microcontrollers

Von Neumann architecture

• Program and data stored in same

memory

• Used in many microprocessors

Von Neumann

Architecture

8-bit

Bus

CPU

Program

& Data

Memory

CPU

Harvard

Architecture

Data

Memory

Program

Memory

8-bit

Bus

14-bit

Bus

Page 6: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

RISC vs. CISC

RISC – Reduced Instruction Set Computer

• Emphasis on Software

• Most instructions execute in a single clock cycle

• Larger code sizes

CISC – Complex Instruction Set Computer

• Emphasis on Hardware

• Instructions require multiple clock cycles

• Smaller code size

Page 7: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

What the does PIC stand for anyway?

PIC – “Peripheral Interface Controller”

The original PIC was

designed to be a

Peripheral Interface

Controller for 6502

microcontroller from

Rockwell late 70's.

Page 8: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Why did we choose Microchip

PIC Family of Microcontrollers?

• Free development software MPLAB IDE (Includes a Simulator)

• Low cost development hardware

• Devices are easy to obtain through distributors and can be sampled

• A wide range of devices are available with varying feature sets

• Microchip is in continuous development of new PIC devices

• Has a large online support community

• Wide acceptance in industry over 12 Billion units shipped

Page 9: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Enhanced Midrange PIC Busses

(PIC12F1822)

CPU

Program

Memory

Data Memory

and

Special Function

Registers

Data

8

12

14

15

RegisterAddress

Instruction

ProgramAddress

15-bit Program Address bus:capable of addressing 32k of program

memory (PIC12F1822 has 2k of program

memory)

14-bit Instruction bus:contains the opcode and the operands

8-bit Register Address:PIC12F1822

128 Bytes of SRAM

256 Bytes EEPROM

8-bit Data bus:

read and write registers

Page 10: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Datasheet Feature Page 12F1822

• Datasheet PIC12F1822

• Processor Structure of the PIC12F1822

• Device Pins for the PIC12F1822

Page 11: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Microcontroller Block Diagram

Page 12: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

I/0 Pins• Up to 6 GPIO Pins on PIC12F1822 (1 input only)

• The Pin Direction is Set with the TRISA Register

• Reading and Writing to the Port is handled in the PORTA

LATA Registers

RA0

AN0

DACOUT

CPS0

C1IN+

P1B

TX

SDO

ICSPDAT

RA1

AN1

VREF

CPS1

C1IN0-

SRI

RX

SCL

ICSPCLK

RA2

AN2

CPS2

C1OUT

T0CLKI

CCP1

INT

IOC

WPU

RA3

T1G

nSS

IOC

nMCLR

VPP

RA4

AN3

CPS3

C1IN1-

T1OSO

P1B

CK

SDO

IOC

RA5

SRNQ

T1CLKI

CCP1

RX

IOC

WPU

CLKIN

Page 13: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Generic Port Structure

Page 14: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

TRISIA Register

Page 15: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

LATA Register

Page 16: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

PORTA Register

Page 17: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Lighting an LED

Page 18: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Program Memory

Map and Stack

• Reset Vector - 0000h

• Interrupt Vector - 0004h

• Program Memory - 2048

• Stack Depth - 16

Page 19: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Data Memory (32 Banks total)

Page 20: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

The “W” Register (working register) movf reg1, w ; Move a file registers contents into the W register

addwf reg2, w ; Place result in W register

Program

Memory

Instruction

Register

Instruction

Decode

and

Control

Program Counter

File

Registers

W Register

ALU

Data Bus8Direct Addr.

7

13

Program Address

14Instruction

8

Page 21: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

The Status Register

Program

Memory

Instruction

Register

Instruction

Decode

and

Control

Program Counter

File

Registers

W Register

ALU

Data Bus8Direct Addr.

7

13

Program Address

14Instruction

8

Status

Register

3result

Page 22: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

The Status bits

• Arithmetic Status of the ALU

• Reset Status WDT and PD

Page 23: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Pipelining of Instructions

• Most instructions Execute in a single Processor Cycle

• A Processor Cycle is 4 Clock Cycles

• Using a 4MHz oscillator each processor cycle is 1MHz

• This results in a 1us execution time

Page 24: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Non-Sequential Address Read

• Exception to the one processor cycle per instruction is for

Branching operations

• The Pipeline will be broken on a non-sequential address

read

• Branching instructions require two processor cycles to

execute

Page 25: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch

T0

Instruction Cycles

movlw 0x05 -

Pre-Fetched Instruction Executing Instruction

Page 26: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0 T1

Instruction Cycles

Fetch

movwf REG1 movlw 0x05

Pre-Fetched Instruction Executing Instruction

Page 27: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0 T1 T2

Instruction Cycles

Fetch Execute

Fetch

call SUB1 movwf REG1

Pre-Fetched Instruction Executing Instruction

Time to execute normal instruction

Page 28: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0 T1 T2 T3

Instruction Cycles

Fetch Execute

Fetch Execute

Fetch

addwf REG2 call SUB1

Pre-Fetched Instruction Executing Instruction

Page 29: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0

Flush

T1 T2 T3 T4

Instruction Cycles

Fetch Execute

Fetch Execute

Fetch

Fetch

movf PORTB,w call SUB1

Pre-Fetched Instruction Executing Instruction

Time to execute call

instruction includes

pipeline flush

Page 30: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0

Flush

T1 T2 T3 T4 T5

Instruction Cycles

Fetch Execute

Fetch Execute

Fetch

Fetch Execute

Fetch

return movf PORTB,w

Pre-Fetched Instruction Executing Instruction

Page 31: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0

Flush

T1 T2 T3 T4 T5

Instruction Cycles

T6

Fetch Execute

Fetch Execute

Fetch

Fetch Execute

Fetch Execute

Fetch

movf PORTC,w return

Pre-Fetched Instruction Executing Instruction

Page 32: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Instruction Pipelining

call SUB1

addwf REG2

movf PORTB,w

return

movf PORTC,w

return

SUB1

SUB2

movlw 0x05MAIN

movwf REG1

1

2

3

4

51

52

53

54

Example ProgramFetch Execute

T0

Flush

T1 T2 T3 T4 T5

Instruction Cycles

T6

Fetch Execute

Fetch Execute

Fetch

Fetch Execute

Fetch Execute

Fetch Flush

Fetch

T7

addwf REG2 return

Pre-Fetched Instruction Executing Instruction

Page 33: Introduction to Microcontrollers - SeniorDesignLabseniordesignlab.com/sdl_docs/Proj_Fall_15/Microcontrollers and the... · Why did we choose Microchip PIC Family of Microcontrollers?

Blinking an LED

(loop method)

Initialize

Main

Call Count_Time

Call Toggle_LED

Goto Main

Count_Time

return

Kill some time

Looping in here

ToggleLED

return

Change state

of LED