PIC microcontroller programming analysis

Publisher:创意驿站Latest update time:2011-09-26 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
PIC microcontrollers use a reduced instruction set. For example, the PIC16F716 microcontroller has only 35 single-byte instructions. To achieve complex control or calculation with so few instructions, it is obvious that more work needs to be done on program design. The following are some opinions on the issues that need to be paid attention to in program design. The CMOS process PIC series microcontrollers developed by Microchip, USA, especially the microcontrollers with built-in second-generation Flash memory, are unique in terms of rapid application. Most of the PIC microcontroller series include common interfaces such as arithmetic units, memory, A/D, PWM, input and output I/O, and communication. The free and flexible definition of functions can adapt to different control requirements.

1. Distinguish between upper and lower case instructions
When writing the source program of the PIC microcontroller, in addition to the strict list instructions at the beginning of the source program, you must also pay attention to the upper and lower case rules of the letters and symbols in the source program, otherwise the program will not be successfully assembled on the PC. The pseudo-instruction INCLUDE is used in the source program. This instruction inserts the microcontroller file specified in the list (in MPLAB) into the source program as part of the source program, so all the registers related to the microcontroller in MPLAB do not need to be assigned with the assignment instruction (EQU) in the source program, which greatly simplifies the established source program.
In addition, due to the pseudo-instruction INCLUDE, according to the format in the MPLAB software, the operands in the source program that involve the register names specified by MPLAB can only be capitalized, not lowercase. The rest of the opcodes and symbol letters can be arbitrarily capitalized, but the X in 0x should be lowercase. Otherwise, the assembly will not succeed. In view of the above reasons, for the convenience of writing, when using the MPLAB software, it is advisable to use uppercase letters for the source program of the PIC microcontroller (except 0x).
The following example is about the realization of robot control. The control part adopts PIC16F7X series microcontroller, which is programmed in assembly language and runs fast, meeting the requirements of the system.

2. Use of action flags
In the whole control, there are many combined actions. When all action positioning is controlled by photoelectric switches, there are some problems in program writing. For example, if the left hand is required to rise to the applause position and the right hand is required to rise to the hand-raising position (the initial position of the hand is at the lowest lower position), the photoelectric switch 0 is valid (that is, it is blocked when it is 0), and it reaches the correct position. With a simple understanding, it can be written as the following program:
list P=16c73
call lefthandup
call righthandup
L0 call readinsignal
bdss csl_v,lefthandligbts
call lefthandstop
bfsc csl_v,righthandlight4
goto L0
call righthandstop
L1 call readinsignal
bfsc csl_v,lefthandlight3
goto L1
call lefthandstop
lefthandlight represents the photoelectric switch, which is used to judge whether it has reached the corresponding position. 1 represents the lowest position of the arm; 2 represents the handshake position of the arm; 3 represents the applause position of the arm; 4 represents the high hand-raising position of the arm. The above program describes the process of the left arm rising to the hand-raising position and the right arm rising to the applause position and stopping. First, determine whether the left hand has reached the position. If it has reached the position, the left hand stops. Then check whether the right hand has reached the hand-raising position. If it has reached the position, it stops. Otherwise, the above detection is repeated until the left hand reaches the applause position and the right hand reaches the hand-raising position.
Note that 3 and 4 here represent the applause position and the hand-raising position. After the cyclic detection, the arm can stop at each position. However, the mechanical action has inertia, and the mechanical stop position may be a little above or below the position, which affects the following actions. The mechanical action may malfunction after several actions, that is, the program cannot run normally. In this case, it is necessary to modify the program writing method and use the flag bit to control the action. If the control flag bit is used, the flag position must be zeroed in the action sub-function.

3. Differentiate between the use occasions of GOTO and CALL instructions
In PIC assembly programs, CALL and GOTO instructions are used frequently and are easy to confuse. Generally, CALL instructions are mostly used between subprograms and main programs; while GOTO instructions are mostly used between state transition modules, that is, entering another state from one state without returning. Since the stack of PIC microcontrollers is limited, GOTO statements cannot be used endlessly in the program, otherwise the stack will overflow and the program will not run normally. The internal loops of each small program occupy a small number of stack levels, so it is feasible to use GOTO instructions, but in large programs, using GOTO cannot return to the next instruction of the program before the call. After the CALL instruction completes the call to the subroutine, it returns to the program before the call. For example, in ultrasonic detection, the program is as follows:
list D=16c76
start:call setcpu
call automaflsmstate1
L3 call readinsignal
bfsc es2_v,ultrasonicdetect1
goto L3
goto automatlsmstate2:
automatlsmstate2:
return
automafismstate1 and automatlsmstate2 represent two states, and ultrasonicdetect1 represents an input ultrasonic detection signal. The above program describes the call to the automatlstmstatei state. After execution, the following detection uhrasonicdetect1 is performed. If there is no trigger, the detection will continue to loop. If triggered, the automatlsmstate2 state will be entered, and the program will not return to the following program after execution.
Since the stack of the PIC microcontroller is limited, the GOTO instruction cannot be used endlessly in the program, otherwise the stack will overflow and the program will not run normally. However, in some cases, such as when the program branches, the GOTO instruction has to be used. For the PIC16F7X series microcontroller, when the program branches, it can only be judged by the z bit or c bit of the STATUS register. In the first case, the GOTO instruction must be used to transfer; otherwise, after the first case is executed, the second case will be executed immediately. Therefore, when using assembly language for programming, the program should be broken down into subroutines at different levels; then calls are made between programs to minimize the range of the GOTO instruction jump.

4. Pay attention to the different usage of status flags Z and C
When judging flags, Z (zero flag) and C (borrow flag) are different. When Z is 1, it means the result above is 0. When Z is 0, the result is not 0. When C is 1, there is a borrow. When C is 0, there is no borrow. When using a timer, the C flag is generally used. This is because when a certain action is completed to check the timer, the time may not have arrived, or it is just right, or it has exceeded the time. As long as the time has arrived or exceeded, the timer must be turned off as required, as described in the following program. If the Z flag is used, it may not be detected when it is equal to 0, and the stop state cannot be determined. Using Z can control the timing well. Z is mostly used for general calculations, such as the previous action flag.
list D=16c76
call openfimer0
L4 movlw d'30'
subwf t0_v2,W
bfsc status,c
goto L4
call dosetimer0

The program checks whether the time has reached 1.5s. If not, it will wait in a loop. If it has reached or the detection time has passed, it will turn off the timer and execute the following program.

In short, using appropriate methods in programming PIC microcontrollers can make the entire program run stably, and the use of program space will also be reduced, avoiding bugs in debugging.

Reference address:PIC microcontroller programming analysis

Previous article:Design of triple speed sampling method for serial asynchronous communication based on PIC software
Next article:Temperature measurement system based on temperature sensor with PWM output

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号