PIC microcontroller programming format and style

Publisher:创新之梦Latest update time:2020-02-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First, let's look at a program:

ittle 'Input.asm' ;Title

This is a simple input experiment

include

COUNT EQU 20H; define the register name 20H as COUNT

ORG 0; The program area will be loaded from 0000H

GOTO START

ORG 4 ;Interrupt entry

GOTO INTEN ; Jump to INTEN when an interrupt occurs

ORG 10 ;START will start from line 10

START

MAIN

GOTO MAIN

INTSEVER

RETFIE ;Interrupt return

END; Program end pseudo instruction, can not be missing!


In fact, the assembler does not have a fixed format, but its parts are arranged in a certain position, usually in the following order:

①Program title: defined by the Title pseudo-instruction, can be defaulted;

②Program comments: Please note that you must use a ";" sign before the program description and comment part, otherwise a compilation error will occur. The program description and comment part can be omitted;

③ The called .inc file: Use the include pseudo-instruction to call. Usually the .inc file corresponds to the microcontroller model, including the definition of some special registers and other contents. The .inc files of various models of PIC microcontrollers can be found in the MPLAB directory, and there is no need to write them yourself. If the called .inc file is lost, a compilation error will occur during compilation;

④General register definition: Defined by EQU pseudo-instruction, the format is "register name EQU register label", and the register name should be written in top space. This is the name given by the programmer to the general register used, corresponding to an actual general register address;

⑤Macro definition: No macro is used in the above program, default;

⑥Program initialization: START part;

⑦Main program: MAIN part;

⑧ Subroutines: such as the INTSEVER part of this program. Sometimes subroutines are placed before the initialization program, such as some table lookup programs;

⑨Program end: END statement, cannot be omitted.


The above parts constitute a complete PIC assembler program. I think the following points should be paid attention to during the program writing process:

1. In programming, we must first avoid using direct addresses to operate registers. Using direct addresses to operate registers will greatly reduce the readability of the program. First, it is easy to confuse with data, and second, it is not easy to change. Take the above program as an example. The direct address of the register "COUNT" is 20H. If during debugging, I suddenly find that the other registers in the subroutine called to COUNT are all in BANK1. It is very inconvenient for me to frequently switch banks to call it, so I have to change its address to 90H. Now I just need to change the definition statement to "COUNT EQU 90H". If I don't define it this way, I can only look for 20H in the program and change it to 90H one by one...


2. Pay attention to the readability of subroutine labels. Although these labels are no different after being compiled (of course, the addresses are still different), they are just marks used by programmers to identify (for example: main is usually used as the main loop label of the program, and start is often used as the label for program initialization. It can be said that the naming of labels has little impact on the compilation and operation of the program. If your main label is changed to loop, the compilation result is the same), but their rationality will affect the debugging of the program. Generally speaking, it is best for the label to be clear at a glance, and even to summarize the content of the program processing. For example, dealy is used for delay programs, intsever is used for interrupt services, and so on. This will increase the readability of the program and make it easier to modify and debug it in the future.


3. The program structure should be reasonable.

Reference address:PIC microcontroller programming format and style

Previous article:Bank and PC issues in PIC microcontrollers
Next article:PIC microcontroller data storage problem

Latest Microcontroller Articles
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号