Event's some pointers on how to use them

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Event's some pointers on how to use them

Post by be80be » Fri Jan 11, 2013 5:37 am

I looking for some pointers on event's a interrupt's

Here some code I started but i don't think it's handling interrupt's right

Code: Select all

Device = 18f2520
Clock = 8
Config osc = INTIO67
Include "utils.bas"
Include "HiLowVolts" 
Event OnVoltChange()
    If PIR2.bits(2) = 1 Then                          
            PORTC.7 = 1
            PIR2.bits(2) = 0          
        Else
            PORTC.7 = 0
    End If
End Event
Dim LedRed As PORTC.7
Dim LedGreen As PORTC.6
OSCCON=$72
SetAllDigital
 
Output(LedGreen)
Output(LedRed)
 
LedRed = 0
LedGreen = 0

HLVDCON =%00011011     
While true
    HiLowVolts.Initialize (OnVoltChange)
    Toggle (LedGreen)
    DelayMS(1000)
Wend 
And the module i made.

Code: Select all

Module HiLowVolts

// event handler type...
Type TEvent = Event()

// local and public variables...
Dim LowVolts As TEvent

// ISR routine...
Interrupt OnVoltChange()
    LowVolts()
End Interrupt

// initialize...
Public Sub Initialize(pOnvoltEvent As TEvent)
   LowVolts = pOnVoltEvent
   PIR2.bits(2) = 0        //clear HLVD flag
   PIE2.bits(2) = 1       //enables HLVD interupt 
   intcon.7 = 1     //enables HLVD interupt 
   Enable(OnVoltChange)
End Sub
	       

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Jan 11, 2013 11:47 am

