Introduction to PIC Microcontroller Programming Style and Format

Publisher:InspiredDreamerLatest update time:2012-06-19 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First, let's look at a program:

Title '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:Introduction to PIC Microcontroller Programming Style and Format

Previous article:PIC microcontroller read/modify/write problems and solutions
Next article:Analysis of bank and PC errors in PIC microcontrollers

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号