eee237 introduction to microprocessors week x. sfrs

Post on 14-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

EEE237 Introduction to Microprocessors

Week x

SFRs

Status Register

The instruction set

The structure of instructions

The instructions

A simple Program• Write a code to find the result and the

remainder of the following division: number/divider.

• The flow chart:Number=0x27Divider=0x05Result=0

START

Result++

Number=Number-Divider<?0

Remainder=Number+DividerResult--

Exit

N

Y

The Assembly Code: ;*******************************************************************************

;INCLUDE filesinclude p16f877.inc ; include files for register definitions

; CONFIG; __config 0xFF39 ;_CONFIG1, _FOSC_XT & _WDTE_OFF & _PWRTE_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

;*******************************************************************************;; Variable Definitions;#define number 0x21#define divider 0x22#define division 0x23#define remainder 0x24#define dummy 0x25

;*******************************************************************************; Reset Vector;*******************************************************************************

RES_VECT  CODE    0x0000            ; processor reset vector    GOTO    START                   ; go to beginning of program

Cont.:• ;*******************************************************************************; Interrupt Service Routines

•;*******************************************************************************; MAIN PROGRAM;*******************************************************************************

MAIN_PROG CODE                      ; let linker place main program

START    bcf STATUS,RP0    bcf STATUS,RP1

    movlw 0x27    movwf number    movlw 0x05    movwf divider    clrf divisionloop    incf division    movf divider,W    bsf STATUS,C    subwf number,1    btfsc STATUS,C    goto loop    decf division    movf number,W    addwf divider,W    movwf remainder    goto $

•    END

Addressing modes• Direct addressing

– Tha address is a RAM location and the data inside this address is manipulated.

– MOVWF alper; Here ‘alper’ is a RAM loc. and the content of W is moved to ‘alper’.

• Indirect addressing – The address points another adress which has

the data to be manipulated– Two SFRs are used for ind. Addr.: FSR&INDF– FSR reg.’s content points an address. The data

inside this adress can be changed by writing to INDF register.

• Write/Read EEPROM memory

An app. Of indirect addr. (Example)• Fill the ram location from 0x25 to

0x50 with 0xFF.– Here if we use direct addr. mode,

• MOVLW 0xFF• MOVWF 0x25• MOVWF 0x26• …• MOVWF 0x50

– We need 44 code lines

Cont.• Use indirect addr.– MOVLW 0x25– MOVWF FSR– MOVLW 0x2B– MOVWF counter– MOVLW 0xFFloop– MOVWF INDF– DECFSZ counter– GOTO loop

• Here only 8 code lines is used to solve the same problem

Ex2:

• Write a code to swap the data inside RAM locations 0x50-0x53 and 0x150-0x153.– Bcf status,RP0– Bcf status,RP1– Movlw 0x50– Movwf FSRloop:– Movf INDF,W– Movwf buffer1– Bsf STATUS,IRP– Movf INDF,W– Bcf STATUS,IRP– Movwf buffer2– Movf buffer1,W– Bsf STATUS,IRP– Movwf INDF– Bcf STATUS,IRP– Movf buffer2,W– Movwf INDF– Incf FSR– Btfss FSR,2– GOTO loop

Read EEPROM

Write EEPROM

Stack & Subroutine• PICs have a hardware call stack

(eight 13 bit registers), which is used to save return addresses.

• When a subroutine is called or an interrupt is occured stack memorizes the current program memory address so that this address can be used to return from the subroutine

• A subroutine is a small code that does a particular job. It has some inputs and outputs vars.

The SW structure

MAIN CODE

SUBROUT. 1 SUBROUT. 2 SUBROUT. N….

The SW architecture is usually based on a main code and the SUB codes that are used to solve small parts of the complete problem.

The Main part just manages these small jobs

Ex: Write an assembly code that defines a register reg1 and increases it in every 1.5 msec. (XTAL freq: 4MHz)• # define reg1 0x30

• # define delay0 0x31Main:

incrf reg1call waitfor750usecgoto $-2

Waitfor750usec:movlw 0xFAmovwf delay0decfsz delay0 ,fgoto $-1return

Better way of delaying is TIMER interrupt • What is interrupt ?

– It is a group of defined events that stop the regular program execution.

– The first thing that the microcontroller does when an interrupt request arrives is to execute the current instruction and then stop regular program execution. Immediately after that, the current program memory address is automatically pushed onto the stack and the default address (predefined by the manufacturer) is written to the program counter.

• That location from where the program continues execution is called the interrupt vector

• For the PIC16F877 microcontroller, this address is 0004h.

• Part of the program being activated when an interrupt request arrives is called the interrupt routine.

• Its first instruction is located at the interrupt vector

• ORG 000H ; a reset redirects program to this point • GOTO MAIN

• ORG 004H ; an interrupt redirects the program to here • GOTO INT_SERV

• MAIN: ; Your main program ; <main code will bi written here>

• end of main

• INT_SERV: ; your interupt service routine<main code will bi written here> retfie

SUB1:ReturnSUB2:Return

İnterrupt service routine

• How long the interrupt servis routine will be and what it will be like depends on the skills of the programmer as well as the interrupt source itself.

• Some microcontrollers have more interrupt vectors (every interrupt request has its vector), but in this case there is only one. Consequently, the first part of the interrupt routine consists in interrupt source recognition.

Interrupt sources

• Timer interrupts: TMR0 int. / TMR0IF

• Extrernal interrupt: INTE int./ INTIF• Peripheral interrupts: (PEIE)

– USART (TX/RX): TXIF, RXIF– TIMER1,TIMER2 ınt.– etc.

• Int. source must be analysed at the beginning of ISR.

Typical ISR

• At start of ISR– Clear GIE bit to disable any further

interrupt.– Save key registers such as STATUS,

W, etc., these registers must be kept since they may change during ISR.

– Use upper 16 bytes to keep hem (ie: W_temp). Because upper 16 bytes don’t require banking. (They are mirrored)

Cont.

• At the end of ISR– Interrupt flag bits must be cleared.

(Avoid recursive interrupts).– Restore the values of key registers.– Set GIE bit.– Exit from intr. Vector using RETFIE

instruction

İnterrupt System Registers

• Registers enables/disables interrupts:

• Registers keeps interrupt flags:

• Timer interrupt– PIC has a special register that counts

the (1/4) of XTAL pulses. – Each 4 XTAL pulse (an instr. cycle)

increases TMR0 register by one.– TMR0 register is an 8 bit reg. And it

can generate an interrupt in case of overflow. So 256 instr. Cycles generates an interrupt.

– In such a case program automatically goes to the interrupt vector.

top related