How to use the HLVD module as a low voltage monitor


+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378

    Default How to use the HLVD module as a low voltage monitor

    In hopes this might help someone avoid the time it took me to learn this I will make this contribution of code as a solution to using the HLVD module as a low Vdd voltage monitor. I was able to resolve this with some offline support from Darrel Taylor. This process has been tested and it works!

    First, you need to make sure you have setup a DT_INTS-18 interrupt for the HLVD module by inclusion of this code ahead of your main program:

    Code:
    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.

    Code:
          
     ' 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
    Then you need to create an interrupt service routine (ISR) near the end of your code as follows:

    Code:
    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 }-------------------------
    Then you need to insert this assembler statement in your code wherever you want to test for a low Vdd voltage condition. I insert this in my main loop so that regardless of what other events are occuring, whenever the above ISR completes, the mainloop sets up the HLVD module for another low voltage interrupt.

    Code:
     
    @    INT_ENABLE   HLVD_INT               ; enable HLVD interrupt
    I hope this might help someone!

  2. #2

    Default Re: How to use the HLVD module as a low voltage monitor

    I cannot get this example code to compile - syntax error after step 2 . I tried several different versions of the statement, but no luck.

    This will be expanded to a much larger program, and used with a 18f2520 once I get this simple part to compile.
    I am using PBP 2.60



    Code:
          DEFINE OSC 4
          OSCCON = %11101110 ' USE THIS, AND CHANGE CONFIGS FOR INT OSCILLATOR
    @ __CONFIG    _CONFIG1H, _OSC_INTIO67_1H  & _FCMEN_OFF_1H & _IESO_OFF_1H
    
    TRISC = %00000000
       
      i var byte
    
         ASM
    INT_LIST  macro    ; IntSource,         Label,  Type, ResetFlag?
            
            INT_Handler   HLVD_INT,        _LowVolt, PBP,  yes
        endm
        INT_CREATE                ; Creates the interrupt processor
    ENDASM
    
    MAIN:
    
              ' 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
    
     PAUSE  1000
        GOTO    MAIN
    
    
    
    LowVolt:
       ' Blink LED_RED 5X to indicate Low Battery voltage detected
            For i = 0 to 4
               HIGH PORTC.0
               Pause 500
               LOW PORTC.0
               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

  3. #3

    Default Re: How to use the HLVD module as a low voltage monitor

    Sorry - I guess I "Jumped the Gun" so to speak. I commented out step 2, and it works fine. By the way, my listing does not show DT's interrupt includes.
    Once again, sorry for not seeing the obvious.

    Ken

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts