Low Voltage Detect Code

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Low Voltage Detect Code

Post by Jason » Sun Oct 23, 2011 12:13 pm

Hi All,
I was wondering if anyone has done any work with the LVD module in the 18F pic's.

I've been reading the 18F425 datasheet, and unfortunately ISIS doesn't model the LVD module in the PIC, so I'm about to start some testing and just wondered if anyone could give me a start with some sample code.

Thanks heaps.

Jason

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

Post by RangerBob » Mon Oct 24, 2011 9:53 am

Hello Jason,

I use it a lot for controlling external battery switching, heres some excepts that may be of use to you.

The CtlEXt and CtlBatt are the control lines to the battery switches. The interrupt fires to switch the system from ext to battery, the switch back (not shown here) actually occurs on a slow timer to add some hysteresis to the system. It watches the extpresent variable and uses the ExtDebounce to keep track. In my system the debounce is essential as the connect / reconnect of power is a messy business!

Hope this makes sense!

Interrupt:

Code: Select all

// HLVD Stuff
Public Const 
	DirUp 		= 1,			'HLVD directions
	DirDown		= 0

Public Dim
	ExtDebounce As Word,
	ExtPresent As Boolean

Code: Select all

// In interrupt 

	If HLVDIF = 1 Then
		// Determine current state
		If HLVDDir = DirDown Then			'Switch to battery		
			CtlBatt = 1
			CtlExt 	= 0
			HLVDDir = DirUp					'Watch for Rising Ext
			ExtPresent = false
			ExtDebounce = 25					'was 500
			HLVDIE = 0						'Turn off Rising edge interrupt for a while
											'Reenabled by timer at extdebounce = 1
		Else 									'Ext is Present
			ExtPresent = true                   'Set timed switch
			HLVDDir = DirDown					'Watch for Falling Ext
			ExtDebounce = 50     				'was 255
		EndIf
		HLVDIF = 0                          'Clear Flag
	EndIf
This is the initialisation;

Code: Select all

Public Dim
	HLVDOn 			As HLVDCON.4,
	HLVDDir 		As HLVDCON.7,
	HLVDStable 		As HLVDCON.5,
	HLVDIF 			As PIR2.2,
	HLVDIE 			As PIE2.2,
	HLVDIP 			As IPR2.2

Code: Select all

//HLVD Setup
 	Low(CtlExt)
  	Low(CtlBatt)
   
	// Set for External input
	HLVDCON.3 = 1
	HLVDCON.2 = 1
	HLVDCON.1 = 1
	HLVDCON.0 = 1

	HLVDIP = 0				'Low Priority
	HLVDDir = DirDown		'Check for falling Ext supply initally
	
	HLVDOn = 1

	Repeat 
		DelayMS(100)
		Inc(n)
	Until HLVDStable = 1 Or n = 50
 	
	HLVDIF = 0
	
    // Enable HLVD Interrupt
	HLVDIE = 1	

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Mon Oct 24, 2011 12:32 pm

Thank you so much Ranger bob!!

You and Jerry Messina are so awesome on this forum.

Ok, I'm off to do some coding!!
I'm making a battery management module for some lithium phosphate polymer batteries (LiFePO4). It's a battery pack I've made up for a sound level meter, and I need the LVD module in the pic to cut off supply when the voltage gets too low. That kind of thing.

Thanks again for sharing your code.

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Tue Oct 25, 2011 4:01 am

Hey Ranger Bob,
What processor were you using?

Cheers

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

Post by RangerBob » Tue Oct 25, 2011 8:50 am

I've been using 18F4550's and 18F8722's with this code. You'll probably have to trawl the datasheet to ensure thats the correct registers. Some are a bit different.

Regards

Post Reply