The source program of the PIC series microcontroller refers to the program written by the mnemonic instructions of the PIC (assembly language program). Here we will briefly introduce the format requirements of the PIC source program and the establishment or writing of the source program.
Generally speaking, there is no unified writing format for the source program format of the PIC microcontroller. Users can write according to their habits. Of course, some assembly specifications should be followed when writing. The following uses the assembly program example of the PIC16F84 chip to light up a light-emitting diode to introduce a method (format) of source program writing for reference in practical applications.
Source program listing
1File TURNON.ASM
2Assembly code for PIC16F84 microcontroller
3Turns on an LED connected to B0
4Uses RC oscillator,about 100kHz
5CPU configuration
6(it's a 16F84,RC oscillator
7watchdog timer off, power-up timer on)
8processor 16F84
9include
10_config_RC_OSC&_WDT_OFF&_PWRTE_ON
11Program
12org 0; start at address 0
13At startup, all ports are inputs
14Set Port B to all outputs
15m ovlw B'00000000'; w=binary 00000000
16tris Portb; copy w to port B control reg
17. Put a 1 in the lowest bit of port B.
18. movlw B'00000001'; w = binary 00000001
19. movwf portb; copy w to port B itself
20. Stop by going into an endless loop
21. fin: goto fin
22. end; program ends here
Now we will explain the source program line by line.
The first line is the file name, which is named TURNON. ASM. TURNON means a turn-on (i.e., turn on LED) program. ASM is the extension of the source program.
The second line shows that it is a source program written in assembly code suitable for PIC16F84 microcontroller.
The third line shows that the purpose of the program is to turn on the B0 bit LED of PIC16F84 PORTB.
The fourth line shows that the clock is provided by the RC oscillator with a frequency of about 100kHz.
Lines 5, 6, and 7 explain the structural settings of the 16F84 CPU (using an RC oscillator, the watchdog timer is off, and the power-up timer is on. The structural settings of the 16F84 will be discussed in detail later).
Lines 8, 9, and 10 are pseudo instructions. This is used to provide the assembler with control command information on how to complete the assembly. It is both a control instruction for the assembly process and an operating instruction for the assembler. Here, lines 8, 9, and 10 also explain to the assembler that the microcontroller is a 16F84, using an RC oscillator, and the watchdog timer is turned off and not used, but the power-up timer is turned on and used. Comparing lines 5 and 6, it can be seen that the two have the same content, but one is a comment specifically for the user, and the other is a pseudo instruction, which provides command information for the PC assembler.
Line 11 is another comment, indicating that the main body of the program starts here.
Line 12 indicates that the program is stored in the program memory of the 16F84 microcontroller starting from address 0, which is also a pseudo instruction.
Lines 13 and 14 are comments, explaining the role of the following instructions (two sentences), that is, at startup, all ports are input, and then port B is set to full output (that is, all bits are output).
Line 15 is the first instruction written in assembly language, that is, the binary (B) number "00000000" is stored in the working register W.
Line 16 transfers the content of the W register (that is, 00000000) to the control register of port B, making port B an output port. Lines 15 and 16 are both commented. Line 17 is a comment, explaining that the following two instructions are to make the lowest bit of port B 1.
The instruction in line 18 sends the binary number 00000001 to the working register W, and the instruction in line 19 copies the content of the W register to port B, then the lowest bit B0 of port B is high, and the rest of the bits are low, making the LED connected to bit B0 light up.
Line 20 is another comment. It means that the following instructions will make bit B0 continue to be high.
The instruction in line 21 is an infinite loop statement, which keeps the output of port B1 high.
Line 22 is the pseudo instruction END, indicating that the program ends here.
After assembling the programs listed in the list on a PC using an assembly software called MPASM, if there are no errors, you can get the corresponding machine code file with the extension name HEX. With the help of a programmer, burn the corresponding content of the file into the program memory of the PIC16F84 microcontroller, insert the programmed chip into the aforementioned circuit board, and add power to light up the LED at position B0 of the 16F84.
Reference address:Composition of PIC series 8-bit microcontroller source program
Generally speaking, there is no unified writing format for the source program format of the PIC microcontroller. Users can write according to their habits. Of course, some assembly specifications should be followed when writing. The following uses the assembly program example of the PIC16F84 chip to light up a light-emitting diode to introduce a method (format) of source program writing for reference in practical applications.
Source program listing
1File TURNON.ASM
2Assembly code for PIC16F84 microcontroller
3Turns on an LED connected to B0
4Uses RC oscillator,about 100kHz
5CPU configuration
6(it's a 16F84,RC oscillator
7watchdog timer off, power-up timer on)
8processor 16F84
9include
10_config_RC_OSC&_WDT_OFF&_PWRTE_ON
11Program
12org 0; start at address 0
13At startup, all ports are inputs
14Set Port B to all outputs
15m ovlw B'00000000'; w=binary 00000000
16tris Portb; copy w to port B control reg
17. Put a 1 in the lowest bit of port B.
18. movlw B'00000001'; w = binary 00000001
19. movwf portb; copy w to port B itself
20. Stop by going into an endless loop
21. fin: goto fin
22. end; program ends here
Now we will explain the source program line by line.
The first line is the file name, which is named TURNON. ASM. TURNON means a turn-on (i.e., turn on LED) program. ASM is the extension of the source program.
The second line shows that it is a source program written in assembly code suitable for PIC16F84 microcontroller.
The third line shows that the purpose of the program is to turn on the B0 bit LED of PIC16F84 PORTB.
The fourth line shows that the clock is provided by the RC oscillator with a frequency of about 100kHz.
Lines 5, 6, and 7 explain the structural settings of the 16F84 CPU (using an RC oscillator, the watchdog timer is off, and the power-up timer is on. The structural settings of the 16F84 will be discussed in detail later).
Lines 8, 9, and 10 are pseudo instructions. This is used to provide the assembler with control command information on how to complete the assembly. It is both a control instruction for the assembly process and an operating instruction for the assembler. Here, lines 8, 9, and 10 also explain to the assembler that the microcontroller is a 16F84, using an RC oscillator, and the watchdog timer is turned off and not used, but the power-up timer is turned on and used. Comparing lines 5 and 6, it can be seen that the two have the same content, but one is a comment specifically for the user, and the other is a pseudo instruction, which provides command information for the PC assembler.
Line 11 is another comment, indicating that the main body of the program starts here.
Line 12 indicates that the program is stored in the program memory of the 16F84 microcontroller starting from address 0, which is also a pseudo instruction.
Lines 13 and 14 are comments, explaining the role of the following instructions (two sentences), that is, at startup, all ports are input, and then port B is set to full output (that is, all bits are output).
Line 15 is the first instruction written in assembly language, that is, the binary (B) number "00000000" is stored in the working register W.
Line 16 transfers the content of the W register (that is, 00000000) to the control register of port B, making port B an output port. Lines 15 and 16 are both commented. Line 17 is a comment, explaining that the following two instructions are to make the lowest bit of port B 1.
The instruction in line 18 sends the binary number 00000001 to the working register W, and the instruction in line 19 copies the content of the W register to port B, then the lowest bit B0 of port B is high, and the rest of the bits are low, making the LED connected to bit B0 light up.
Line 20 is another comment. It means that the following instructions will make bit B0 continue to be high.
The instruction in line 21 is an infinite loop statement, which keeps the output of port B1 high.
Line 22 is the pseudo instruction END, indicating that the program ends here.
After assembling the programs listed in the list on a PC using an assembly software called MPASM, if there are no errors, you can get the corresponding machine code file with the extension name HEX. With the help of a programmer, burn the corresponding content of the file into the program memory of the PIC16F84 microcontroller, insert the programmed chip into the aforementioned circuit board, and add power to light up the LED at position B0 of the 16F84.
Previous article:The basic format of PIC microcontroller programming
Next article:Assembly language for PIC 8-bit microcontrollers
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- The STM32 branch adds receive buffer setting parameters during serial port initialization
- EEWORLD core points are online, and the rules for adding points are announced~ It concerns every EE user
- 【Silicon Labs Development Kit Review】IDE Installation and Testing
- The role of parallel resistance and capacitance in signal lines
- How to use IAR for 430 software for beginners
- [RISC-V MCU CH32V103 Review] ---Advancing Dimension---Unboxing
- How to solve the strange phenomenon of CCS
- Disassembly report | Honor X20 SE + OPPO A74 + Redmi Note 10 Pro
- USART_SendArray(DEBUG_USARTx, a,10); failed to output the elements in a[10]?
- Merry Christmas