Академический Документы
Профессиональный Документы
Культура Документы
Input Interface
Output Interface
Processors
Microprocessors and Micro-controllers
Microprocessors
CPU for Computers No RAM, ROM, I/O on CPU chip itself Example: Intels x86, Motorolas 680x0
CPU GeneralPurpose Microprocessor
Data Bus
RAM
ROM
I/O Port
Timer
Serial Port
Address Bus
What is Micro-controller?
Basically, a micro-controller is a device which integrates a number of the components of a microprocessor system onto a single microchip. A micro-controller combines onto the same microchip:
The CPU core Memory (both ROM and RAM) Some parallel digital I/O & more
Micro-controller
Source: www.ami.bolton.uk
Components of a Micro-controller
A Timer module to allow the micro-controller to perform tasks for certain time periods. A serial I/O port to allow data to flow between the micro-controller and other devices such as a PC or another microcontroller. An ADC to allow the micro-controller to accept analogue input data for processing.
Micro-controller
Source : An Introduction to the design of small-scale embedded systems , Tim Wilmshurst, palgrave 2001
Why Micro-controller?
Low cost, small packaging Low power consumption Programmable, re-programmable Lots of I/O capabilities Easy integration with circuits For applications in which cost, power and space are critical Single-purpose
Basics of Architecture
VonNeuman Architecture
Only one bus between CPU and memory RAM and program memory share the same bus and the same memory, and so must have the same bit width Bottleneck: Getting instructions interferes with accessing RAM
Program and Data Memory
BUS
Harvard Architecture
Separate program bus and data bus: can be different widths!
Instruction Pipelining easy
Data Memory
Program Memory
8-Bits 12/14/16-Bits
Load-store Architecture
No memory access for data processing instructions
PIC Architecture
PICs are RISC Reduced Instruction Set Computer (RISC) Few instructions (usually < 50) Only a few addressing modes Executes 1 instruction in 1 internal clock cycle (Tcyc)
Peripheral Components
Clock Frequency
Examples 12C50x 4MHz 12C67x 10MHz 16Cxxx 20MHz 17C4x / 17C7xxx 33MHz 18Cxxx 40MHz
Clocking Scheme
Instruction Execution
Clock internally divided by 4 to generate 4 quadrature clocks Instruction cycle consist of 4 Q cycles PC incremented every Q1 Instruction is fetched from program and latched into instruction register by Q4 Instruction is decoded and executed in the following Q1 to Q4
Instruction Pipelining
Fetch takes one cycle, decode and execute takes another cycle While execution, next instruction can be fetched
No bus conflict due to Harvard Architecture
ALU
W: working register
d: destination bit
Ref: PIC architecture reference manual
Status Register
Status register contains
Arithmetic status of ALU operation RESET status Memory bank select bits
TO: time out; PD: power Down; IRP, RP1, RP0: bank select; Z: zero, DC:digit carry (BCD). C: carry
Memory Organization
Program Memory
Data Memory Access to both possible in each cycle because of distinct bus
Program Memory
FLASH
Re-writable (even by chip itself) Much faster to develop on! Finite number of writes (~100k Writes) PIC Examples: Any F part: 16F84, 16F87x, 18Fxxx (future)
Program Memory
Mid-range PIC processors have 13 bit Program Counter Width of program memory bus 14 bits Program memory space divided into 4 pages of 2K each
Memory Map
PCLATH (program counter latch high): to jump from one page to another these bits modified
Ref: PIC reference manual
Stack
Mid-range PIC 8-level deep 13 bit wide hardware stack
Not part of program or data memory
PC is pushed onto stack when CALL instruction is executed or Interrupt occurs PCLATH is not modified when PUSHed to or POPed from stack
Registers
GPR: Area banked to provide greater than 96 bytes of general purpose RAM SFR: To control the peripheral and core functions like indirect addressing
Summary
We have reviewed architectural features Understood features of processor core of the PIC family of micro-controllers We shall look at instruction set and peripheral components in the next class
Instructions : 14 bit (mid-range) All instructions take one cycle unless conditional test is true or PC is changed as a result of an instruction
Instruction format
Byte-oriented Operation 0..6 : 7 bit file register address 7 : d bit ; 0 for destination W 8..13: opcode Bit-oriented Operation 0..6: 7 bit file register address 7..9: 3-bit bit address within the 8-bit file register 10..13: opcode
Instruction Format
Literal and Control Operations
General
0..7 : 8 bit literal (immediate) value 8..13: opcode
Addressing Modes
Direct Addressing
in status register
Indirect Addressing
Full 8-bit register file address is first written into FSR, a special purpose register FSR serves as address pointer to any address through out the entire register file Subsequent direct access of INDF (a SFR) will actually access the register file using the content of FSR as a pointer to the location of operand
Indirect Addressing
in status register
Byte oriented operations Arithmetic and logical operations Data Movement Examples addwf f,d Add contents of W with register f, if d=0 store result in W else store in register f; e.g.: addwf 0x20,0 clrf f Contents of register f are cleared and Z bit (STATUS) is set; e.g.: clrf 0x30 movwf f Move data from W register to register f e.g : movwf 0x04
Instructions
Decrement register f, place result in f or W depending on value of d; Skip the next instruction if result is zero e.g.: decfsz 0x20,1 Decrement f, place result depending on value of d, effects Z flag e.g : decf 0x30,0
conditional branching
decf f,d
Literal Operations
Immediate addressing mode Examples addlw k Add literal k to register W e.g: addlw 0x05 movlw k Move literal k into register W e.g : movlw 0x21
Instruction CLRF writes constant 0 in 'f ' register, and CLRW writes constant 0 in register W. SWAPF instruction exchanges places of the 4-bit nibbles field inside a register.
On Rotation instructions
Rotates the register contents through flag C Bits move by one bit to the left (toward bit 7), or to the right (toward bit 0). Bit which "comes out" of a register is written in flag C, and value of C flag is written in a bit on the "opposite side" of the register. Example: rrf f,F(W)
Copy f into F or W, rotate F or W right through the carry bit
Control Instructions
Program and Processor Control Examples goto k (k 11 bit) Unconditional branch. Literal k is loaded into PC; e.g. : goto there (use of labels) call k (k 11 bit) Call subroutine at location k; e.g: call sum
Upper 2 bits of PC loaded from PCLATH<4:3>
clrwdt
Clear watchdog timer
Example Code
Decrement a 16 bit counter:
upper byte of the counter COUNTH & lower byte COUNTL
movf COUNTL,F btfsc STATUS,Z decf COUNTH,F decf COUNTL,F ; Set Z if lower byte =0 ; if so, decrement COUNTH ; decrement COUNTL
Example Code
00h INDF
04h
FSR
7Fh
Register File
14bit core
Look-up:
; Load W with 4 ; Call the table subroutine ; Store the result from the table Table addwf PCL, W ; Jump to(current PCL)+W retlw 0x00 ; Return with 0x00 in W retlw 0x23 ; Return with 0x23 in W retlw 0x33 ; etc. retlw 0x88
Facility of MACRO
New instructions, at assembler level can be created that are sequences of PIC instructions Example: macro definition
bank1 macro bsf endm status, RP0
Summarizing
We have studied instruction set of PIC micro-controllers Looked at small code snippets We know about MPASM You will use these instructions for developing different applications
Interrupts: a review
An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services the interrupt When the CPU finishes servicing the interrupt, it returns to the original execution stream at the point where it left off. Interrupt driven I/O for interfacing with on chip peripherals
Interrupts in PIC
Sources of interrupt are many
INT pin interrupt from external source Port B change interrupt (RB7:RB4) Timer overflow interrupts USART interrupts A/D conversion interrupts LCD interrupt others
Interrupt Management
Use of register INTCON: Status and Control
Bit 7: Global interrupt enable
Enables (if set) all unmasked interrupts or disables all interrupts
Bits 6,5,4,3: For enabling peripheral, timer0, external interrupt, port B bit change interrupts respectively Bits 2,1,0 : Timer0, INT, port change interrupt flag respectively
Flag bits get set when interrupt occurs regardless of the value of enable bit
Peripheral Interrupts
Managed using PIE and PIR registers PIE registers contain bits for enabling interrupts from individual peripherals PIR registers contain flag bits for individual peripheral interrupts Bit oriented instructions can be used to examine and/or manipulate interrupt control and status registers
Interrupt Processing
When interrupt is responded to
GIE bit is cleared to disable other interrupts PC is pushed into stack PC is loaded with 0004h Save STATUS & W register in temporary memory locations In the ISR, source of interrupt is determined by polling the interrupt flag bit
Return from interrupt instruction (retfie) exits ISR, sets GIE bit to allow pending interrupt to execute
Interrupt Timing
Interrupt Constraints
Each interrupt source charaterised by
Minimum time interval between interrupts from the same source Maximum time it takes the CPU to execute interrupt sources handler
Servicing of interrupts must not be delayed beyond the limit imposed by the timing requirement of the source
Critical Region
A critical region is a sequence of instructions that must be protected from an intervening interrupt or produce erroneous output In PIC this problem is handled by
Single cycle read-modify-write instructions
xorwf PORTD,F Port D data read, XORed with W and written back to port D
Disabling interrupts by clearing GIE bit for the required set of instructions
Ports used to control and monitor external devices Ports have 2 control registers
TRISX sets whether each pin is an input or output PORTX sets their output bit levels
Example: Port A
All pins are I/O with associated direction bits in TRISA Initialisation code:
clrf STATUS clrf PORTA bsf STATUS, RP0 movlw 0xCF movlwf TRISA ;bank 0 ; initialises by clearing output latches ; select bank1 ; value used to initialise data direction ; PortA<3:0>=input, <5:4>=output
Interrupt on overflow from 0xFF to 0x00 Edge Select for external clock
Programmable prescaler
Synchronised Counter
Timer increments on rising edge of external clock External clock is synchronised with internal phase clock
Asynchronous Counter
Timer increments independent of internal phase clock
Even when processor sleeps, timer1 continues to count in asynchronous mode, on overflow could wake-up the device
Watchdog Timer
Free running on chip RC oscillator which does not require any external component A WDT time-out generates a device reset In sleep mode a WDT time-out causes the device to wake-up To avoid unintended device reset, postscaler has to be changed after clearing watchdog timer WDT is enabled/disabled by a device configuration bit
Capture
Capture mode records value of timer1 when events like rising edge or falling edge occurs on pin CCPx When capture is made, interrupt request flag bit is set
Compare
Content of register compared with Timer1 register pair value When match occurs, voltage level at CCPx pin is changed depending on the value of control bits
PWM
Pulse Width Modulation Duty Cycle often expressed as a percentage of the period. Average DC voltage will be approximately the same percentage of the on voltage. Typical uses:
Intensity control Motor control Temperature control
PWM Mode
In pulse width modulation mode, CCPx pin produces up to a 10-bit resolution PWM output
Since CCPx pin is multiplexed with the port data latch, the corresponding TRIS bit must be cleared
Coupled with Timer2 for producing output Period and duty cycle of timer2 output manipulated for obtaining desired PWM waveform
PWM: Set up
Steps required for setting up PWM
Establish the PWM period by writing to PR2 register Establish the PWM duty cycle by writing onto CCPRxL & CCPxCON<5:4> bits Make CCPx pin an output Establish TMR2 prescale value and enable timer by writing to T2CON Configure CCP module for PWM operation
Input analog channel, Conversion clock, Analog reference voltage is software selectable Can operate even while the device in sleep mode. Can generate an interrupt on ADC conversion done
Result written on to ADRES register
LCD Module
Generates timing control to drive LCD panel Also provides control of pixel data
In-circuit programmer
Serial in-circuit programming support
PIC: Examples
Low End: 12C508
8pin package (DIP) 12bit core - 33 instructions 1us instruction time (Tclk = 4MHz) 512 12bit program memory 25 8bit data memory or registers (File registers) 2 level hardware stack (no interrupts) 5 GPIO pins, 1 input only (25mA source/sink) Features: Internal pullups, wake up on pin change, internal oscillator Peripherals: Timer, Watch Dog Timer
PIC Examples
Mid Range: 16F876
28pin package (DIP) 14bit core - 35 instructions 200ns instruction time (Tclk = 20MHz) 8,092 14bit FLASH program memory 368 8bit data memory or registers (File registers) 256 8bit EEPROM (nonvolatile) data registers 8 level hardware stack (interrupts enabled) 22 GPIO (20mA source / 25mA 7sink) Peripherals: 5ch 10bit ADC, USART/I2C/SPI, 16bit & 8bit timers Features: Brown out detect, In-Circuit Debugger (ICD)
Summary
We have studied PIC family of processors
Architecture Instruction set Peripherals
PIC processors are well suited for lowend and mid-range applications