This article provides a summary of some practical experience and technical insights gained during the development of PIC microcontroller projects, aimed at helping others with similar tasks.
1. How to Further Reduce Power Consumption
Power consumption is a crucial factor in battery-powered devices. The PIC16C×× series microcontrollers are known for their low power usage—operating at less than 2mA when running at 4MHz under 5V. To further reduce power consumption while maintaining functionality, one effective method is to lower the operating frequency. For example, reducing the frequency to 32kHz can bring the current down to as low as 15μA. However, this may slow down certain operations like mathematical computations. In such cases, using an RC oscillator allows for temporary frequency increases during critical calculations. This can be done by connecting a resistor (R1) between an I/O pin (like RB1) and the OSC1 pin. When the system is idle, RB1 is set to 0, and when faster computation is needed, RB1 is set to 1. This causes the capacitor to charge quickly, increasing the frequency and reducing processing time. After the operation, RB1 is set back to 0 to return to low-power mode. It's important to choose R1 carefully, as too small a resistance may prevent oscillation. A value above 5kΩ is generally recommended.
In addition, utilizing the "sleep" instruction can significantly cut power usage. When the MCU enters sleep mode, it consumes only a few microamps. This is especially useful for waiting for events or implementing delays. For instance, you can use the sleep command in a delay loop to conserve energy. Moreover, changes on Port B can wake the device from sleep, allowing early termination of the delay. Be sure to clear the RBIF flag before enabling interrupts to avoid unexpected behavior.
2. Understanding the RBIF Bit in INTCON
The RBIF bit in the INTCON register indicates an interrupt caused by a change on Port B. If the port is configured as input, the pins are sampled during each read cycle. If the new value differs from the previous one, RBIF is set to 1. Before enabling the RB interrupt, make sure to clear RBIF first to avoid false triggers. After handling the interrupt, it’s also good practice to reset the flag to prevent repeated triggering.
3. Programming Tips for PIC Microcontrollers in MPLAB-C
When writing assembly instructions within C code in MPLAB-C, ensure that each assembly block starts on a new line. For example, using #asm followed by #endasm must be properly formatted. Otherwise, the compiler will throw errors. Additionally, when performing arithmetic operations like multiplication, it's safer to break them into steps to avoid overflow issues. For instance, instead of directly assigning c = a * b, consider breaking it into c = a; c *= b; to ensure correct results, especially when dealing with larger data types.
4. RAM Usage and Multiplication/Division Functions
The PIC microcontroller has limited RAM, and the MPLAB-C compiler does not release memory addresses once assigned. Therefore, variables may overlap if not managed carefully. The built-in multiplication and division functions require temporary storage in RAM, which could interfere with user variables. To avoid unexpected results, it's advisable to review the disassembled code and check for address conflicts. Example 5 shows how specific RAM locations are used during multiplication, so careful planning is essential.
5. Programming without an Emulator
For developers without a hardware emulator, EPROM chips are often used for debugging. Each program update typically requires erasing the chip, which is time-consuming and reduces the chip's lifespan. However, if a bit changes from '1' to '0', the chip can be reprogrammed without erasing. During debugging, using NOP instructions can help manage code changes more efficiently. Also, pay attention to the security fuse settings on newer EPROM chips, as they may prevent reprogramming once set to '0'. Always verify these settings to avoid unnecessary waste.
Our Wind Power Lighting Pole, Solar Street Lights Pole are made from quality sheet through bending, forming, automatic welding and hot galvanization. We can reach one-run machining length of 14m and can bend sheet of thickness up to 45mm. We adopt advanced welding procedures, automatically weld main joints and reach rank-II welding quality.
We have supplied our Wind Power Lighting Pole,Solar Street Lights Pole to Australia, Columbia, England, Kuwait, Iraq, Philippines, Pakistan and etc.
Wind Power Pole, Solar Lighting Pole, Solar Street Lights Pole, Wind and Solar Lighting Poles
JIANGSU XINJINLEI STEEL INDUSTRY CO.,LTD , https://www.steel-pole.com