PIC8-bit microcontroller source program writing method

Publisher:EuphoricMelodyLatest update time:2013-01-09 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

2Assembly code for PIC16F84 microcontroller

3Turns on an LED connected to B0

4Uses RC oscillator,about 100kHz

5. CPU configuration

6(it's a 16F84,RC oscillator

7watchdog 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

13At startup, all ports are inputs

14Set Port B to all outputs

15movlw B'00000000';w=binary 00000000

16tris 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

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.

Reference address:PIC8-bit microcontroller source program writing method

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号