PIC microcontroller assembly language learning (I)

Publisher:konglingdeyuanLatest update time:2019-11-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. The statement format of assembly language:

label opcode (instruction mnemonic) operand; comment
(label) (opcode) (operand) (comment)

2. Common pseudo-instructions

a.EQU——Symbol name assignment pseudo-instruction
format: symbol name EQU nn

b.ORG——Pseudo instruction format for defining the program starting address
: ORG nnnn

c.END——Program end pseudo instruction
format: END

d.LIST——List option pseudo-instruction
format: LIST [optional, optional, ...]

e.INCLUDE: Call in external program file pseudo-instruction
format: INCLUDE "file name"


2. Branching Program Structure

——A few explanations on the use of instructions in the program:
(1) For any logical operation (AND, OR, XOR) and arithmetic operation (addition, subtraction) that requires two numbers to participate, one of the operands must be placed in W in advance. Special attention should be paid to the subtraction instruction used here. The subtrahend should be placed in W in advance. In other words, the number placed in W in advance is used as the subtrahend in the operation, and the number in the register is used as the minuend.
(2) A conditional jump instruction often needs to be followed by an unconditional jump instruction to achieve long-distance transfer and program branching.
(3) The instruction system of the PIC microcontroller does not have a dedicated stop instruction, which can be achieved by using an unconditional jump instruction GOTO to jump to itself.


3. PIC microcontroller instructions
consist of three basic types of instructions:
a. Byte operation instructions
b. Bit operation instructions
c. Immediate and control operation instructions

For byte operation instructions, f——> file register identifier, d——> target register identifier

Description: The target identifier specifies the storage location of the operation result:
d=0 The operation result is stored in the W register
d=1 The operation result is stored in the specified file register. The default value of d is 1

 

 4. Instruction Set

5. Examples

  1 ;--------------------------------------------------------

  2 

  3; Sequential program structure

  4; Take out the lower 4 bits of the 20H unit and store them in 21H, and take out the upper 4 bits and store them in 22H

  5; Key points: ANDLW and SWAPF

  6 

  7;---------------------------------------------------------

  8 MOVF 20H,0 ; Send the content of 20H unit to W

  9 ANDLW 0FH ;W high 4 bits cleared, low 4 bits remain unchanged

 10 MOVWF 21H ; send the lower 4 bits after splitting to 21H

 11 SWAPF 20H,0 ; Swap the high and low nibble of the 20H unit content and send it to W

 12 

 13 ANDLW 0FH ; Clear the high four bits of W to 0 and keep the low four bits unchanged

 14 MOVWF 22H ; send the split high four bits to unit 22H

 15 

 16 

 17;--------------------------------------------------------

 18 

 19 ;Branch program structure

 20; Two numbers are stored in the 20H and 21H units in the RAM. Find the larger number and store it in the 22H unit.

 21 ; Key points: Subtract two numbers and determine the value of flag C

 twenty two 

 twenty three ;---------------------------------------------------------

 24 STATUS EQU 03H ; define the STATUS register address as 03H

 25 C EQU 0 ; define the carry/borrow flag C in STATUS to get the address 0

 26 MOVF 20H 0 ; Send the content of unit 20H to W

 27 SUBWF 21H 0 ; Subtract the content in W from the content in 21H, and store the result in W

 28 BTFSS STATUS, C; if C=1, no borrow, then the number in unit 21H is large, jump to F21BIG

 29 GOTO F20BIG ; If C=0, there is a borrow, and the number in unit 20H is larger, then jump to F20BIG

 30     

 31 F21BIG MOVF 21H,0 ; store the content of 21H into W register

 32 MOVWF 22H; then transfer it to unit 22H

 33 GOTO STOP ; skip the following two instructions to the end of the program

 34         

 35 F20BIG MOVF 20H,0 ; store the content of 20H into W register

 36 MOVWF 22H ; then transfer it to unit 22H

 37 

 38 STOP GOTO STOP; Task completed, shutdown, stay where you are

 39 

 40 

 41 ;--------------------------------------------------------

 42 

 43 ;Loop program structure

 44 ; In the data memory, all 50 cells starting from address 30H are written to 00H

 45 ;Key points: The indirect addressing register FSR is used as an address pointer

 46 

 47;---------------------------------------------------------

 48 COUNT EQU 20H; Designate unit 20H as the loop count counter (i.e. loop variable)

 49 FSR EQU 04H ; define FSR register address as 04H

 50 INDF EQU 00H ; Set the INDF register address to 00H

 51 MOVLW D50 ; send the initial value of the counter 50 to W

 52 MOVWF COUNT ; Transfer 50 to the counter (as the operation value of the loop variable)

 53 MOVLW 30H ; send 30H (starting address) to W

 54 MOVWF FSR ; then transfer 30H to register FSR (used as address pointer)

 55         

 56 NEXT CLRF INDF ; Clear the unit specified by the FSR content as the address to 0

 57 INCF FSR,1 ;Address pointer content plus 1, pointing to the next unit

 58 DECFSZ COUNT,1 ;Decrement the count value by 1. If the result is 0, skip to the next instruction to STOP

 59 GOTO NEXT ; Jump back and execute the next loop

 60 STOP GOTO STOP ; After the loop ends, execute this statement to stop the machine.

 61 

 62;--------------------------------------------------------

 63 

 64 ;Subroutine structure

 65; The largest number of 3 is placed in the 40H unit

 66 

 67;---------------------------------------------------------

 68 STATUS EQU 03H

 69 C EQU 00H

 70 X EQU 20H

 71 Y EQU 21H

 72 Z EQU 22H

 73 ;--------------------------------------------------------

 74 

 75 ; Main program

 76 

 77 ;---------------------------------------------------------

 78 

 79 MAIN MOVF 30H,0

 80 MOVWF X

 81 MOVF 21H,0

 82 MOVWF Y

 83 CALL SUB

 84 MOVF Z,0

 85 MOVWF X

 86 MOVF 32H,0

 87 MOVWF Y

 88 CALL SUB

 89 MOVF Z,0

 90 MOVWF 40H

 91 STOP GOTO STOP

 92 ;--------------------------------------------------------

 93 

 94 ; Subroutine: (input parameters: X and Y, output parameter: Z)

 95 

 96 ;---------------------------------------------------------

 97 SUB MOVF X,0 ; send the contents of X to W

 98 SUBWF Y,0 ; Subtract the content of W from the content of Y, and store the result in W

 99 BTFSS STATUS, C; If C=1, no borrow occurs, execute the next line, otherwise jump

100 GOTO X_BIG        

101         

102 Y_BIG MOVF Y,0 ; Send the data in Y to W

103 MOVWF Z ; then save it to Z

104 GOTO THEEND ; skip the following two to the end

105         

106 X_BIG MOVF X,0 ; send the data in X to W

107 MOVWF Z ; then save it to Z

108 THEEND RETURN ; Subroutine returns


Reference address:PIC microcontroller assembly language learning (I)

Previous article:Compare/Capture/PWM (CCP) Module
Next article:【PIC Microcontroller】MPLAB X IDE Quick Start Guide

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号