Assembly language foundation of MCU programming - PIC microcontroller assembly instruction



1, the basic format of the program

First introduce two pseudo-instructions:

EQU - label assignment directive

ORG - address definition directive

The PIC16C5X instruction calculator PC is set to all "1" after RESET, so the reset address of several PIC16C5X chips is:

PIC16C54/55: 1FFH

PIC16C56: 3FFH

PIC16C57/58: 7FFH

In general, the PIC source program does not require a uniform format, you can write according to your own style. But here we recommend a clear and clear format for reference.

TITLE This is ......; program title

;--------------------------------------

; name definition and variable definition

;--------------------------------------

F0 EQU 0

RTCC EQU 1

PC EQU 2

STATUS EQU 3

FSR EQU 4

RA EQU 5

RB EQU 6

RC EQU 7

┋

PIC16C54 EQU 1FFH; chip reset address

PIC16C56 EQU 3FFH

PIC16C57 EQU 7FFH

;-----------------------------------------

ORG PIC16C54 GOTO MAIN ; transfer to the main program at the reset address

ORG 0; start the program at 0000H

;-----------------------------------------

Subprogram area

;-----------------------------------------

DELAY MOVLW 255

┋

RETLW 0

;------------------------------------------

Main program area

;------------------------------------------

MAIN

MOVLW B'00000000'

TRIS RB ; RB has been defined by the pseudo instruction to 6, that is, port B

┋

LOOP

BSF RB, 7 CALL DELAY

BCF RB, 7 CALL DELAY

┋

GOTO LOOP

;-------------------------------------------

END; the program ends

Note: The MAIN label must be within the 0 page.

2, the basis of programming

1) Set the input/output direction of the I/O port

The I/O ports of the PIC16C5X are bidirectionally programmable, that is, each I/O line can be individually programmed as an input or output. This process is implemented by writing the I/O control register TRIS f. The input value is "1", which is the input; if the write value is "0", it is the output.

MOVLW 0FH ;0000 1111(0FH)

input Output

TRIS 6; write 0FH in W to the B port controller,

The 4th high of the B port is the output, and the lower 4 bits are the input.

MOVLW 0C0H ; 11 000000 (0C0H)

RB4, RB5 output 0 RB6, RB7 output 1

2) Check if the register is zero

If you want to determine whether a register is zero, it is very simple, now take the register F10 as an example:

MOVF 10,1 ;F10→F10, the result affects the zero mark status bit Z

BTFSS STATUS, Z; F10 is zero

GOTO NZ; Z=0, that is, F10 is not zero.

┋ ;Z=1 is the F10=0 handler

3) Compare the size of the two registers

To compare the size of two registers, you can subtract them and then judge according to the status bit C. Note that the result of the subtraction is placed in W, which does not affect the original value of the second register.

For example, the F8 and F9 registers should be compared in size:

MOVF 8,0 ;F8→W

SUBWF 9,0 ;F9—W(F8)→W

BTFSC STATUS, Z; judge F8=F9 No

GOTO F8=F9

BTFSC STATUS, C ; C=0 then jump

GOTO F9>F8 ; C=1 subtraction result is positive, F9>F8

GOTO F9<

F9 ; C=0 subtraction result is negative, F9

┋

4) Program that loops n times

If you want to make a program loop n times, you can use a register as a counter. The following example uses F10 as a counter to cycle the program 8 times.

COUNT EQU 10 ; define F10 name as COUNT (counter)

┋

MOVLW 8

MOVWF COUNT LOOP ; loop body

LOOP

┋

DECFSZ COUNT,1 ;COUNT minus 1, the result is zero

GOTO LOOP ; the result is not zero, continue to loop

┋ ; the result is zero, jumping out of the loop

5) Procedures in the format "IF...THEN..."

The following uses the "IF X=Y THEN GOTO NEXT" format as an example.

MOVF X,0 ;X→W

SUBWF Y,0 ;Y—W(X)→W

BTFSC STATUS, Z ; X=Y No

GOTO NEXT ; X=Y, jump to NEXT to execute.

┋ ;X≠Y

