Lecture (04) PIC16F84A (3) By: Dr. Ahmed ElShafee ١ Central Processing Unit Central processing unit (CPU) is the brain of a microcontroller responsible for finding and fetching the right instruction which needs to be executed, decoding that instruction, finally its execution. Arithmetic logic unit is responsible for performing operations of adding, subtracting, moving, and logic operations, and shifting ٢
In instructions with two operands, ordinarily one operand is in work register (W register), and the other is one of the registers (GPR, SFR ) or a constant. In instructions with one operand, is either W register or one of the registers. Execution of some instructions affects status bits, which depends on the result itself ALU can affect values of Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register. ٣ Memory organization PIC16F84 has two separate memory blocks, one for data and the other for program Program memory 1024 locations with 14 bits width where locations zero and four are reserved for reset and interrupt vector. ٤
Data memory consists of EEPROM and RAM memories EEPROM memory consists of 64 eight bit locations RAM memory for data occupies space on a memory map from location 0x0C to 0x4F which comes to 68 locations. Locations of RAM memory are also called GPR registers which is an abbreviation for General Purpose Registers. ٥ GPR registers can be accessed regardless of which bank is selected at the moment. SFR registers first 12 locations in banks 0 and 1 are registers of specialized function assigned with certain blocks of the microcontroller. ٦
Working with Memory Banks Memory Banks Selecting one of the banks is done via RP0 bit in STATUS register. Example: bcf STATUS, RP0 Instruction BCF clears bit RP0 (RP0=0) in STATUS register and thus sets up bank 0. bsf STATUS, RP0 Instruction BSF sets the bit RP0 (RP0=1) in STATUS register ٧ and thus sets up bank1. Let's assume that we have selected bank 0 at the beginning of the program, and that we now want to write to certain register located in bank 1, say TRISB. Although we specified the name of the register TRISB, data will be actually stored to a bank 0 register at the appropriate address, which is PORTB in our example. Bcf STATUS, RP0 ;Select memory bank 0 Bsf STATUS, RP0 ;Select memory bank 1 ٨
Locations 0Ch 4Fh are general purpose registers (GPR) which are used as RAM memory. When locations 8Ch CFh in Bank 1 are accessed, we actually access the exact same locations in Bank 0. In other words, whenever you wish to access one of the GPR registers, there is no need to worry about which ٩ bank we are in! Program Counter Program counter (PC) is a 13 bit register that contains the address of the instruction being executed 5 bit register PCLATH for the five higher bits of the address, and the 8 bit register PCL for the lower 8 bits of the address. ١٠
STATUS Register bits 6:5 RP1:RP0 (Register Bank Select bits) 01 = first bank 00 = zero bank bit 4 TO Time out ; Watchdog overflow. 1 = overflow did not occur 0 = overflow did occur ١١ bit 3 PD (Power down bit) 1 = after supply has been turned on 0 = executing SLEEP instruction bit 2 Z (Zero bit) Indication of a zero result This bit is set when the result of an executed arithmetic bit 1 DC (Digit Carry) DC Transfer Bit affected by operations of addition, subtraction. 1 = transfer occurred on the fourth bit according to the order of the result 0 = transfer did not occur ١٢
bit 0 C (Carry) Transfer Bit that is affected by operations of addition, subtraction and shifting. 1 = transfer occurred from the highest resulting bit 0 = transfer did not occur ١٣ TRISB, PORTB, TRISA, and PORTA To change function of port to the following directions O O I O O O I I You need to write the following literal to TRIS register =0x23 0 0 1 0 0 0 1 1 ١٤
Assembly programming Compiler directives (MPLAP.X) Variables Values assignment Arithmetic operations Logic operations Loops Conditioning Branches Other instructions ١٥ Compiler directives LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_ON & _WDT_OFF & _CP_OFF ١٦
Variables As mentioned before PIC16F84 has three types of registers (memory) 1. W working register 2. SFR: Bank 00: 00h 0bh Bank 01: 80h 8bh ١٧ 2.1 Programmer can access SFR directly using its address (after selecting targeted bank) 0x00, 0x01,.., 0x0b 0x80, 0x81,, 0x8b 2.2 Programmer can assign name for SFR address INDF equ 0x00 PCL equ 0x02 STATUS equ 0x03 FSR equ 0x04 PCLATH equ 0x0A ١٨
Programmer can refer to Bits within registers Z equ 0x02 C equ 0x00 IRP equ 0x07 2.3 Programmer include processor.inc file include "p16f84.inc File path C:\Program Files (x86)\microchip\mplabx\mpasmx ١٩ W EQU H'0000' F EQU H'0001' ;-----Bank0------------------ INDF EQU H'0000' TMR0 EQU H'0001' PCL EQU H'0002' STATUS EQU H'0003' FSR EQU H'0004' PORTA EQU H'0005' PORTB EQU H'0006' EEDATA EQU H'0008' EEADR EQU H'0009' PCLATH EQU H'000A' INTCON EQU H'000B ;-----Bank1------------------ OPTION_REG EQU H'0081' TRISA EQU H'0085' TRISB EQU H'0086' EECON1 EQU H'0088' EECON2 EQU H'0089 ٢٠ ;----- STATUS Bits --------------------------- C EQU H'0000' DC EQU H'0001' Z EQU H'0002' NOT_PD EQU H'0003' NOT_TO EQU H'0004' IRP EQU H'0007' RP0 EQU H'0005' RP1 EQU H'0006' ;----- PORTA Bits --------------------------- RA0 EQU H'0000' RA1 EQU H'0001' RA2 EQU H'0002' RA3 EQU H'0003' RA4 EQU H'0004' ;----- PORTB Bits ----------------------- RB0 EQU H'0000' RB1 EQU H'0001' RB2 EQU H'0002' RB3 EQU H'0003' RB4 EQU H'0004' RB5 EQU H'0005' RB6 EQU H'0006' RB7 EQU H'0007'
;----- OPTION_REG Bits ------------------------ PSA EQU H'0003' T0SE EQU H'0004' T0CS EQU H'0005' INTEDG EQU H'0006' NOT_RBPU EQU H'0007' PS0 EQU H'0000' PS1 EQU H'0001' PS2 EQU H'0002' ;----- TRISA Bits ---------------------------------- TRISA0 EQU H'0000' TRISA1 EQU H'0001' TRISA2 EQU H'0002' TRISA3 EQU H'0003' TRISA4 EQU H'0004' ;----- TRISB Bits ----------------------------------- TRISB0 EQU H'0000' TRISB1 EQU H'0001' TRISB2 EQU H'0002' TRISB3 EQU H'0003' TRISB4 EQU H'0004' TRISB5 EQU H'0005' TRISB6 EQU H'0006' TRISB7 EQU H'0007' ٢١ 3. GPR Bank 00: 0ch 4fh Bank 01: 8ch cfh 3.1 Programmer can access GFR directly using its address (after selecting targeted bank) 3.2 Programmer can assign name for GPR address ٢٢ num1 equ 0x0c num2 equ 0x0d n equ 0x0e x equ 0x0f temperature equ 0x1A
Values assignment 1 MOVLW k Write constant in W register 2 MOVWF f Copy W to f 3 MOVF f,d Copy f to d (d=0 des w, d=1; des f itdelf) 4 CLRW Write 0 in W 5 CLRF f Write 0 in f 6 BCF f,b Reset bit b in f 7 BSF f,b Set bit b in f MOVLW H'00' BSF STATUS,RP0 MOVWF TRISB BCF STATUS,RP0 MOVF 0X0C,W ٢٣ Arithmetic operations 8 ADDLW k Add W to a constant 9 SUBLW k Subtract W from a constant 10 ADDWF f,d Add W to f 11 SUBWF f,d Subtract W from f 12 INCF f,d Increment f 13 DECF f,d Decrement f ٢٤ Addlw H 12 Sublw D 12 Addwf b 11110000,f addwf b 11110000,1 Subwf b 00001111,w subwf b 00001111,0 Incf PORTB,f Decf PORTB,f
Logic operations 14 ANDLW k Logic AND W with constant 15 IORLW k Logic OR W with constant 16 XORLW k Logic exclusive OR W with constant 17 ANDWF f,d Logic AND W with f 18 IORWF f,d Logic OR W with f 19 XORWF f,d Logic exclusive OR W with f 20 RLF f,d Rotate f to the left through CARRY 21 RRF f,d Rotate f to the right through CARRY 22COMF f,d Complement f 23SWAPF f,d Copy the nibbles from f to d crosswise ٢٥ Loops 24 INCFSZ f Increment f, skip if f =0 25 DECFSZ f Decrement f, skip if f = 0 MOVLW D'04' MOVWF 0x0c loop......... decfsz 0x0c goto loop ٢٦
Conditioning 26 BTFSC f,b Test bit b in f, skip next instruction if b = 0 27 BTFSS f,b Test bit b in f, skip next instruction b =1 check btfsc PORTA,RA0 goto press_active_low goto check press_active_low...... ٢٧ Branching 28 GOTO address Jump to address 29 CALL address Call a program 30 RETURN Return from a subprogram 31 RETLW Return from a subprogram with constant in W MOVLW h ff' MOVWF 0x0c loop call delay decfsz 0x0c goto loop ٢٨ delay nop nop nop return
Other instructions 32 NOP No operation 33 RETFIE Return from interrupt routine 34 CLRWDT Initialize watchdog timer 35 SLEEP Stand by mode ٢٩ Creating project ٣٠
٣١ ٣٢
٣٣ ٣٤
٣٥ ٣٦
٣٧ ٣٨
٣٩ Working with simulator ٤٠
٤١ ٤٢
٤٣ ٤٤
٤٥ ٤٦
٤٧ ٤٨
٤٩ Build delays DELAY MOVLW d'255' MOVWF 0x4f MOVWF 0x4E DELAY_loop2 DELAY_loop1 DECFSZ 0x4f goto DELAY_loop1 DECFSZ 0x4e goto DELAY_loop2 RETURN ٥٠
LedFlasher.asm LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_ON & _WDT_OFF & _CP_OFF ORG 0 GOTO start ORG 4 GOTO start start MOVLW H'00' BSF STATUS,RP0 MOVWF TRISB BCF STATUS,RP0 LOOP MOVLW B'00000000' MOVWF PORTB CALL DELAY MOVLW B'11111111' MOVWF PORTB CALL DELAY GOTO LOOP ٥١ PressControlledLedFlasher.asm LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_OFF & _WDT_OFF & _CP_OFF ORG 0 GOTO start ORG 4 GOTO start start BSF STATUS,RP0 MOVLW H'00' MOVWF TRISB MOVLW H'01' MOVWF TRISA BCF STATUS,RP0 LOOP MOVLW B'00000000' MOVWF PORTB CALL DELAY BTFSs PORTA,RA0 GOTO LOOP MOVLW B'11111111' MOVWF PORTB CALL DELAY GOTO LOOP ٥٢
PressControlledLedToggle.asm LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_FF & _WDT_OFF & _CP_OFF ORG 0 GOTO start ORG 4 GOTO start start BSF STATUS,RP0 MOVLW H'00' MOVWF TRISB MOVLW H'01' MOVWF TRISA BCF STATUS,RP0 MOVLW h'00' MOVWF 0X0C MOVLW B'00000000' ٥٣ MOVWF PORTB LOOP CALL DELAY BTFSS PORTA,RA0 GOTO LOOP COMF 0X0C,F MOVF 0X0C,W MOVWF PORTB GOTO LOOP LedChaser.asm LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_FF & _WDT_OFF & _CP_OFF ORG 0 GOTO start ORG 4 GOTO start start MOVLW H'00' BSF STATUS,RP0 MOVWF TRISB BCF STATUS,RP0 CALL DELAY MOVLW B'0000010' MOVWF PORTB CALL DELAY MOVLW B'0000100' MOVWF PORTB CALL DELAY MOVLW B'0001000' MOVWF PORTB GOTO LOOP LOOP CALL DELAY MOVLW B'00000001' MOVWF PORTB ٥٤
Reversible Chaser LIST P=16f84a include "p16f84a.inc" config _FOSC_XT & _PWRTE_OFF & _WDT_OFF & _CP_OFF ORG 0 goto start ORG 4 goto start start BSF STATUS,RP0 MOVLW 0X00 MOVWF TRISB MOVLW 0XFF MOVWF TRISA MOVLW 0X01 BCF STATUS,RP0 MOVWF PORTB ٥٥ LOOP1 MOVLW 0X07 MOVWF 0X0C LEFT RLF PORTB,W MOVWF PORTB call DELAY DECFSZ 0x0C,f GOTO LEFT MOVLW 0X07 MOVWF 0X0C RIGHT RRF PORTB,W MOVWF PORTB call DELAY DECFSZ 0x0C,f GOTO RIGHT GOTO LOOP1 DELAY MOVLW d'255' MOVWF 0x4f MOVWF 0x4E DELAY_loop2 DELAY_loop1 DECFSZ 0x4f goto DELAY_loop1 DECFSZ 0x4e goto DELAY_loop2 RETURN end ٥٦
Thanks,.. See you next week (ISA), ٥٧