GOTO F9>F8; C=1 subtraction result is positive, F9>F8
GOTO F9<
4) Program looping n times
If you want to loop a certain program n times, you can use a register as a counter. The following example uses F10 as a counter to loop 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, if the result is zero, jump to
GOTO LOOP; if the result is not zero, continue looping
┋; if the result is zero, jump out of the loop
5) Program in "IF...THEN..." format
The following takes 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) "FOR...NEXT" format program
"FOR...NEXT" program makes the loop execute 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=5 No)
BTFSS STATUS, Z
GOTO LOOP ;X<5, continue loop┋
;X=5, end loop7
) "DO WHILE...END" format program
"DO WHILE...END" program is to execute the loop when the condition is met. The following example is a "DO WHILE X=1" format program. 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 continue
loop┋ ;X≠1 jump out of
loop8) Table lookup program
Table lookup is an operation often used in programs. The following example converts decimal 0~9 into 7-segment LED digital display values. If RB0~RB6 of port B are used to drive the a~g line segments of the LED, the following relationship exists:
Assuming the LED is a common anode, the line segment values corresponding to the numbers 0~9 are as follows:
Decimal
|
Segment Value
|
Decimal
|
Segment Value
|
0
|
C0H
|
5
|
92H
|
1
|
C9H
|
6
|
82H
|
2
|
A4H
|
7
|
F8H
|
3
|
B0H
|
8
|
80H
|
4
|
99H
|
9
|
90H
|
The table lookup program of PIC microcontroller can be realized by using the feature of subroutine with value return. Specifically, in the main program, first get the table data address and put it into W, then call the subroutine. The first instruction of the subroutine puts W into PC, then the program jumps to the data address, and then the "RETLW" instruction puts the data into W and returns to the main program. The following program uses F10 to put the table header address.
MOVLW TABLE ;Table header address→F10
MOVWF 10
┋
MOVLW 1 ;1→W, prepare to get the line segment value of "1"
ADDWF 10,1 ;F10+W = "1" data address
CALL CONVERT
MOVWF 6 ;Set the line segment value to port B, light up the LED
┋
CONVERT MOVWF 2 ;W→PC TABLE
RETLW 0C0H ;"0" line segment value
RETLW 0F9H ;"1" line segment value
┋
RETLW 90H ;"9" line segment value
9) "READ...DATA, RESTORE" format program
"READ...DATA" program reads one data in the data table each time, then adds 1 to the data pointer, and prepares to get the next data. In the following program, F10 is used as the starting address of the data table, and F11 is used as the data pointer.
POINTER EQU 11 ; define F11 name as POINTER
┋
MOVLW DATA
MOVWF 10 ; data table 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 ; call subprogram, get table data
┋
CONVERT MOVWF 2 ; data address → PC
DATA RETLW 20H ; data
┋
RETLW 15H ; data
If you want to execute "RESTORE", just execute one "CLRF POINTER".
10) Delay program
If the delay time is short, you can let the program simply execute several no-operation instructions "NOP" in succession. If the delay time is long, you can use a loop to implement it. The following example uses F10 to calculate and repeat the loop 100 times.
MOVLW D'100'
MOVWF 10
LOOP DECFSZ 10, 1; F10—1→F10, if the result is zero, jump to
GOTO LOOP
┋
The delay time is the sum of the execution time of the instructions in the delay program. If a 4MHz oscillation is used, each instruction cycle is 1μS. Therefore, the single-cycle instruction time is 1μS, and the double-cycle instruction time is 2μS. In the above example, the LOOP loop delay time is: (1+2)*100+2=302 (μS). Inserting a no-operation instruction in the loop can extend the delay time:
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).
Using several loop nesting methods can greatly extend the delay time. The following example uses 2 loops to 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 PIC microcontroller RTCC counter
RTCC is a pulse counter. Its counting pulse has two sources, one is the external signal input from the RTCC pin, and the other is the internal instruction clock signal. The program can be used to select one of the signal sources as input. RTCC can be used by the program for timing; the program reads the RTCC register value to calculate the time. When RTCC is used as an internal timer, the RTCC pin must be connected to VDD or VSS to reduce interference and current consumption. The following program uses RTCC for delay:
RTCC EQU 1
┋
CLRF RTCC; RTCC cleared to 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 increases by 1 every 256 instruction cycles (division ratio = 1:256). Assuming 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 after a period of time and detect its count value.
12) Addressing of register bank (BANK)
For PIC16C54/55/56, there are 32 registers and only one bank (BANK), so there is no bank addressing problem. For PIC16C57/58, there are 80 registers, divided into 4 banks (BANK0-BANK3). From the description of F4 (FSR), we know that bit6 and bit5 of F4 are register addressing bits, and their corresponding relationship is 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 and reset, bit6 and bit5 of F4 are random, and non-powered reset keeps the original state unchanged.
The following example writes data to the 30H and 50H registers of BANK1 and BANK2.
Example 1. (Assume that the current bank is selected 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 bank (BANK), we must first operate the bank addressing bit in F4. In actual applications, generally after power-on reset, bit6 and bit5 of F4 are cleared to 0 first, so that it points to BANK0, and then it is pointed to the corresponding bank as needed.
Note that in the example, when writing data to the 30H register (BANK1) and the 50H register (BANK2), the register address in the instruction "MOVWF 10H" is "10H", not "MOVWF 30H" and "MOVWF 50H" as expected by the reader. Why?
Let's review the instruction table. In all the instruction codes related to the registers of PIC16C5X, the register addressing bits only occupy 5 bits: fffff, and can only address 32 (00H-1FH) registers. Therefore, to address 80 registers, the two-bit body addressing bits PA1 and PA0 are used. When we set the body addressing bits PA1 and PA0 to point to a BANK, the instruction "MOVWF 10H" will put the content of W into the corresponding register in this BANK (10H, 30H, 50H, or 70H).
Some designers are in contact with the concept of bank address selection for the first time, and inevitably have different understandings. The following is an example:
Example 2: (assuming that the current bank address selection is BANK0)
MOVLW 55H
MOVWF 30H; to change the register from 55H to 30H
MOVLW 66H
MOVWF 50H; to change the register
from 66H to 50H It is wrong to think that "MOVWF 30H" can definitely put W into 30H, and "MOVWF 50H" can definitely put W into 50H. Because the actual effect of these two instructions is "MOVWF 10H", the reason has been explained above. So the final result of this program in Example 2 is F10H=66H, and the real F30H and F50H are not operated.
Suggestion: In order to make the bank address selection program clear, it is recommended to use more name definition symbols to write the program, so that it is not easy to confuse. Example 3: Assume that the following registers of BANK0, BANK1, and BANK2 are used in the program:
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
┋
If the program is written in this way, it is believed that the body address selection will not be easy to make mistakes.
13) Program cross-page jump and call
The following introduces the page concept of the program storage area of the PIC16C5X microcontroller and the application examples of the two page address bits PA1 and PA0 in the F3 register.
(1) "GOTO" cross-page
Example: Suppose the current program is on page 0 (PAGE0), and you want to use "GOTO" to jump to a certain place
KEY (PAGE1)
on page
1.
STATUS EQU 3
PA1 EQU 6
PA0 EQU 5
┋
BSF STATUS, PA0; PA0=1, select PAGE page
GOTO KEY; jump to page 1 KEY
┋
KEY NOP; program on page 1
┋
(2) "CALL" cross-page
Example: Suppose the current program is on page 0 (PAGE0), and now you want to call the subroutine DELAY placed on page 1 (PAGE1).
┋
BSF STATUS, PA0; PA0=1, select PAGE1 page
CALL DELAY; cross-page call
BCF STATUS, PA0; restore page 0 address
┋
DELAY NOP; subroutine of page 1
┋
Note: the program sets the page address for cross-page CALL. After returning from the subroutine, the original page address must be restored.
(3) Writing cross-page jumps and calls in the program
Readers must ask when they see this: 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 going to cross pages, and that CALL is going to cross pages? Indeed, when you start writing the source program, you don’t know when a cross-page jump or call will occur, but when you assemble the source program, it will be automatically given. When the assembly result shows:
XXX (address) "GOTO out of Range"
XXX (address) "CALL out of Range",
this indicates that your program has cross-page jumps and calls, and the corresponding page addresses have not been set before these cross-page GOTO and CALL in your program. At this time, you should check the .LST file generated by the assembly, find these GOTO and CALL, and check which page the address they are going to jump to is on, and then return to the source program (.ASM) to make necessary changes. Until your source program passes the assembly (0 Errors and Warnnings).
(4) Connection of program pages
Some processing should be done at the connection of the four program pages. It is generally recommended to use the following format: That is, after entering another page, immediately set the corresponding page address bit (PA1, PA0). Page processing is the most troublesome part of PIC16C5X programming, but it is not difficult. As long as you do a practical programming exercise, you can master it.
Previous article:Oscillator Configuration Method of PIC Series Microcontroller
Next article:PIC series microcontroller programming basics
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Analysis of Typical Anti-Reverse Connection Circuits for Automotive Electronics
- Consulting employment
- [Runhe Neptune Review] Four network communications
- EEWORLD University Hall----What can universal fast charging bring?
- Complete learning manual for using RT-Thread on RISC-V (based on Longan development board)
- How does Tbox protect itself to ensure data security? What good methods can you recommend?
- Byte/word alignment issues in DSP
- Pre-registration for the live broadcast with prizes: Cytech & ADI discuss with you Gigabit digital isolators for video, converters, and communications
- GPIO Operation of TMS320C6748
- Gigabit Ethernet Courseware.zip