Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEE237 Introduction to Microprocessors Week x. SFRs.

Similar presentations


Presentation on theme: "EEE237 Introduction to Microprocessors Week x. SFRs."— Presentation transcript:

1 EEE237 Introduction to Microprocessors Week x

2 SFRs

3 Status Register

4 The instruction set

5 The structure of instructions The instructions

6 A simple Program Write a code to find the result and the remainder of the following division: number/divider. The flow chart: Number=0x27 Divider=0x05 Result=0 START Result++ Number=Number-Divider<?0 Remainder=Number+Divider Result-- Exit N Y

7 The Assembly Code: ;******************************************************************************* ;INCLUDE files include 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

8 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 division loop 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

9 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

10 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

11 Cont. Use indirect addr. –MOVLW 0x25 –MOVWF FSR –MOVLW 0x2B –MOVWF counter –MOVLW 0xFF loop –MOVWF INDF –DECFSZ counter –GOTO loop Here only 8 code lines is used to solve the same problem

12 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 FSR loop: –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

13 Read EEPROM Write EEPROM

14 Stack & Subroutine PICs have a hardware call stack (eight 13 bit registers), which is used to save return addresses.call stack 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.

15 The SW structure MAIN CODE SUBROUT. 1SUBROUT. 2SUBROUT. 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

16 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 0x31 Main: incrf reg1 call waitfor750usec goto $-2 Waitfor750usec: movlw 0xFA movwf delay0 decfsz delay0,f goto $-1 return

17 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.

18 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

19 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 ; end of main INT_SERV: ; your interupt service routine retfie SUB1: Return SUB2: Return

20

21

22 İ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.

23 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.

24

25 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)

26 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

27 İnterrupt System Registers Registers enables/disables interrupts: Registers keeps interrupt flags:

28 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.


Download ppt "EEE237 Introduction to Microprocessors Week x. SFRs."

Similar presentations


Ads by Google