mcu mcu micro controller unit 23/11/2005 – 14/12/2005 andrea aizza
TRANSCRIPT
MCU
MCUMCUMicro Controller UnitMicro Controller Unit
23/11/2005 – 14/12/2005
Andrea Aizza
A. Aizza - 23/11/05 - 2 -
MCU
Lezione 1
Introduzione ai microcontrollori MCU Microchip PIC16F877
Caratteristiche (prima parte) Set di istruzioni (prima parte)
Pratica: come iniziare Hardware: configurazione ‘minima’ Software: ‘Hello World’ Tools: programmatore
ContenutiContenuti
A. Aizza - 23/11/05 - 3 -
MCU
Lezione 2
PIC16F877 Caratteristiche (seconda parte) Set di istruzioni (seconda parte) Semplici tecniche di codifica
Tools: Tools: ICD – In Circuit Debugger Compilatore ANSI C per PIC18
ContenutiContenuti
A. Aizza - 23/11/05 - 4 -
MCU
Lezione 3
Pratica: esperienza di laboratorio LED + pulsante LED + pulsante + anti-rimbalzo (hardware/software)
Misuratore di tensione con indicatore a LED Trasmissione su porta seriale
ContenutiContenuti
A. Aizza - 23/11/05 - 5 -
MCU Introduzione ai microcontrolloriIntroduzione ai microcontrollori
Microcontrollers:- Use internal RAM and ROM, so the external busses are available for general
purpose I/O. Each bit can be assigned as an input or output.- Are generally used in very simple or lowcost designs.- Often have special ports: A/D converters, PWM outputs, USART, ...and so
on.
Microprocessors:- Use external RAM and ROM and require address and data busses to access
them.- Are used for designs that require more I/O and memory.
A. Aizza - 23/11/05 - 6 -
MCU MCU MicrochipMCU Microchip
Harvard ArchitectureHarvard Architecture
Instruction PipeliningInstruction Pipelining
Long Word InstructionsLong Word Instructions
Single Cycle InstructionsSingle Cycle Instructions
Single Word InstructionsSingle Word Instructions
Large Register FileLarge Register File
Reduced Instruction Set Reduced Instruction Set
Orthogonal Instruction SetOrthogonal Instruction Set
A. Aizza - 23/11/05 - 7 -
MCU MCU Microchip - Architecture MCU Microchip - Architecture
• Traditional Von Neumann Architecture:– Fetches instructions and data
from a single memory space– Limits operating bandwidth
• Harvard Architecture:– Uses two separate memory
spaces for program instructions and data
– Improved operating bandwidth
– Allows for different bus widths
Von NeumannArchitecture
8-bit Bus
CPU
Program& DataMemory
CPU
HarvardArchitecture
DataMemory
ProgramMemory
8-bit Bus
14-bit BusX12 = PIC16C5x, PIC12C5xxX12 = PIC16C5x, PIC12C5xxx14 = PIC16Cxxx, PIC14Cxxx, PIC12C6xxx14 = PIC16Cxxx, PIC14Cxxx, PIC12C6xxx16 = PIC17Cxxxx16 = PIC17Cxxxx16 Enhanced = PIC18Cxxxx16 Enhanced = PIC18Cxxx
A. Aizza - 23/11/05 - 8 -
MCU MCU Microchip - PipeliningMCU Microchip - Pipelining
• In most micro controllers, instructions are fetched and executed
sequentially
• The pipelined architecture overlaps fetch and execution, making single
cycle execution possible
TCY=0 TCY=1 TCY=2 TCY=3 TCY=4 TCY=5Instruction Cycles :
Fetch 1 Execute 1 M OVLW 0x55
M OVW F PORTB Fetch 2 Execute 2
Fetch 3 Execute 3
Fetch 4 Flush
Fetch SUB Execute SUB
CALL SUB
BSF PORTA,3
Instruction @ SUB
Pipelining
A. Aizza - 23/11/05 - 9 -
MCU MCU Microchip – Long Word InstructionMCU Microchip – Long Word Instruction
Long Word Instruction
• Separate instruction/data bus allows different bus widths
• PIC16 word length optimized to 14-bits
• Harvard architecture allows single word/single cycle instructions
• 2k x 16 program memory on PICmicro roughly equivalent to 4k x 8 on other 8-bit microcontrollers
• Single cycle access increases execution bandwidth
1000 0110
kk kk kk kk
8-bit Instructionldaa #imm<8>
8-Bit InstructionMotorola M68HC05National COP8Zilog Z8SGS Thomson ST62Intel / Philips 80C51
ProgramMemory
kk kk kk k0000 1110
16-bit Instruction
k
movlw k
Long Word InstructionMicrochip PICmicro
ProgramMemory
A. Aizza - 23/11/05 - 10 -
MCU MCU Microchip - Register File ConceptMCU Microchip - Register File Concept
Register File ConceptRegister File Concept
• Register File Concept: Unlike other RISC processors, all of data memory is part of the register file, so any location in data memory may be operated on directly
• All peripherals are mapped into data memory as a series of registers
• Orthogonal Instruction Set: ALL instructions can operate on ANY data memory location
• The Long Word Instruction format allows a directly addressable register file
w f
ALU
w f
WREG
d
DataMemory
Function Examples:
addwf MyReg,w ; WREG = WREG + MyRegaddwf MyReg,f ; MyReg = WREG + MyReg
A. Aizza - 23/11/05 - 11 -
MCU PIC16F877 - CaratteristichePIC16F877 - Caratteristiche
Velocita` massima: 20Mhz Fino a 8K words (da 14bit) Pinout compatibile con altri
PIC16
Internal timers (8bit e 16bit) PWM (capture, compare) SSP (SPI o IIC) USART PSP (slave) Brown-out detection
Caratteristiche principali
10bit A/D converter Brown-out reset Comparatore analogico
256byte internal data EEPROM Oscillatore configurabile
ICSP (debug/ICD) Protezione del codice Watch-dog timer Modalita` SLEEP
A. Aizza - 23/11/05 - 12 -
MCU PIC16F877 - CaratteristichePIC16F877 - Caratteristiche
Caratteristiche principali
Descrizione generale
Piedinatura Schema a blocchi
Oscillatore Reset Configuration Bits
Memoria dati Memoria programma Paging memoria
programma Registri speciali
Convertitore A/D Interrupts
Manuale completo
A. Aizza - 23/11/05 - 13 -
MCU PIC16F877 – Set di istruzioniPIC16F877 – Set di istruzioni
Indirizzamento – Accesso alla Data MemoryIndirizzamento – Accesso alla Data Memory
Diretto:movwf <reg>
Indiretto:movwf INDF
Immediato (literal)movlw <const>
A. Aizza - 23/11/05 - 14 -
MCU PIC16F877 – Set di istruzioniPIC16F877 – Set di istruzioni
Indirizzamento – controllo di flussoIndirizzamento – controllo di flusso
Assoluto:goto <addr>
Subroutine:call <addr>
Istruzioni ‘Skip’:btfss <reg>, <bit>
A. Aizza - 23/11/05 - 15 -
MCU PIC16F877 – Set di istruzioniPIC16F877 – Set di istruzioni
Classi di istruzioni: Control, Bit e Byte.Classi di istruzioni: Control, Bit e Byte.
A. Aizza - 23/11/05 - 16 -
MCU PIC16F877 – Set di istruzioniPIC16F877 – Set di istruzioni
A. Aizza - 23/11/05 - 17 -
MCU Pratica – Configurazione hw minimaPratica – Configurazione hw minima
Alimentazione: +5Vdc (pin 11, 32), 0Vdc (pin 12, 31) Clock: RC (pin 13) Reset: +5Vdc run, 0Vdc reset (pin 1)
Schema configurazione hardware minima
Configurazione hardware minima
A. Aizza - 23/11/05 - 18 -
MCU Pratica – SoftwarePratica – Software
Software: ‘Hello World’
Per far ‘funzionare’ un PIC serve:
- Il codice da eseguire caricato nella program memory,- I configuration bits programmati in modo da rispecchiare le
caratteristiche hardware.
ll file .hex, creato per esempio dall’assemblatore a partire dall’assembler o da un compilatore+assemblatore a partire dal linguaggio-C, contiene sempre il codice da eseguire assemblato. Puo` anche contenere i configuration bits. In caso contrario i configuration bits possono essere direttamente impostati tramite il programmatore.
Il codice da eseguire (e i configuration bits) vengono caricati nell’MCU tramite il programmatore.
A. Aizza - 23/11/05 - 19 -
MCU Pratica – Software Pratica – Software
ERRERR
MAPMAP
LSTLST
CODCOD
HEXHEX
COFCOF
Multiple Source File Projects
CC
OOASMASM
OO
LIBLIB
LKRLKR
Object File(s)
Library File(Collection of Pre-Compiled Object Files)
Linker Script File
C Source File(s)
Assembly Source File(s)
Common Object File Format(COFF) Object Module File
Map File
Used to generate HEX, CODand LST files:mp2cod.exe COD and LSTmp2hex.exe HEX
Relocatable CodeRelocatable Code
A. Aizza - 23/11/05 - 20 -
MCU Pratica – Software ‘Hello World’ – ASMPratica – Software ‘Hello World’ – ASM
Software: ‘Hello World’
Header: LIST P=16F877, F=INHX8M
include <P16F877.inc>
__CONFIG _CP_ALL & _DEBUG_OFF & ...
errorlevel -302, -306, -202
radix dec
Inizio Codice: ASM – hello.asmorg 0x00
goto Start
org 0x04
goto ISR
Codice:Start
...
A. Aizza - 23/11/05 - 21 -
MCU Pratica – Software – documenti utiliPratica – Software – documenti utili
Suggerimento...
Quando si scrive codice ASM e` bene avere a disposizione:
1) Manuale ASM: Quick ASM reference / ASM reference manual
2) Il datasheet del PIC che si sta usando
3) Il circuito per il quale si sta scrivendo il codice
A. Aizza - 23/11/05 - 22 -
MCU Pratica – Tools: programmatorePratica – Tools: programmatore
Come programmare un PIC? IC-Prog + JDM!
Codice in program memory
Configuration bits
Data EEPROM
Read, Write e Verify