6) Programs in the "FOR...NEXT" format

The "FOR...NEXT" program causes the loop to proceed within a certain range. The following example is a program in the "FOR X=0 TO 5" format. F10 puts the initial value of X, and F11 puts the final value of X.

START EQU 10

DAEND EQU 11

┋

MOVLW 0

MOVWF START ; 0→START(F10)

MOVLW 5

MOVWF DAEND ;5→DAEND(F11)

LOOP

┋

INCF START,1 ;START value plus 1

MOVF START,0

SUBWF DAEND,0 ;START=DAEND ?(X=5No)

BTFSS STATUS, Z

GOTO LOOP ; X<5, continue to loop

┋ ;X=5, end loop

7) Program in the format "DO WHILE...END"

The "DO WHILE...END" program executes the loop if it meets the conditions. The following example is a program in the "DO WHILE X=1" format. F10 puts the value of X.

X EQU 10

┋

MOVLW 1

MOVWF X ; 1 → X (F10), as the initial value

LOOP

┋

MOVLW 1

SUBWF X,0

BTFSS STATUS, Z; X=1 No?

GOTO LOOP ; X=1 continues to loop

┋ ;X≠1 jumps out of the loop

8) Checklist procedure

Lookup tables are an operation that is often used in programs. The following example converts decimal 0 to 9 into 7-segment LED digital display values. If the a to g line segments of the LED are driven by RB0 to RB6 of the B port, the following relationship is obtained:

Let the LED be a common yang, then the line value corresponding to the 0~9 number is as follows:

Decimal number segment value decimal value segment value

0 C0H 5 92H

1 C9H 6 82H

2 A4H 7 F8H

3 B0H 8 80H

4 99H 9 90H

The PIC's look-up table program can be implemented using the characteristics of the subroutine with value return. Specifically, in the main program, the table data address is first put into W, and then the subroutine is called. The first instruction of the subroutine puts W into the PC, and the program jumps to the place of the data address, and then the data is placed by the "RETLW" instruction. Enter W to return to the main program. The following program puts the header address with F10.

MOVLW TABLE ; header address → F10

MOVWF 10

┋

MOVLW 1 ;1→W, ready to take the line value of "1"

ADDWF 10,1 ;F10+W = "1" data address

CALL CONVERT

MOVWF 6 ; line segment value is set to port B, lighting LED

┋

CONVERT MOVWF 2 ;W→PC TABLE

RETLW 0C0H; "0" line segment value

RETLW 0F9H; "1" line segment value

┋

RETLW 90H; "9" line value

9) "READ...DATA,RESTORE" format program

The "READ...DATA" program reads data from the data table each time, then increments the data pointer by one to prepare to fetch the next data. In the following example, F10 is the starting address of the data table, and F11 is the data pointer.

POINTER EQU 11 ; define F11 name as POINTER

┋

MOVLW DATA

MOVWF 10; data header address → F10

CLRF POINTER ; data pointer clear

┋

MOVF POINTER,0

ADDWF 10,0 ;W =F10+POINTER

┋

INCF POINTER, 1 ; pointer plus 1

CALL CONVERT ; to adjust the program, take the form data

┋

CONVERT MOVWF 2; data address → PC

DATA RETLW 20H; data

┋

RETLW 15H; data

If you want to execute "RESTORE", just execute a "CLRF POINTER".

10) Delay program

If the delay time is short, the program can simply execute several empty operation instructions "NOP" continuously. If the delay time is long, it can be implemented with a loop. The following example is calculated in F10, and the loop is repeated 100 times.

MOVLW D'100'

MOVWF 10

LOOP DECFSZ 10,1 ;F10—1→F10, the result is zero

GOTO LOOP

┋

The time during which the instruction is executed in the delay program is the delay time. If 4MHz oscillation is used, each instruction cycle is 1μS. Therefore, the single-cycle instruction time is 1μS, and the two-cycle instruction time is 2μS. In the above example, the LOOP cycle delay time is: (1+2)*100+2=302 (μS). Extend the delay time by inserting a no-op command in the loop:

