MCU
MCU
Micro Controller Unit
23/11/2005 – 14/12/2005
Andrea Aizza
Contenuti
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
A. Aizza - 23/11/05
-2-
Contenuti
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
A. Aizza - 23/11/05
-3-
Contenuti
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
A. Aizza - 23/11/05
-4-
MCU
Introduzione 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
-5-
MCU
A. Aizza - 23/11/05
MCU Microchip

Harvard Architecture

Instruction Pipelining

Long Word Instructions

Single Cycle Instructions

Single Word Instructions

Large Register File

Reduced Instruction Set

Orthogonal Instruction Set
-6-
MCU
MCU Microchip - Architecture
Von Neumann
Architecture
8-bit Bus
CPU
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
Program
& Data
Memory
Harvard
Architecture
8-bit Bus
14-bit Bus
•
Data
Memory
CPU
X12 = PIC16C5x, PIC12C5xx
x14 = PIC16Cxxx, PIC14Cxxx, PIC12C6xx
x16 = PIC17Cxxx
x16 Enhanced = PIC18Cxxx
Program
Memory
A. Aizza - 23/11/05
-7-
MCU
MCU Microchip - Pipelining
Pipelining
•
In most micro controllers, instructions are fetched and executed sequentially
•
The pipelined architecture overlaps fetch and execution, making single cycle
execution possible
Instruction Cycles :
ΠMOVLW
0x55
 MOVWF
PORTB
Ž CALL
SUB
 BSF
PORTA,3
TCY=0
TCY=1
Fetch 1
Execute 1
Fetch 2
TCY=2
TCY=4
TCY=5
Execute 2
Fetch 3
Execute 3
Fetch 4
 Instruction @ SUB
A. Aizza - 23/11/05
TCY=3
Flush
Fetch SUB
-8-
Execute SUB
MCU
MCU Microchip – Long Word Instruction
Long Word Instruction
Program
Memory
•
•
•
•
•
Separate instruction/data bus
allows different bus widths
PIC16 word length optimized to 14bits
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 Program
Memory
Single cycle access increases
execution bandwidth
8-Bit Instruction
Motorola
M68HC05
National
COP8
Zilog
Z8
SGS Thomson
Intel / Philips 80C51
ST62
8-bit Instruction
ldaa
#imm<8>
1000 0110
k
k
k
k
k
k
k
k
Long Word Instruction
Microchip
PICmicro
16-bit Instruction
movlw k
0000 1110
A. Aizza - 23/11/05
-9-
k
k
k
k
k
k
k
k
MCU
MCU Microchip - Register File Concept
Register File Concept
Data
Memory
w
•
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
f
ALU
d
w
f
WREG
Function Examples:
addwf MyReg,w
addwf MyReg,f
A. Aizza - 23/11/05
; WREG = WREG + MyReg
; MyReg = WREG + MyReg
- 10 -
MCU
PIC16F877 - Caratteristiche
Caratteristiche principali



Velocita` massima: 20Mhz
Fino a 8K words (da 14bit)
Pinout compatibile con altri PIC16



10bit A/D converter
Brown-out reset
Comparatore analogico






Internal timers (8bit e 16bit)
PWM (capture, compare)
SSP (SPI o IIC)
USART
PSP (slave)
Brown-out detection


256byte internal data EEPROM
Oscillatore configurabile




ICSP (debug/ICD)
Protezione del codice
Watch-dog timer
Modalita` SLEEP
A. Aizza - 23/11/05
- 11 -
MCU
PIC16F877 - 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
- 12 -
MCU
PIC16F877 – Set di istruzioni
Indirizzamento – Accesso alla Data Memory

Diretto:
movwf

Indiretto:
movwf

<reg>
INDF
Immediato (literal)
movlw
A. Aizza - 23/11/05
<const>
- 13 -
MCU
PIC16F877 – Set di istruzioni
Indirizzamento – controllo di flusso

Assoluto:
goto

Subroutine:
call

<addr>
<addr>
Istruzioni ‘Skip’:
btfss
A. Aizza - 23/11/05
<reg>, <bit>
- 14 -
MCU
PIC16F877 – Set di istruzioni
Classi di istruzioni: Control, Bit e Byte.
A. Aizza - 23/11/05
- 15 -
MCU
A. Aizza - 23/11/05
PIC16F877 – Set di istruzioni
- 16 -
MCU
Pratica – Configurazione hw minima
Configurazione hardware 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
A. Aizza - 23/11/05
- 17 -
MCU
Pratica – 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
- 18 -
MCU
Pratica – Software
Multiple Source File Projects
Relocatable Code
Assembly Source File(s)
ASM
C
C Source File(s)
Object File(s)
O
ERR
Map File MAP
LST
COD
HEX
COF
O
Common Object File Format
(COFF) Object Module File
LIB
LKR
Library File
(Collection of Pre-Compiled Object Files)
A. Aizza - 23/11/05
- 19 -
Linker Script File
Used to generate HEX, COD
and LST files:
mp2cod.exe  COD and LST
mp2hex.exe  HEX
MCU
Pratica – 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:
org
goto
org
goto

ASM – hello.asm
0x00
Start
0x04
ISR
Codice:
Start
...
A. Aizza - 23/11/05
- 20 -
MCU
Pratica – 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
- 21 -
MCU
Pratica – Tools: programmatore
Come programmare un PIC? IC-Prog + JDM!
Read, Write e Verify
Codice in program memory
Data EEPROM
Configuration bits
A. Aizza - 23/11/05
- 22 -
Scarica

MCU