The PIC series single-chip microcomputers produced by the American Microchip company have been widely used in industrial control, instruments, meters, communications, home appliances, toys and other fields due to their ultra-small, low power consumption, low cost, and multiple varieties. This article summarizes the author Some experience and skills in the development of PIC microcontrollers are for reference by colleagues.
1 How to further reduce power consumption
Power consumption is an important consideration in battery-powered instrumentation. The power consumption of the PIC16C×× series of single-chip microcomputers is relatively low (the operating current is less than 2mA when the oscillation frequency is 5V and 4MHz). In order to further reduce the power consumption, under the premise of ensuring that the working requirements are met, the method of reducing the working frequency can be adopted. The reduction of the working frequency can greatly reduce the power consumption (such as PIC16C×× working under 3V, 32kHz, the current can be reduced to 15μA), but the lower operating frequency may cause some subroutines (such as mathematical calculations) to take more time. In this case, when the oscillation mode of the one-chip computer adopts the form of RC circuit, it can be solved by increasing the working frequency in the middle.
The specific method is to connect a resistor (R1) across an idle I/O pin (such as RB1) and OSC1 pin, as shown in Figure 1. Set RB1=0 in the low-speed state. Set RB1=1 when fast calculation is needed. Because the capacitor voltage rises quickly when charging, the operating frequency increases, and the calculation time is reduced. After the calculation, set RB1=0 again, and enter a low-speed, low-power state. The amount of change in the operating frequency depends on the resistance of R1 (note that R1 cannot be selected too small to prevent the oscillation circuit from not vibrating, generally greater than 5kΩ).
In addition, to further reduce power consumption, you can make full use of the "sleep" instruction. Execute the "sleep" instruction, the machine is in sleep state, and the power consumption is a few microamps. The program can not only use the "sleep" instruction to wait for an event in the standby state, but also can be used in a 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 state is off, the change of Port B port level can wake up "sleep" and end the delay program early. This is particularly useful in some applications. At the same time, pay attention to the relationship with WDT and interrupt when using "sleep".
Example 1 (written in Mplab-C) Example 2 (written in Masm)
Delay() Delay
{;This line can add switch interrupt instruction
/*This line can add a switch interrupt instruction*/ movlw.10
for (i=0; i《=10; i) movwf Counter
SLEEP(); Loop1
} Sleep
decfsz Counter
goto Loop1
return
2 Pay attention to the RBIF bit in INTCON
Each interrupt enable bit in INTCON has no effect on the interrupt status bit. When PORT B ​​is configured as input mode, the RB "7:4" pin input is sampled in each read operation cycle and compared with the old latched value. Once it is different, a high level is generated, and RBIF=1 is set. Before opening the RB interrupt, perhaps RBIF has been set to "1", so the RBIF bit should be cleared before the RB interrupt is turned on to avoid the influence of the original value of RBIF. At the same time, it is better to clear the RBIF bit after the interrupt processing is completed.
3 Problems to be paid attention to when writing PIC microcontroller programs with Mplab-C high-level language
3.1 Pay attention to the writing format when embedding assembly instructions in the program, see Example 3.
Example 3
…………
while(1) {#asmwhile(1) {
…… #asm /*Should start a new line*/
#endasm ……
}/*Cannot compile correctly*/ #endasm
…… }/*Compile passed*/
...
When the assembly instructions are embedded, each instruction from "#asm" to "endasm" must be on its own line, otherwise an error will occur during compilation.
3.2 The safest way to express addition and multiplication is shown in Example 4.
Example 4
#include《16c71.h》
#include《math.h》
unsigned int a, b;
unsigned long c;
void main()
{a=200;
b=2;
c=a*b;
} /*Can't get the correct result c=400*/
The reason is that Mplab-C compiles c=a*b by 8×8 multiplication, and returns a single-byte result to c, and the overflow of the result is ignored. Change the "c=a*b;" expression in the above example to "c=a; c=c*b;", which is the safest (same as above for addition).
3.3 Understand the occupation of registers by multiplication and division functions
Because the RAM on the PIC chip only has dozens of bytes, the space is extremely precious, and the Mplab-C compiler has non-releasable RAM addresses, that is, the address used by one variable can no longer be allocated to other variables. If the RAM space cannot meet the requirements of too many variables, some variables can only be forced to allocate the same RAM space alternately by the user. 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 overlap with the address of the user variable, unpredictable results will occur. If the multiplication and division operations are used in the C program, it is best to check whether the address occupied by the multiplication and division conflicts with the addresses of other variables through the disassembly code of the program machine code (included in the generated LST file) to prevent the program from running away. The Mplab-C manual does not give the occupation of specific RAM addresses by its multiplication and division functions. Example 5 is the occupation of 0×13, 0×14, 0×19, 0×1A address by the multiplication function.
Part of the disassembled code
#include 《pic16c71》01A7081FMOVF 1F, W
#include《math.h》01A80093MOVWF 13
;borrow
unsigned long Value @0x101A90820MOVF 20, W
char Xm @0x2d;01AA0094MOVWF 14
;borrow
void main() 01AB082DMOVF 2D, W
{Value=20;01AC0099MOVWF 19
;borrow
Xm=40;01AD 019ACLRF1A
;borrow
Value=Value*Xm01AE235FCALL 035Fh
; Call the multiplication function
……01AF1283BCF 03, 5
}01B0009FMOVWF 1F
; Return low byte of result
01B10804MOVF 04, W
01B200A0MOVWF 20
; Return the high byte of the result
4 Repeat programming of the chip
For users who don't have a hardware emulator, always choose the chip with EPROM to debug the program. Every time the program is changed, the original content is erased and then programmed. The process wastes a lot of time and shortens the service life of the chip. If the result of the next programming is higher than that of the previous one, only the same bit of the corresponding machine code byte is changed from "1" to "0", then data can be written on the previous programming chip without erasing the original chip. content.
In the process of program debugging, constant adjustments are often encountered. For example, the constant change can ensure that the corresponding bit changes from "1" to "0", and programming can be continued on the basis of the original content. In addition, since the machine code corresponding to the instruction "NOP" is "00", the deletion of the instruction during the debugging process should be replaced by the "NOP" instruction first, and the programming can also be continued on the original film content after compilation.
In addition, when programming the chip with EPROM, pay special attention to the program security status bit. The manufacturer has changed the security status bit of the new generation of EPROM chips from the original EPROM erasable type to the fuse type. Once the program code security fuse is programmed to "0", the reprogrammable EPROM chip cannot be reprogrammed. Pay attention to this when using it to avoid unnecessary waste.
Product advantages:
1. Stylish, portable, practical and convenient home charger
2. 6-port USB emergency charger, which can be used safely for tablet computers and can charge 6 devices at the same time
3. Built-in IC chip and fuse, protection: over current, over voltage, short circuit protection
4. The latest release, new design, beautiful appearance, high efficiency and energy saving, the best alternative to the original charger
5. Chargers for MP3, MP4, GPS, PDA, e-books, mobile phones, digital cameras, tablets, etc.
Features
(1) Overcharge protection
(2) Over discharge protection
(3) Short circuit protection
(4) Voltage stabilization protection.
(5) Automatic identification of mobile phone current protection.
1. Connect up to 6 USB devices at a time, and each device will receive a unique and tailor-made charge. The maximum output power of QC3.0 is 18W, which can optimize the charging of all devices.
2.2QC3.0+4 ports total 6 charging ports, which can charge 6 devices with a total power of 84W at the same time
3. With multiple protection safety systems, the product will automatically adjust the charging current according to the needs of the device being charged. Automatically detect and adjust the charging current to charge your device at the fastest speed.
One machine for multiple uses
The USB charger can charge 6 devices at the same time, and the smart recognition technology will detect and provide the best charging level for each of your devices.
The maximum output current of a single USB port is 2.4A. 2QC3.0 has 6 charging ports, which can charge 6 devices at the same time without causing shunting or reducing power. The USB charger can quickly charge your devices. Smartphones and tablets can receive up to 2.4 amps of current, so you can use the device as you like while charging.
Over-current, over-charge, and short-circuit protection will make charging safe, without any risk of overheating or damaging the device. Passed CE/FCC/ROSH/PSE/CCC certification. Through safety certification to ensure safe use.
Usb Charger,Usb C Charger,Usb C Power Adapter,Portable Usb Charger
Shenzhen Jinziming Electronic Technology Co.,LTD , https://www.powerchargerusb.com