PIC single chip microcomputer (peripheral interface controller) is an integrated circuit (IC) used to develop and control peripheral devices. A CPU with decentralized (multitasking) function. The learning content of PIC in MCU is described

1. Pic basic program format of single chip microcomputer

First, two pseudo instructions are introduced:

Equ — pseudo instruction of label assignment

Org — address definition pseudo instruction

Analysis of basic programming method of PIC Single Chip Microcomputer

After reset, the PIC16C5X single chip microcomputer instructs the calculator PC to be set to all “1”, so the reset addresses of PIC16C5X chips are:

PIC16C54/55:1FFH

PIC16C56:3FFH

PIC16C57/58:7FFH

Generally speaking, the source program of PIC MCU does not require a unified format. You can write it according to your own style. But here we recommend a 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 storing the program at 0000H

;—————————————–

; Subroutine area

;—————————————–

DELAY MOVLW 255

RETLW 0

;——————————————

; Main program area

;——————————————

MAIN

MOVLW B‘00000000’

TRIS RB       ; RB has been defined as 6 by pseudo instruction, i.e. port B

LOOP

BSF RB,7 CALL DELAY

BCF RB,7 CALL DELAY

GO TO LOOP

;——————————————-

END       ; Program end

Note: the main label must be in page 0.

2. Fundamentals of PIC MCU programming

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

The I / O ports of PIC16C5X single chip microcomputer are bidirectional programmable, that is, each I / O end line can be set as input or output by program separately. This process is realized by writing the I / O control register Tris F. if the write value is “1”, it is the input; If the write value is “0”, it is output.

MOVLW 0FH  ; 0000 1111(0FH)

Input and output

TRIS 6    ; Write 0Fh in w to the B-port controller,

; The upper 4 bits of port B are output and the lower 4 bits are input.

MOVLW 0C0H ; 11 000000(0C0H)

RB4, RB5 output 0, rb6, RB7 output 1

2) Check whether the register is zero

If you want to judge whether the content of a register is zero, it is very simple. Now take register F10 as an example:

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

BTFSS STATUS,Z    ; Jump if F10 is zero

GOTO NZ        ; Z = 0, i.e. F10 is not zero, transfer to the program at label NZ

┋          ; Z = 1, i.e. F10 = 0 handler

3) Compare the size of the two registers

To compare the size of the two registers, you can subtract them and judge them according to the status bit C. Note that if the result of subtraction is put into W, the original value of the second register will not be affected.

For example, 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

GO TO F8=F9

BTFSC STATUS,C    ; Jump if C = 0

GO TO F9》F8       ; C = 1, the subtraction result is positive, F9 “F8”

GOTO F9《

F9       ; C = 0, the subtraction result is negative, F9 ┋

4) Procedure for N cycles

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

COUNT EQU 10     ; Define F10 as count (counter)

MOVLW 8

MOVWF COUNT LOOP   ; Circulatory body

LOOP

DECFSZ COUNT,1    ; Count minus 1. If the result is zero, skip

GOTO LOOP       ; The result is not zero, continue the cycle

┋       ; The result is zero, jump out of the loop

5) Program in “if… Then…” format

Take the format “if x = y then goto next” 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, skip to next to execute.

┋       ; X≠Y

6) Program in “for… Next” format

The “for… Next” program makes the loop within a certain range. The following example is a program in the format of “for x = 0 to 5”. 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 = 5 (no)

BTFSS STATUS,Z

GOTO LOOP        ; X < 5, continue the cycle

┋       ; X = 5, end cycle

7) Program in “do while… End” format

The “do while… End” program executes the loop under the conditions. The following example is a program in the format of “do while x = 1”. F10 puts the value of X.

Source; 21ic

Leave a Reply

Your email address will not be published. Required fields are marked *