looking at the HLVD block diagram on page 244 of the 18F2420 data sheet, the resistor ladder is connected to Vdd so using this code the LED1 should come on when the voltage drops below 2.79-3.04. No other connections for external voltage measurement as the block diagram is for external trip point voltage.
I feel I don't have the registers set right so I need some help if possible?
Thinking of removing the 330 ohm resistor I have tied to Vdd seeing how when the voltage drops below 2.89 typical voltage I don't have enough to power the LED anyway but need some expert advice on the code.
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 1/7/2013 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Device = 18f2420
Clock = 8
//Config OSC=HS
Include "InternalOscillator.bas"
Include "utils.bas"
Include "convert.bas"
Dim led1 As PORTC.5
HLVDCON=%00110110 // 2.74-3.04 volts trip
HLVDCON.bits(5)=1 // voltage detect flag
HLVDCON.bits(4)=1 // voltage detect enable bit
HLVDCON.bits(3)=0 // bits 3-0 = range
HLVDCON.bits(2)=1
HLVDCON.bits(1)=1
HLVDCON.bits(0)=0 // set for 2.74 - 3.04
PIE2.bits(2)=0 //HLVDIE
INTCON.bits(7)=1 //GIE/GIEH bit
INTCON.bits(6)=1 //PEIE/GIEL BIT
output(led1)
WHILE TRUE
If HLVDCON.bits(5)=0 // voltage below 3.04 - 2.79
then led1 = 0 // low is when voltage is LOW
else led1 =1 // voltage detect flag
HLVDCON.bits(5)=1 // voltage detect flag
endif
wend
I don't believe it's in my best interest to continue helping you, but since nobody else is....
Code:
HLVDCON=%00110110 // 2.74-3.04 volts trip
HLVDCON.bits(5)=1 // voltage detect flag
HLVDCON.bits(4)=1 // voltage detect enable bit
HLVDCON.bits(3)=0 // bits 3-0 = range
HLVDCON.bits(2)=1
HLVDCON.bits(1)=1
HLVDCON.bits(0)=0 // set for 2.74 - 3.04
The first question you need to answer is why do you have/need the last 6 lines when you have the first? This is a really basic point which you SHOULD understand by now.
Without wasting any MORE of my time, it appears to me that the sense of HLVDCON.bits(5) is backwards.
Geez on grief, haven't you figured out how to write an IF/THEN statement yet? The compiler may understand your wacko format, but nobody can.
Code:
If HLVDCON.bits(5)=0 // voltage below 3.04 - 2.79
then led1 = 0 // low is when voltage is LOW
else led1 =1 // voltage detect flag
HLVDCON.bits(5)=1 // voltage detect flag
endif
LowVolt:
' Blink LED_RED 5X to indicate Low Battery voltage detected
For i = 0 to 4
HIGH( LED_RED)
delayms (500)
LOW (LED_RED)
delayms (500)
Next
asm 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 asm
'------------------{ End of Interrupt Handlers }-------------------------
I wasn't sure if I can use the HLVDCON=%00110110 so I failed to remove and do each bit by itself. I guess I was right in thinking I could use but didn't follow my instinct.
I though I had the if/then right but I guess not.
Am I correct in thinking that I do not need a connection to RA.5 if using the Vdd and the issue of the LED voltage will look at as well.
LOTS of little issues so on to repairing them.
THANKS
If x > y then // if/then all on the same line
LED1 = 0
Somevalue = somevalue + 1
Else
LED1 = 1
Somevalue = 0
End If
The if/then all goes on the same line.
The indenting shows the hierarchy.
The else and end if each go on a line.
Yes, the way you did it works but it's not the normal presentation and it makes it difficult to understand. As one who is depending on the kindness of strangers for help, you really should do everything possible to make it easy for them.
Look at the schematic for the HLVD. The HLVDIN connection is used only for setting an external trip point.
I have seen the if/then done both ways, just didn't know if one was better or not.
I have been doing some code cleaning and got really inspired with the scroll wheel artical. Very easy read of the code so I have started trying to do same.
here is the present TX code that I wrote most if not all by myself studying examples and putting bits and pieces together. Surprising it worked the first time.
As you can see it still needs polishing up, add the battery monitor code, an interrupt to put the pic to sleep and a method to create an audible sound to user (a clicking as in a car). Been considering a small piezo buzzer or an earpiece but using an earpiece the user might want to listen to their MP# or ? (bad idea to use while riding a bike anyway. But if you look over this code I hope you find it easy to decipher. IMO I am getting better at this code stuff but having never done much with interrupts and control registers but still learning.
As for batteries, looking at using nicad. Very flat discharge rate.
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 12/17/2012 *
* Version : 1.0 *
* Notes : need to add HLVD code with blinking led. set for 3v *
* : *
*****************************************************************************
}
Device = 18f2420
Clock = 8
//Config OSC=HS
Include "InternalOscillator.bas"
Include "utils.bas"
Include "convert.bas"
//xxxxxxxxxxxxxxxxx
' TURN PORTS
'xxxxxxxxxxxxxxxxx
Dim Data_R As PORTC.0
Dim DATA_L As PORTC.1
'xxxxxxxxxxxxxxxxxx
'transmit & transmit power
'xxxxxxxxxxxxxxxxxx
Dim TE As PORTC.2
Dim TX_pwr As PORTC.3
Dim Bat_mon As PORTC.5
'xxxxxxxxxxxxxxxx
'switches
'xxxxxxxxxxxxxxxxxxx
Dim s1 As PORTB.5
Dim s2 As PORTB.6
'xxxxxxxxxxxxxxxxxxxxxxx
' SUB ROUTINES
'---------------------
// LEFT TURN sub
'xxxxxxxxxxxxxxxxxxxx
Sub LEFT()
TX_pwr = 1 //Transmitter power ON
TE = 0 //transmit enable
DATA_L = 0 //data stream 1011
DelayMS(2000) //to track progress, make 100?
DATA_L=1
TE=1
TX_pwr = 0 // transmitter powered OFF
End Sub
'xxxxxxxxxxxxxxxxxxxxxxxxxx
// RIGHT TURN sub
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Sub RIGHT()
TX_pwr = 1 //Transmitter power ON
TE = 0 //transmit enable
Data_R = 0 //data stream 1101 portC6
DelayMS(2000) //to track progress, make 100?
Data_R =1
TE=1
TX_pwr = 0 // transmitter powered OFF
End Sub
'xxxxxxxxxxxxxxxxxxxxxxxxx
// set up outputs
'xxxxxxxxxxxxxxxxxxxxxx
Output(TX_pwr)
Output (TE)
Output(DATA_L)
Output(Data_R)
'xxxxxxxxxxxxxxxxxxxxx
// SETUP Variables
'xxxxxxxxxxxxxxxxxxx
TE = 1
ADCON1=$07 // adc regesters
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
DATA_L = 1 // set to HIGH
Data_R = 1
TX_pwr = 0
//hlvdl = 0110 //2.74- 3.04 volts
'xxxxxxxxxxxxxxxxxxxxxxx
'set up[ inputs
'xxxxxxxxxxxxxxxxxxxxxxxx
Input(s1)
Input(s2)
'xxxxxxxxxxxxxxxxxxxxxxxxxxx
//START OF PROGRAM
'xxxxxxxxxxxxxxxxxxxxxxxxxx
//pie2 = 0
//vdirmag = 1
While TRUE ' poll switches
If s1 = 0 Then
DelayMS(1000)
LEFT
EndIf
If s2 = 0 Then
DelayMS(1000)
RIGHT
EndIf
If s1 = 1 And s2 = 1 // DATA = 1111
Then TE = 1 ' transmit enable OFF
End If
Wend
Tried changing the bit 7 (high/low trip select bit) as well as change trip point but the LED blinks 11 times then stays on. The anode is tied to Vdd and Cathode tied to C.5
I must have the flag not resetting but I think / though I do/did. Must be something I missed or have the interrupt wrong?
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 1/7/2013 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Device = 18f2420
Clock = 8
//Config OSC=HS
Include "InternalOscillator.bas"
Include "utils.bas"
Include "convert.bas"
dim i as byte
Dim led1 As PORTC.5
sub lowVolt()
' Blink LED_RED 11X to indicate Low Battery voltage detected
For i = 0 to 10
HIGH( LED1)
delayms (1000)
LOW (LED1)
delayms (1000)
Next
asm 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 asm
end sub
HLVDCON=%10010000 // 2.74-3.04 volts trip
'HLVDCON.bits(7)=1 //1 = equal or exceeds 0 = lower than trip point
'HLVDCON.bits(5)=1 // voltage detect flag
'HLVDCON.bits(4)=1 // voltage detect enable bit
'HLVDCON.bits(3)=0 // bits 3-0 = range
'HLVDCON.bits(2)=1
'HLVDCON.bits(1)=1
'HLVDCON.bits(0)=0 // set for 2.74 - 3.04
PIE2.bits(2)=0 //HLVDIE
INTCON.bits(7)=1 //GIE/GIEH bit
INTCON.bits(6)=1 //PEIE/GIEL BIT
Output(led1)
HLVDCON.bits(5)=1 // voltage detect flag reset
While TRUE
If HLVDCON.bits(5)=0 Then // voltage below 3.04 - 2.79
lowVolt()
HLVDCON.bits(5)=1 // voltage detect flag reset
EndIf
Wend
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You set the HLVD up then if your voltage drops a interrupt happens your code stops and jump to see what it should do then returns to your main code where it was.
Do you see your code doing that
You could use onevent
This code I'm posting it for uart but you still can see how this works
Code:
module MyRX
// import USART library...
include "USART.bas"
// event handler type...
type TEvent = event()
// local and public variables...
dim FOnDataEvent as TEvent
public dim DataChar as USART.RCRegister.AsChar
// ISR routine...
interrupt OnRX()
FOnDataEvent()
end interrupt
// initialize...
public sub Initialize(pOnDataEvent as TEvent)
FOnDataEvent = pOnDataEvent
USART.ClearOverrun
USART.RCIEnable = true
enable(OnRX)
end sub
The main program code can now use the generic functionality of the interrupt module to take some specific action, through the use of an event…
program MyRXProgram
// import module...
include "USART.bas"
include "MyRX.bas"
// event handler
event OnRX()
if MyRX.DataChar = " " then
high(PORTD.0)
elseif MyRX.DataChar = "*" then
high(PORTD.1)
endif
end event
// main program
SetBaudrate(br19200)
MyRX.Initialize(OnRX)
while true
wend
Will work on it some more after studying your posted example.
I tried the min and max voltage trip point as well as high and low voltage trip points but they all worked the same.
I see what your saying, the code does its thing then dosen't really know what to do next.
Kinda what I was wondering, maybe the interrupt is not getting called but the LED blinks so it must be but going around in circles.
Will print out your posted example and study.
THANKS
This looked easy, so I coded it up to see if I was missing anything. Nope. It is easy.
I have set this up to check for low supply voltage by polling the HLVD register. If the voltage drops below the set point, an LED is illuminated. If the voltage rises above that set point, the LED is extinguished - which may not be exactly what you want to check for low batteries.
This code was tested on actual hardware, using a variable supply voltage from the PICkit2.
Code:
{
*****************************************************************************
* Name : HLVD Test *
* Author : Jon Chandler *
* Notice : Copyright (c) 2013 Creative Commons 3.0 sa-by-nc *
* : All Rights Reserved *
* Date : 1/8/2013 *
* Version : 1.0 *
* Notes : Poll the HLVD register to detect low Vdd voltage. *
* : Register settings may vary with device. *
*****************************************************************************
}
Device = 18f2520
Clock = 20
include ("utils.bas")
Dim LEDYellow As PORTB.3
Dim LEDBlue As PORTC.0
SetAllDigital
Output(LEDBlue)
Output(LEDYellow)
ledblue = 0
ledyellow = 0
delayms(2000)
LEDBlue = 1
LEDYellow = 1
hlvdcon =%00011011 'see section 22.0 for setup and table 26-4 for voltage setting
'set to 4 volts in this case
pir2.bits(2) = 0 'clear HLVD interrupt flag
pie2.bits(2) = 1 'enable HLVD interupt (section 22.2, sentence 5)
'intcon.bits(7) = 1 'enable HLVD interupt (section 22.2, sentence 5)
'not needed for polled result
while 1 = 1
if pir2.bits(2) = 1 then 'check HLVD interrupt
ledblue = 0
pir2.bits(2) = 0 'clear HVLD interrupt
else
ledblue = 1
end if
toggle (ledyellow)
delayms(1000)
wend
About half an hour start to posting here, following exactly the data sheet for the PIC18F2520. Be careful, spelling counts in the register names and they have similar names.
Be advised that not all 18F-series PICs have this feature.
Thanks Jon I will try it out.
Maybe a change of direction but maybe not. Was conversing with my wife during dinner and I realized I may be going in the right church but wrong pew.
SETUP = the user picks up the device, turns it on then attaches to helmet. Why have the LED blinking while riding? waste of battery power
Thinking, when user turns off the device then have the led blink but this happens when user turns off device.
Perhaps a delayed power off ? that triggers the interrupt for the HLVD?
Going to sleep on this after I look at the xcode Jon posted. I usually find I can absorb things after contemplating or thinking about might be a better description.
LOL -- I have been experimenting with Jons code wanting to configure a way to not have the LED (low battery) come on until the user shuts it off. Was looking a a pushbutton to check battery condition as well as a delayed power off. Just long enough to flash the LED. My mobil GPS has a spring loaded slide switch that turns the unit off and on going the same direction.
One question I had about Jons code was ("utils.bas"). Never seen with brackets around an include.
I experimented with the voltage setting bits 0-3 and using a low voltage battery pack found that all works as planed.
I was surprised it took Jon half an hour. I figured maybe 15 minutes tops!
He is pretty good using Swordfish but maybe he has never had a use to use the HLVD feature?
No, I have never used the HLVD module before. So, before charging blindly ahead, I actually read and understood the necessary parts of the data sheet. I could have avoided that and asked many questions in a forum somewhere, but why should I waste other peoples' time when everything is spelled out clearly?
I see where I made a big mistake I missed the PIR2 and PIE2 (your reference to mis-spelling?I charged ahead but didn't fully understand the data sheet COMPLETELY.
I admit I looked at that data sheet and got the thing to blink the LED but I missed setting the right registers. Face It I almost got it but almost doesn't count.
Now why the brackets around the include ("utils.bas")
I got curious and tried both but no difference?
You got that one right. Have found this out on several occasions.
Got a meeting tonight but still trying to come up with a way to turn off the device THEN it indicates the battery status (green or red led)