MOVLW D'100'

MOVWF 10

LOOP NOP

NOP

NOP

DECFSZ 10,1

GOTO LOOP

┋

Delay time = (1 + 1 + 1 + 1 + 2) * 100 + 2 = 602 (μS).

The delay time can be greatly extended by a few loop nesting methods. The following example uses 2 loops to do the delay:

MOVLW D'100'

MOVWF 10

LOOP MOVLW D'16'

MOVWF 11

LOOP1 DECFSZ 11,1

GOTO LOOP1

DECFSZ 10,1

GOTO LOOP

┋

Delay time=1+1+[1+1+(1+2)*16-1+1+2]*100-1=5201(μS)

11) Use of RTCC counters

The RTCC is a pulse counter. Its counting pulse has two sources, one is an external signal input from the RTCC pin, and the other is an internal command clock signal. You can use the program to select one of the sources as an input. The RTCC can be used by the program for timing; the program reads the RTCC register value to calculate the time. When the RTCC is used as an internal timer, connect the RTCC pin to VDD or VSS to reduce interference and current consumption. The following example uses RTCC for delay:

RTCC EQU 1

┋

CLRF RTCC; RTCC clear 0

MOVLW 07H

OPTION; select preset multiple 1:256 → RTCC

LOOP MOVLW 255; RTCC count final value

SUBWF RTCC, 0

BTFSS STATUS, Z; RTCC=255?

GOTO LOOP

┋

In this delay program, the RTCC register is incremented by 1 (divide ratio = 1:256) every 256 instruction cycles. If the chip uses 4MHz oscillation, then:

Delay time = 256 * 256 = 65536 (μS)

RTCC is self-oscillating. When it counts, the program can do other things. Just read it at intervals and check its count value.

12) Addressing of the register body (BANK)

For the PIC16C54/55/56, there are 32 registers and only one body (BANK), so there is no body addressing problem. For the PIC16C57/58, there are 80 registers divided into 4 individuals (BANK0-BANK3). In the description of F4 (FSR), it can be seen that bit 6 and bit 5 of F4 are register body addressing bits, and their correspondences are as follows:

Bit6 Bit5 BANK Physical Address

0 0 BANK0 10H~1FH

0 1 BANK1 30H~3FH

1 0 BANK2 50H~5FH

1 1 BANK3 70H~7FH

When the chip is powered on RESET, bit 6 and bit 5 of F4 are random, and the non-powered RESET remains unchanged.

The following example writes data to the 30H and 50H registers of BANK1 and BANK2.

Example 1. (Set the current selection as BANK0)

BSF 4,5 ; Set bit5=1, select BANK1

MOVLW DATA

MOVWF 10H ; DATA→30H

BCF 4,5

BSF 4,6 ;bit6=1, bit5=0 select BANK2

MOVWF 10H ;DATA→50H

From the above example, we can see that to read and write registers in a certain body (BANK), we must first operate the body addressing bits in F4. In practical applications, after power-on reset, clear bit 6 and bit 5 of F4 to 0, so that it points to BANK0, and then point it to the corresponding body as needed.

Note that in the example, when writing numbers to the 30H register (BANK1) and 50H register (BANK2), the register address in the instruction "MOVWF 10H" is written as "10H" instead of the reader's expected "MOVWF 30H" and " MOVWF 50H", why?

Let us review the instruction list. In all of the PIC16C5X's associated register instructions, the Registered Address bits are only 5 bits: fffff, which can only address 32 (00H-1FH) registers. Therefore, it is necessary to select 80 registers, and then use the binary address selection bits PA1 and PA0. When we set the body addressing bits PA1 and PA0 to point to a BANK, the instruction "MOVWF 10H" places the W content into the corresponding register in the BANK (10H, 30H, 50H, or 70H).

Some designers have first contacted the concept of body location, and it is inevitable that there is a discrepancy. The following is an example:

Example 2: (Set the current selection to BANK0)

MOVLW 55H

MOVWF 30H; want to put 55H→30H register

MOVLW 66H

MOVWF 50H; want to put 66H→50H register

