This article summarizes some of the experience and skills of the author PIC microcontroller development process for peer reference. 1 How to further reduce power consumption Power consumption, an important consideration in battery-powered instrumentation. The PIC16C×× series MCUs have lower power consumption (5V, operating current is less than 2mA at 4MHz oscillation frequency). In order to further reduce power consumption and ensure that the working requirements are met, the method of reducing the operating frequency can be adopted. The operating frequency can be greatly reduced (for example, PIC16C××3V, working at 32kHz, the current can be reduced to 15μA), but Low operating frequencies may cause some subroutines (such as mathematical calculations) to take more time. In this case, when the oscillating mode of the single chip adopts the RC circuit form, it can be solved by increasing the working frequency in the middle. Example 1 (written in Mplab-C) Example 2 (written in Masm) 4000 Puffs Vape,Colored E-Cigarette,Electronic Cigarettes Vape,Vape Pen Vaporizer Guangzhou Yunge Tianhong Electronic Technology Co., Ltd , https://www.e-cigarettesfactory.com
The specific method is to idle a resistor (R1) between an I/O pin (such as RB1) and the OSC1 pin, as shown in Figure 1. The low speed state sets RB1=0. When fast calculation is required, RB1=1 is set first. When charging, the capacitor voltage rises rapidly, the operating frequency increases, the operation time decreases, and RB1=0 is set at the end of the operation, and the low-speed and low-power state are entered. The amount of change in operating frequency depends on the resistance of R1 (note that R1 cannot be selected too small, the oscillation circuit does not vibrate, and generally selects more than 5kΩ).
In addition, further reduction in power consumption can make full use of the "sleep" instruction. Execute the "sleep" command, the machine is in sleep state, and the power consumption is several microamps. The program can be used to wait for events using the "sleep" command, or it can be used in a delayed program (see Example 1, Example 2). Using the "sleep" instruction in the delay program to reduce power consumption is one aspect. At the same time, it is the off state. The port B port level change can wake up "sleep" and end the delay program early. This is especially useful for some applications. At the same time, pay attention to the relationship between WDT and interrupt when using "sleep".
Delay() Delay
{ ; This line can add switch interrupt instruction / * This line can add switch interrupt command * / movlw.10
For (i=0; i<=10; i++) movwf Counter
SLEEP(); Loop1
} Sleep
Decfsz Counter
Goto Loop1
Return
2 Note that the RBIF bit in INTCON has an interrupt enable bit in INTCON that has no effect on the interrupt status bit. When PORT B ​​is configured for input mode, the RB<7:4> pin inputs are sampled for each read cycle and compared to the old latch value. Once different, a high level is generated, setting RBIF=1. Before the RB interrupt is opened, perhaps RBIF has been set to "1". When the RB interrupt is opened, the RBIF bit should be cleared first to avoid the original value of RBIF, and it is better to clear the RBIF bit after the interrupt processing is completed.
3 Pay attention to the problem when writing PIC microcontroller program in Mplab-C high-level language. Note the writing format when embedding assembly instructions in the program. See Example 3.
Example 3
...... ......
While(1) {#asm while(1) {
...... #asm /* should start another line*/
#endasm ......
}/* does not compile correctly */ #endasm
...... }/*Compile*/
......
When the assembly instruction is embedded, each instruction from "#asm" to "endasm" must be on a separate line, otherwise an error will occur during compilation.
3.2 The most safe representation of addition and multiplication is shown in Example 4.
Example 4
#include<16c71.h>
#include
Unsigned int a, b;
Unsigned long c;
Void main()
{ a=200;
b=2;
c=a*b;
} /* less than correct result c=400*/
The reason is that Mplab-C compiles c=a*b in 8×8 multiplication, returns a single-byte result to c, and the result overflow is ignored. In the above example, the expression "c=a*b;" is "c=a;c=c*b;", which is the safest (for the addition processing as above).
3.3 Understanding the multiply-divide function The register occupies only a few tens of bytes of PIC on-chip RAM. The space is especially valuable, and the Mplab-C compiler has no release to the RAM address, that is, a variable address can no longer be allocated to other variables. If the RAM space cannot meet too many variable requirements, some variables can only be used alternately by the user to force the same RAM space. The Mplab-C multiplication and division function needs to borrow the RAM space to store the intermediate result. When the multiplication and division function occupies the RAM and the user variable address overlap, it will lead to unpredictable results. The multiplication and division operation is used in the C program. It is best to first program the machine code disassembly code (including the generated LST file) to check whether the multiplication and division address is conflicting with the variable address, so as to avoid the program running away. The Mplab-C manual does not give its multiplication and division function to the specific RAM address occupancy. Example 5 is the occupancy of the 0x13, 0x14, 0x19, and 0x1A addresses of the multiplication function.
Example 5
Partially disassembled code #include 01A7 081F MOVF 1F,W
#include 01A8 0093 MOVWF 13
Borrow unsigned long Value @0x1 01A9 0820 MOVF 20,W
Char Xm @0x2d; 01AA 0094 MOVWF 14
; borrow void main() 01AB 082D MOVF 2D,W
{Value=20; 01AC 0099 MOVWF 19
Borrow Xm=40; 01AD 019A CLRF1A
; borrow Value=Value*Xm 01AE 235F CALL 035Fh
; call the multiplication function... 01AF 1283 BCF 03,5
} 01B0 009F MOVWF 1F
; return result low byte 01B1 0804 MOVF 04, W
01B2 00A0 MOVWF 20
Return result high byte 4 Repeat programming for the chip For users without hardware emulator, always use the EPROM chip to debug the program. Every time the program is changed, the original content is erased and then programmed, which wastes a considerable amount of time and shortens the life of the chip. The result of the latter programming is the same as the previous one. Only when the same bit of the corresponding machine code byte is changed from "1" to "0", the data can be written again on the previous programming chip without erasing the original content.
During the debugging process, constant adjustments are often encountered. For example, if the constant change can ensure that the corresponding bit changes from "1" to "0", the original content can continue to be programmed. In addition, the instruction "NOP" corresponds to the machine code as "00". During the debugging process, the instruction is deleted. First, the "NOP" instruction is used instead. After compiling, the original film content can be continued.
In addition, when programming with the EPROM chip, pay special attention to the program security status bit. The manufacturer has changed the security status of the new generation EPROM chip from the original EPROM erasable type to fuse type. Once the program code security fuse is programmed to "0", the EPROM chip can be reprogrammed without reprogramming. Pay attention to this when using it to avoid unnecessary waste (this is not explained in the Microchip data).