The PIC series of microcontrollers produced by Microchip Corporation of the United States have been widely used in industrial control, instruments, meters, communications, home appliances, toys and other fields due to their ultra-small size, low power consumption, low cost and wide variety. This article summarizes some of the author's experiences and skills in the development process of PIC microcontrollers for reference by peers.
1 How to further reduce power consumption
Power consumption is an important consideration in battery-powered instruments. The PIC16C×× series microcontrollers have low power consumption (the operating current is less than 2mA at 5V, 4MHz oscillation frequency). To further reduce power consumption, the operating frequency can be reduced while ensuring that the working requirements are met. The reduction in operating frequency can greatly reduce power consumption (for example, when the PIC16C×× works at 3V, 32kHz, its current can be reduced to 15μA), but the lower operating frequency may cause some subroutines (such as mathematical calculations) to take up more time. In this case, when the oscillation mode of the microcontroller adopts the form of an RC circuit, the solution can be to increase the operating frequency midway. |
The specific method is to connect a resistor ( R1
)
between an idle I/O pin (such as RB1) and the OSC1 pin
,
as shown in Figure 1. In the low-speed state, set RB1=0. When fast calculation is required, set RB1=1 first. Since the capacitor voltage rises quickly during charging, the operating frequency increases and the calculation time is reduced. After the calculation is completed, set RB1=0 again to enter the low-speed and low-power state. The change in the operating frequency depends on
the resistance value of
R1
(note
that
R1
cannot be selected too small to prevent the oscillation circuit from not oscillating. Generally, it is selected to be greater than 5kΩ).
In
addition, the "sleep" instruction can be fully utilized to further reduce power consumption. When the "sleep" instruction is executed, the machine is in a sleep state and the power consumption is several microamperes. The program can use the "sleep" instruction not only in the standby state to wait for events, but also in the delay program (see Example 1 and Example 2). Using the "sleep" instruction in the delay program to reduce power consumption is one aspect. At the same time, even if the interrupt is turned off, the change in the level of Port B can wake up the "sleep" and end the delay program in advance. This is particularly useful in some applications. At the same time, pay attention to the relationship between WDT and interrupt when using "sleep".
|
Figure 1 Methods for increasing operating frequency |
Example 1 (written in Mplab-C) | Example 2 (written in Masm) | |
Delay() | Delay | |
{ | ; This line can add a switch interrupt instruction | |
/*This line can add a switch interrupt instruction*/ | movlw.10 | |
for (i=0; i<=10; i++) | movwf Counter | |
SLEEP(); | Walk1 | |
} | Sleep | |
decfsz Counter | ||
goto Loop1 | ||
return |
2 Note the RBIF bit in INTCON
The interrupt enable bits in INTCON have no effect on the interrupt status bits. When PORT B is configured as input mode, the RB<7:4> pin input is sampled and compared with the old latch value in each read operation cycle. Once different, a high level is generated and RBIF=1 is set. Before opening the RB interrupt, RBIF may have been set to "1", so when opening the RB interrupt, the RBIF bit should be cleared first to avoid being affected by the original value of RBIF. At the same time, it is best to clear the RBIF bit after the interrupt processing is completed.
3 Issues to note when using Mplab-C high-level language to write PIC microcontroller programs
3.1 Pay attention to the writing format when embedding assembly instructions in the program See Example 3.
Example 3
…… | …… | |
while(1) {#asm | while(1) { | |
…… | #asm /*Should start on a new line*/ | |
#endasm | …… | |
}/*Cannot compile correctly*/ | #endasm | |
…… | }/*Compilation passed*/ | |
…… |
When inlining assembly instructions, each instruction from "#asm" to "endasm" must occupy a line, otherwise an error will occur during compilation.
3.2 The safest representation of addition and multiplication See Example 4.
Example 4
#include<16c71.h> | |
#include | |
unsigned int a, b; | |
unsigned long c; | |
void main() | |
{ a=200; | |
b=2; | |
c=a*b; | |
} /*Cannot get the correct result c=400*/ |
The reason is that Mplab-C compiles c=a*b in 8×8 multiplication mode, returns a single-byte result to c, and the overflow of the result is ignored. It is safest to change the expression "c=a*b;" in the above example to "c=a; c=c*b;" (the same treatment is applied to addition).
3.3 Understanding the register usage of multiplication and division functions
Since the RAM in the PIC microcontroller is only a few dozen bytes, the space is very valuable, and the Mplab-C compiler has the property of not releasing the RAM address, that is, the address used by a variable cannot be allocated to other variables. If the RAM space cannot meet the requirements of too many variables, some variables can only be forced by the user to allocate the same RAM space for alternating use. The multiplication and division functions in Mplab-C need to borrow RAM space to store intermediate results, so if the RAM occupied by the multiplication and division functions overlaps with the address of the user variable, it will lead to unpredictable results. If multiplication and division operations are used in a C program, it is best to first check whether the addresses occupied by the multiplication and division functions conflict with the addresses of other variables through the disassembly code of the program machine code (included in the generated LST file) to avoid the program running away. The Mplab-C manual does not give the specific RAM address occupied by its multiplication and division functions. Example 5 is the address occupation of 0×13, 0×14, 0×19, and 0×1A by the multiplication function.
Example 5
Partial disassembly 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 the low byte of the result | |||
01B1 | 0804 | MOVF 04,W | |
01B2 | 00A0 | MOVWF 20 | |
; Return the high byte of the result |
4 Reprogramming the PIC microcontroller chip
For users without hardware emulators, EPROM chips are always used to debug programs. Every time a program is changed, the original content is erased first and then programmed. This process wastes a lot of time and shortens the service life of the chip. If the result of the latter programming is only that the same bit of the corresponding machine code byte changes from "1" to "0" compared to the previous one, data can be written again on the previously programmed chip without erasing the original chip content. In
the process of debugging the program, constant adjustments are often encountered. If the change of constants can ensure that the corresponding bit changes from "1" to "0", programming can continue on the basis of the original chip content. In addition, since the machine code corresponding to the instruction "NOP" is "00", the deletion of instructions during the debugging process is replaced by the "NOP" instruction first, and programming can continue on the original chip content after compilation. In addition,
when programming chips with EPROM, pay special attention to the program confidentiality status bit. The manufacturer has changed the confidentiality status bit of the new generation of EPROM chips from the original EPROM erasable type to the fuse type. Once the program code confidentiality fuse is programmed to "0", the reprogrammable EPROM chip cannot be programmed again. Please pay attention to this when using it to avoid unnecessary waste (Microchip information does not explain this).
References
1 Micorchip PIC16Cxx Data Book
2 MPLAB-C USER
’
S GUIDE
Previous article:Flexible use of PROGRAM MEMORY of PIC16C57 microcontroller
Next article:PIC microcontroller bank and PC error problem
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【TI mmWave Radar Evaluation】SDK and Example Download
- Electromagnetic wave characteristics
- [Free] Raspberry Pi 400 | Faster CPU, better heat dissipation
- Tips for getting STM32 code running time
- Release an operating system source code package that runs on NXP LPC1768.
- EEWORLD University Hall----Live Replay: SimpleLink? Latest Software and Hardware Solutions for Wireless Platform
- Thank you-Brothers of EE Water Army
- nF capacitor and 1M resistor in parallel between the metal case and GND
- Do you know about Allegro's latest solution for vehicle electrification and emission control 48V system?
- 【CH579M-R1】+ Bluetooth function key preliminary test