The following PIC® microcontrollers contain a High/Low-Voltage Detect (HLVD) Module: 24F Family, 18F Family.
The HLVD is a valuable feature that can be used to create a low voltage monitor that can warn the user when the power supply is not providing the minimum required operating voltage or when the battery supply is in need of replacement or re-charging. I will describe in this article the code required to do this and how to structure it in your program for proper operation. I will also attach a schematic of an application that uses this code and a description of that product application which I created that makes use of this HLVD voltage monitor to detect a low battery condition. To magnify these two attachments for easier reading, single click your mouse on the image after it opens.
The specific process and associated code I will list below has been tailored for use with an 18F4550 (or 2455, 2550, 4455) microcontroller. However by reviewing the relevant HLVD section of the data sheet for your microcontroller of choice (that must have an HLVD module), you should be able to easily modify this code for your particular PIC microcontroller. With some advice from Darrel Taylor, I was able to develop/debug this code, which has been tested, and I can verify it works!
First, you need to decide whether to use an externally generated trip point or to use the internally generated trip point capability. If you decide to use the externally generated trip point you must make sure that the HLVDIN pin of your microcontroller (pin 7 of the PDIP version of the 18F4550) is connected to the Vdd power supply via a resistor network as shown in Figure 24-1 of the 18F4550 data sheet (or per the similar figure in the data sheet for your specific microcontroller type). I prefer to use the programmable, internally generated trip point, which provides relatively good resolution for the trip point over a voltage range from 2.17 to 4.59 volts by use of an internal resistor array rather than any external resistors. The code in this example is based on how to use the programmable internal trip point set to monitor when the loaded battery supply reaches 4.33 volts. Refer to the HLVD section of the data sheet for for your particular microcontroller for more details.
Next, you need to setup one of Darrel Taylor's instant interrupts for the HLVD module using either the DT_INTS-14 or DT_INTS-18 version, depending on your type of microcontroller. For a download of the required files and more detail on how to use the DT_INTS instant interrupts for either PIC 14F/16F or 18F microcontrollers, go to either http://darreltaylor.com/DT_INTS-14/intro.html or http://darreltaylor.com/DT_INTS-18/home.html, respectively.
Once you have downloaded your instant interrupt files and placed them in the correct directory locations on your host machine as instructed at the above URLs, you must then set up your instant interrupt in your code similar to variables used in the assembler code listed below:
ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? ;INT_Handler USB_Handler INT_Handler HLVD_INT, _LowVolt, PBP, yes endm INT_CREATE ; Creates the interrupt processor ENDASM
Then you need to insert this code in your main program loop to setup the use of the HLVD module.
' Set registers for using HLVD feature per section 24.2 of the 18F4550 data sheet ' Step 1-Disable the module by clearing HLVDCON.4 HLVDCON.4 = 0 ' Implement Step 1 ' Step2 HLVD3:HLVDL0=1101 'Set the trip point at Vdd=4.33 vdc ' Step3 HLVDCON.7=0 'Set VDIRMAG=0 to detect low voltage transition ' Step4 HLVDCON.4=1 'Enable the HLVD module HLVDCON = %00011101 'Implements Steps 2-4 ' Clear the HLVDIF interrupt flag (PIR2<2>) PIR2.2 = 0
LowVolt: ' Blink LED_RED 5X to indicate Low Battery voltage detected For i = 0 to 4 HIGH LED_RED Pause 500 LOW LED_RED PAUSE 500 Next @ INT_DISABLE HLVD_INT ; This statement very important, else code will lockup on exit ; from the ISR to main program. ' Resume Main Program @ INT_RETURN '------------------{ End of Interrupt Handlers }-------------------------
@ INT_ENABLE HLVD_INT ; enable HLVD interrupt
This article was originally published on May 24, 2010 16:21 by jellis00 in the Code Examples forum under the thread: "How to use the HLVD module as a low voltage monitor".
Re: How can I reduce ADC drift
If the ADC result has no or minimal effect because of temp then we have to look elsewhere for the drift.
Ioannis Today, 19:28Ioannis