Re: Battery monitoring - ever tried AN1072?
OK, now I see how this works. Sorry I did not gave the proper attention to the AN1072.
It works in reverse. It does supply a 0,6 volts to the ADC input and the refererence is read from the Vdd supply.
Unfortunately this is NOT what Roger wants. He has a stable Vdd from his LDO regulator so his ADRES result will always be the same, no matter the battery condition.
Clever technique but not the one needed here...
Ioannis
Re: Battery monitoring - ever tried AN1072?
I'll need to download the latest DS, mine is from 2007 :frown:
George, would you have a little piece of PICBASIC code about how to use the 0,6V reference please?
As a matter of fact, my AN1072 is from 2006 and there is no code example in there. And even if there would be some, it wouldn't be in PICBASIC anyway.
1 Attachment(s)
Re: Battery monitoring - ever tried AN1072?
Thanks Ioannis. I just replied while you were sending your reply.
The first lines of the AN1072 say:
INTRODUCTION
This application note describes how to measure the voltage supplied to a PIC ® microcontroller, VDD . The device used in preparation of this application note was the PIC16F690. The ability to measure VDD lends itself to battery applications where VDD is likely to fall over time. In this application note, an example program is provided with routines to measure VDD.
THEORY OF OPERATION
Select Microchip PIC microcontrollers contain a 0.6V or 1.2V internal reference that is selectable as an input to
the ADC module. This provides a fixed reference to allow measurement of VDD . The PIC16F690 was chosen for this feature.
A:- I can't still find a code example in this AN.
B:- Can this AN be wrong?
Right; on MICROCHIP's website, I could find two ASM code examples....but I don't know machine language or I wouldn't probably be on this forum :wink:
But here they are (ZIPfile) so if somebody want's to have a look and "translate" the essential stuff in PICBASIC, it would be great.
In case I can't really use the 0,6 reference voltage, I plan to make my circuit looking like this:
Attachment 8059
Attachment 8058
Re: Battery monitoring - ever tried AN1072?
Roger, for this to work you need to get rid of the LDO and feed a 4,5Volt battery pack to the PIC. That way you will measure the Vdd voltage in respect with the VP6 input to the ADC. There is no other way to do it.
Your configuration would be:
Code:
ADCON0=%10110101 ' Here we drive the 0,6 volts to the ADC
ADCON1=%00110000 ' I prefer to be sure to have FRC for the beginning at least
VRCON= %00010000 ' Enable the VP6 reference
Now if you want to measure the Vdd, just do this:
Code:
ADCON0.1=1
while ADCON0.1: wend
Bat_Value.Byte1=ADRESH:Bat_Value.Byte0=ADRESL
But remember, you have to drop the LDO and supply your circuit from the battery directly.
Ioannis
Re: Battery monitoring - ever tried AN1072?
Quote:
Originally Posted by
Ioannis
...you need to get rid of the LDO and feed a 4,5Volt battery pack to the PIC. That way you will measure the Vdd voltage in respect with the VP6 input to the ADC.
Thanks Ioannis; I now understand what you mean.
This morning, I tried to manually setup the ADC but it was always "failing" (= giving same value all time); you just explained why it didn't work ;)
I found an example on which I based my test code here.
I'll give your example a try :)
1 Attachment(s)
Re: Battery monitoring - ever tried AN1072?
...and here are the two code example files I was talking about in my post#43 ;-)
Attachment 8060
Re: Battery monitoring - ever tried AN1072?
There is a post by Darrel re this but I can't find it. I don't have any PICBasic code to hand but this in Proton might help -
Code:
ADCON1 = %00100000 ' ADCS=010 Fosc/32 4usec
Symbol Go_Done = ADCON0.1
Symbol ADON = ADCON0.0
Symbol VP6EN = VRCON.4 ' 0.6V Reference Enable Bit
Dim Ad_Result As ADRESL.Word ' Convert the ADRESL register into a WORD variable
Dim VDD As Word
VP6EN = 1 ' Turn 0.6V reference ON
ADCON0 = %10110101 ' right justify, Vdd, select 0.6v ref, ADC on
DelayUS 100 ' Wait for sample/hold capacitors to charge and VP6 to settle
Go_Done = 1 ' Start conversion
While Go_Done = 1 : Wend ' Poll the GO_DONE flag for completion of conversion
ADCON0 = %10110100 ' right justify, Vdd, Select 0.6v ref, ADC off
VP6EN = 0 ' Turn 0.6V reference OFF
VDD = 6144/Ad_Result ' convert input reading to VDD voltage *VDD must be a Word
' 0.6 * 1024 = 614.4 - 6144 To get 1/10th
George
"Right Justifying" is the key
Thanks Ioannis and George,
Here is the corected and 100% working code example using ADCIN:
Code:
' PIC 16F690 Fuses
@ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
'-------------------------------------------------------------------------------
' Registers 76543210
OPTION_REG = %10000000 'OPTION register
ANSEL = %00000000 'Select analog inputs Channels 0 to 7
ANSELH = %00000000 'Select analog inputs Channels 8 to 11
WPUA = %00000000 'Select weak pull-ups
WPUB = %00000000 'Select weak pull-ups
ADCON0 = %00000000 'AD Module
ADCON1 = %00000000 'AD control register
CM1CON0 = %00000000 'Comparator1 Module
CM2CON0 = %00000000 'Comparator2 Module
INTCON = %00000000 'INTerrupts CONtrol
TRISA = %00000000 'Select Input/Output (0 to 5)
PORTA = %00000000 'Set High/Low (0 to 5)
TRISB = %00000000 'Select Input/Output (4 to 7)
PORTB = %00000000 'Set High/Low (4 to 7)
TRISC = %00000000 'Select Input/Output (0 to 7)
PORTC = %00000000 'Set High/Low (0 to 7)
'-------------------------------------------------------------------------------
' Defines
DEFINE OSC 8
DEFINE LCD_DREG PORTC 'LCD data port
DEFINE LCD_DBIT 4 'LCD data starting PORT.bit (0 or 4)
DEFINE LCD_RSREG PORTC 'LCD register select port
DEFINE LCD_RSBIT 3 'LCD register select bit
DEFINE LCD_EREG PORTC 'LCD enable port
DEFINE LCD_EBIT 2 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE ADC_BITS 10 'Number of bits in ADCIN result
'-------------------------------------------------------------------------------
' Init display
' ELECTRONIC ASSEMBLY DOGM081 LCD display Mandatory settings
' See datasheet for circuitry changes by 5V or 3,3V operation
PAUSE 1000 'Time to settle Vdd (THIS IS CRUCIAL FOR THIS DISPLAY!!!)
LCDOUT $FE, $29 'Function Set: 4 bits bus mode
LCDOUT $FE, $1C 'Bias set
LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52 or 3,3V=55)
LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
LCDOUT $FE, $78 'Contrast (LowByte)
'-------------------------------------------------------------------------------
' Variables
Bat_Value var word 'holds ADC value
Bat_Value = 0
'-------------------------------------------------------------------------------
' Start program
MAIN:
ADCON0 = %10000000 ' Here we drive the 0,6 volts to the ADC
VRCON = %00010000 ' Enable the VP6 reference
ADCIN 13, Bat_Value ' Select the VP6 channel's address ADCON0 = %xx1101xx
Bat_Value.HIGHBYTE = ADRESH : Bat_Value.LOWBYTE = ADRESL
LCDOUT $FE,2,DEC4 Bat_Value
PAUSE 1000
GOTO MAIN
END
Re: "Right Justifying" is the key
Did you see how to convert he ADC count in Bat_Value to volts?
I see you are still not using a recommended value for ADCS in ADCON1.
George
1 Attachment(s)
the maths and the proof the system works
Yes George,
For any reason, either ADCON1.ADCS setting will affect the results.
For those interested in the maths (where "2n" is 1024 because I use 10bits in ADC):
Attachment 8065
Regarding my results and comparing them to the table A-1 (Appendix A: AC Result Table in AN1072), something must be wrong.
A 135 ADC result corresponds to 4,52V and 147 to 4,16V (?!).
Grrrrrr! I didn't notice I hadn't removed the voltage regulator on my breadboard. My PICkit2 programmer delivers 4,78VDC (even if it shows 5V in the "VDD PICkit2" field) so the "new" ADC values I get now seem to be much closer to what they are supposed to be "on the paper".
For 4,82V displayed on my FLUKE87, the ADC result is now 127 (exact as table A-1) and @4,31V I get 142 (exact as table A-1).
((1024 - 1) / 4,82) * 0,6 = 127 and ((1024 - 1) / 4,31) * 0,6 = 142
Re: the maths and the proof the system works
Glad you did it Roger! Bravo!
Ioannis
Re: the maths and the proof the system works
Quote:
For any reason, either ADCON1.ADCS setting will affect the results.
I don't know the internals of ADCIN, but unless it sets the ADCS bits to something else, you're ADC clock source will be Fosc/2 which at 8Mhz will give a Tad of 250ns. Fosc/16 or Fosc/32 are recommended.
George
Re: the maths and the proof the system works
George, If you do not want any headaches, just use the Frc. The internal 500Khz clock is good enough for most applications.
Plus, you do not worry for any timing violations.
Ioannis