Here's a slight change just to clarify what you're doing (it doesn't really change much of anything)

Code: Select all

Dim LedRed As PORTC.7
Dim LedGreen As PORTC.6
Dim HLVDIN as PORTA.5       // HLVDIN (RA5_AN4) is an analog input

Event OnVoltChange()
    If PIR2.bits(2) = 1 Then
        LedRed = 1          // was PORTC.7 = 1
        PIR2.bits(2) = 0    // this will only clear the IF bit when pin V > ref
    Else                    // this condition will never happen
        LedRed = 0          // was PORTC.7 = 0
    End If
End Event

OSCCON=$72
SetAllDigital()

Output(LedGreen)
Output(LedRed)
input(HLVDIN)
The HLVDIN (RA5_AN4) is an analog input pin, so just make sure it's an input. Even though you're calling SetAllDigital(), I don't think this will prevent HLVD from working since according to the datasheet
Pins configured as digital inputs will convert as analog inputs. Analog levels on a digitally configured input will be accurately converted.

Analog levels on any pin defined as a digital input may cause the digital input buffer to consume current out of the device’s specification limits.

but I don't have an 18F2520 to try it on, and I've long since quit assuming that what works on one chip will work on another.

One thing about the event/intr and HLVDIN - you've set up the HLVD to detect when RA5 drops below ~ 3.9V.
Once HLVDIN < 3.9V, you won't be able to clear HLVDIF (PIR2.2) until HLVDIN rises back above the trip point, so you'll keep getting an interrupt over and over until that happens. You should never get one once it rises back above 3.9V

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Fri Jan 11, 2013 11:58 am

The interrupt happens as the voltages drops below 3.9 volts but it never changes back the red led it stays on when the interrupt is not happening.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Jan 11, 2013 12:05 pm

but it never changes back the red led it stays on
That's what I just pointed out... the HLVD module gives you an interrupt in one direction only depending on the VDIRMAG bit setting.

If you want to detect both edges you'd have to change the setup inside the ISR, but that can be tricky to get it to work right 100% of the time.

Check out Figure 22-2 in the 2520 datasheet for how it works.

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Post by Jon Chandler » Fri Jan 11, 2013 1:28 pm

Jerry Messina wrote: The HLVDIN (RA5_AN4) is an analog input pin, so just make sure it's an input.
The HLVDIN pin is actually only used only if an external reference level is desired instead of the internal band-gap reference and 16 step voltage divider. The HVLD module is hard-wired to monitor Vdd.
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Jan 11, 2013 1:59 pm

The HVLD module is hard-wired to monitor Vdd
Duh... you're right! Thanks for pointing that out, Jon.

Just so happens I was writing some code this morning for using HLVDIN as an external input monitor when Burt posted this. What a coincidence.

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Fri Jan 11, 2013 1:59 pm

Jerry could you maybe show a example of how thats done i never used interrupt's just polling
change the setup inside the ISR
Thanks Burt

The HLVDIN pin lets you fine tune it Jon did a polling setup it work fine can't wait to see Mrdeb hack it up lol

The 16-tap resistor ladder network is not accurate mine is off by .3 volts

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Jan 11, 2013 2:12 pm

Thinking about it some more, this could be tricky with HLVD since you can't clear the intr flag until the voltage goes back over the threshold.

Normally, with things like edge-triggered interrupts or comparators, you can flip a control register bit inside the ISR to change which polarity you're looking for. That probably won't work too well here.

If I get a chance later I'll try it and see what happens.

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Fri Jan 11, 2013 2:23 pm

I'm increasing the voltage back to 5 volts but without a reset you can't trip it agin.

That's kind of what i'm seeing
Thinking about it some more, this could be tricky with HLVD since you can't clear the intr flag until the voltage goes back over the threshold.
Last edited by be80be on Mon Jan 14, 2013 11:19 am, edited 1 time in total.

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Post by RangerBob » Mon Jan 14, 2013 10:36 am

Not sure if this is what you're after Burt, but Iposted an example of using the HLVD before here.

I've used this setup in production for detecting DC adapter removal with battery switchover reliably for years now. I switch the detect edges within the interrupt, and have seen no real issues. The main idea is to debounce the signal thoughly as you'll find a lot of messiness on the edges.

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Mon Jan 14, 2013 11:20 am

Thanks RangerBob

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

HLVD interrupt one direction

Post by backpacker69 » Sun Feb 03, 2013 6:40 pm

I am curious if perhaps changing the VDIRMAG to set trip point higher if it would reset the interrupt so then its a two way interrupt.
Been conversing w/ Burt about this HLVD as I presently am getting stuck going into the interrupt but getting back out.
I miss backpacking the Sierras

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Interrupt issue solved

Post by backpacker69 » Mon Feb 04, 2013 5:27 pm

I solved my "locking up" issue by disabling bit 4 in the HLVECON register(HIGH/LOW VOLTAGE DETECT POWER ENABLE BIT) in my battery test sub routine.
Am now able to increase/decrease/increase battery voltage, test at each step without issues of "locking Up"
Would like to continuously monitor battery but havn't got that far yet. Need to clean up etc but this routine appears to work for my application.
Using the voltage setting in MPLAB to adjust voltage vers trip point. No need for fancy variable power supply.

Code: Select all

   ' xxxxxxxxxxxxxxxxxxxxxxxxxx
        Inline Sub  BATTERY_TEST()
        'xxxxxxxxxxxxxxxxxxxxxxxxxxx
        ' BAD BATTERY
        HLVDCON = %00011000    // 3.12 volts   HLVD is enabled
        If PIR2.bits(2) = 1 Then       // interrupt flag bit
         
         
          // check hlvd flag
             // rbif = 0                 
             // rbie = 0  //interrupt enable bit
             // TX_pwr = 1    //turn on transmitter
            led0 = 1     //blue led next to green BI color in final plan
                 DelayMS(1000)
            Toggle(led0)
            
            PIR2.bits(2)=0  // clear interrupt flag
            PIE2.bits(2) = 0                    // allow interrupt temp
      //  HLVDCON = %00001000    // 3.12 volts   HLVD is enabled
         Else
         
         
         ' GOOD BATTERY
              PIR2.bits(2) = 0
              // TX_pwr = 1    //turn on transmitter
               GD_bat = 1       // green led next to blue GD_bat = good battery
                      DelayMS(1000)
               Toggle( GD_bat)
                      DelayMS(1000)
               TX_pwr = 0                       //turn off transmitter
            EndIf  
        //PIE2.bits(2) = 0                    // allow interrupt temp
        //HLVDCON.bits(5) = 0                // set the IRVST bit for interrupts    
        HLVDCON = %00001000    // 3.12 volts   
        End Sub

    
I miss backpacking the Sierras

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Post by Jon Chandler » Tue Feb 05, 2013 9:39 am

Using the HLVD Module with Swordfish Basic

An expanded explanation of my original test code posted at Electrotech. This is tested and working on actual hardware.
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Post Reply