Thought that "MOVWF 30H" must be able to put W into 30H, "MOVWF 50H" must be able to put W into 50H, which is wrong. Because the actual effect of these two instructions is "MOVWF 10H", the reason has already been explained above. So the final result of the procedure in Example 2 is F10H=66H, and the real F30H and F50H are not operated.

Suggestion: In order to make the program of the site selection clear, it is recommended to use the name delimiter to write the program, which is not easy to be confused. Example 3: Assume that several registers of BANK0, BANK1, and BANK2 are used in the program as follows:

BANK0 address BANK1 address BANK2 address BANK3 address

A 10H B 30H C 50H · 70H

· · · · · · · ·

· · · · · · · ·

A EQU 10H ;BANK0

B EQU 10H ;BANK1

C EQU 10H ;BANK2

┋

FSR EQU 4

Bit6 EQU 6

Bit5 EQU 5

DATA EQU 55H

┋

MOVLW DATA

MOVWF A

BSF FSR, Bit5

MOVWF B ;DATA→F30H

BCF FSR, Bit5

BSF FSR, Bit6

MOVWF C ;DATA→F50H

┋

The program is written like this, and it is not easy to mistake the body.

13) Program jumps and calls across pages

The following describes the page concept of the program memory area of ​​the PIC16C5X and the two application examples of the page address bits PA1 and PA0 in the F3 register.

(1) "GOTO" cross-page

Example: Set the current program on page 0 (PAGE0), and use "GOTO" to jump to somewhere on page 1.

KEY (PAGE1).

STATUS EQU 3

PA1 EQU 6

PA0 EQU 5

┋

BSF STATUS, PA0; PA0=1, select PAGE page

GOTO KEY; jump to page 1 KEY across pages

┋

KEY NOP; 1 page program

┋

(2) "CALL" cross page

Example: Let the current program be on page 0 (PAGE0), now to call - put the subroutine DELAY on 1 page (PAGE1).

┋

BSF STATUS, PA0; PA0=1, select PAGE1 page

CALL DELAY ; cross-page call

BCF STATUS, PA0; Restore 0 page address

┋

DELAY NOP; subroutine of page 1

┋

Note: The program has a page address for the cross-page CALL. After returning from the subroutine, be sure to restore the original page address.

(3) Program cross-page jump and call preparation

Readers here must ask: When I write the source program (.ASM), I don't pay attention to the storage address of each instruction. How do I know that this GOTO is cross-page, does the CALL need to cross pages? When you start writing the source program, you know when a cross-page jump or call occurs, but when you assemble the source program, it is automatically given. When the assembly results show:

XXX (address) "GOTO out of Range"

XXX (address) "CALL out of Range"

This indicates that your program has jumped and called across pages, and your program has not set the appropriate page address before these spreads GOTO and CALL. At this point you should look at the .LST file generated by the assembly, find these GOTOs and CALLs, and see what page they are going to jump to, and then go back to the source program (.ASM) to make the necessary changes. Until your source assembly is compiled (0 Errors and Warnnings).

(4) Connection of the program page

There should be some processing at the 4 page connection of the program. It is generally recommended to use the following format: Immediately after entering another page, set the corresponding page address bits (PA1, PA0). Page processing is the most troublesome part of PIC16C5X programming, but it's not difficult. Once you have done an actual programming exercise, you will be able to master it.

Vehicle Diagnostic Cables

We make OBD connector with terminal by ourselves, soldering type and crimping type are both available. Also 12V and 24V type. OBD1, OB2, J1939, J1708, J1962, etc. Also molded by different type, straight type or right-angle type. The OBD connector cables used for Audi, Honda, Toyota, BWM, etc. We have wide range of materials source , also we can support customers to make a customized one to replace the original ones.

Vehicle Diagnostic Cables,Diagnostic OBD Cable,Heavy Vehicle Diagnostic Cables,OBD2 Splitter Y Cables,OBD2 Diagnostic Adapters,OBD Heavy Vehicle Cables

ETOP WIREHARNESS LIMITED , https://www.etopwireharness.com

Posted on