The source program of the PIC series microcontroller refers to the program written in the mnemonic instructions of the PIC (assembly language program). Here we will briefly introduce the format requirements of the PIC source program and its establishment or writing.
Generally speaking, there is no unified format for the source program of PIC microcontrollers. Users can write according to their habits. Of course, they should comply with some assembly standards when writing. The following is an example of the assembly program of the PIC16F84 chip lighting up a light-emitting diode. This article introduces a method (format) of writing source programs for reference in practical applications.
Source code list
1. File TURNON.ASM
2Assembly code for PIC16F84 microcontroller
3Turns on an LED connected to B0
4Uses RC oscillator,about 100kHz
5. CPU configuration
6(it's a 16F84,RC oscillator
7watchdog timer off, power-up timer on)
8. Processor 16F84
9. include
10_config_RC_OSC&_WDT_OFF&_PWRTE_ON
11. Program
12.org 0; start at address 0
13At startup, all ports are inputs
14Set Port B to all outputs
15movlw B'00000000';w=binary 00000000
16tris Portb;copy w to port B control reg
17Put a 1 in the lowest bit of port B
18movlw B'00000001';w=binary 00000001
19movwf portb ;copy w to port B itself
20Stop by going into an endless loop
21. fin: goto fin
22. end; program ends here
The source program listing is now explained line by line.
The first line is the file name, here named TURNON.ASM. TURNON means a turn-on (i.e., turn on the LED) program. ASM is the extension of the source program.
The second line indicates that the source program is written in assembly code suitable for the PIC16F84 microcontroller.
Line 3 shows that the purpose of the program is to turn on the B0 LED of PIC16F84 PORTB.
Line 4 shows that the clock is supplied by an RC oscillator with a frequency of approximately 100kHz.
Lines 5, 6, and 7 describe the structural settings for the 16F84 CPU (using an RC oscillator, the watchdog timer is off, and the power-up timer is on. The structural settings for the 16F84 will be discussed in detail later).
Lines 8, 9, and 10 are pseudo instructions. They are used to provide the assembler with control command information on how to complete the assembly. They are both control instructions for the assembly process and operating instructions for the assembler. Here, lines 8, 9, and 10 also explain to the assembler that the microcontroller is 16F84, using an RC oscillator and the watchdog timer is turned off and not used, but the power-on 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 user instructions, 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 begins here.
Line 12 indicates that the program is stored in the program memory of the 16F84 microcontroller starting at address 0, which is also a pseudo instruction.
Lines 13 and 14 are comments, explaining the role of the following two instructions, 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, which is to store the binary (B) number "00000000" into the working register W.
Line 16 transfers the contents of the W register (i.e. 00000000) to the port B control register, making port B an output port. Lines 15 and 16 are both commented. Line 17 is a comment, indicating that the following two instructions make the lowest bit of port B 1.
The instruction on line 18 sends the binary number 00000001 into the working register W, and the instruction on line 19 copies the contents 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, causing the LED connected to bit B0 to light up.
Line 20 is another comment, which indicates that the following instruction will keep the B0 bit 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, and insert the programmed chip into the aforementioned circuit board. Add power to light up the LED at the B0 position of 16F84.
Previous article:Circuit diagram of intelligent humidity measuring instrument composed of humidity sensor and single chip
Next article:Design of a simple digital oscilloscope based on PIC microcontroller
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
- EEWORLD University ----PI power chip: Learn about BridgeSwitch in one minute
- CCS compilation optimization and volatile
- cc3200 GPIO input mode error
- Excellent materials, free of credits: Album of routines for common functional modules in the National Undergraduate Electronic Design Competition
- [Popular Science] IGBT - Starting from the name
- Relay protection device tester verification
- The circuit board cannot start normally after plugging in a power bank
- FPGA beginners ask for help with Verilog test code
- I found a very good 51 single-chip microcomputer tutorial book and recommend it to beginners for self-study of programming.
- PCB circuit board heat dissipation