Sei sulla pagina 1di 2

'Example code for battery monitor

'Uses Vdd as reference and internal oscillator


'Lights one LED when voltage is below 50% of Vdd
'Lights two LEDs when voltage is below 25% of Vdd
'For Proton IDE Lite Basic Compiler
Device =
' Pin 1:
' Pin 2:
' Pin 3:
' Pin 4:
' Pin 5:
' Pin 6:
' Pin 7:
' Pin 8:

12F675
Vdd
No connection
No connection
No connection
GP2 LED 2
GP1 LED 1
AN0 (voltage in)
Vss

XTAL = 4
' Set data code protection, code protection, brown out detection, watchdog timer
, and mclr function OFF;
' power-up timer ON; and oscillator to internal.
Config CPD_OFF, CP_OFF, BODEN_OFF, WDT_OFF, MCLRE_OFF, PWRTE_ON, INTRC_OSC_NOCL
KOUT
Dim ADC_Val As ADRESL.Word ' Combine ADRESL and ADRESH registers into a word va
riable
Dim i As Byte
' For/Next loop counter
Symbol LED1 = GPIO.1
Symbol LED2 = GPIO.2

' Define pin 6 as LED1


' Define pin 5 as LED2

DelayMS 500

' Wait for PIC to settle

' ----- Init the ADC ---------------------------------INIT_ADC:


TRISIO = %111001 ' Pins 5&6 as outputs, rest as inputs
ANSEL = %00110001
'ANSEL.0 = 1
' Analog
'ANSel.1 = 0
' Digital
'ANSel.2 = 0
' Digital
'ANSel.3 = 0
' Digital
'Ansel.4 = 1
' Set ADCS to internal clock
'Ansel.5 = 1
' "
'Ansel.6 = 0
' "
'Ansel.7 = 0
' Unused
ADCON0 = %10000000
' ADCON0.0 = 0
' ADC off
' ADCON0.1 = 0
' ADC status (GO/!DONE)
' ADCON0.2 = 0
' Channel 0 selected
' ADCON0.3 = 0
' "
' ADCON0.4 = 0
' Unused
' ADCON0.5 = 0
' Unused
' ADCON0.6 = 0
' Set VREF To VDD
' ADCON0.7 = 1
' Right justify the ADC result
' ----- Main ---------------------------------For i = 1 To 4
High LED1
DelayMS 250
Low LED1

'Flash the LEDs at startup

High LED2
DelayMS 250
Low LED2
Next i
While 1 = 1
GoSub GET_ADC
If ADC_Val < 256 Then
High LED2
Else
Low LED2
EndIf
If ADC_Val < 512 Then
High LED1
Else
Low LED1
EndIf
DelayMS 500
Wend

' Loop forever


' Read the ADC pin
'25% of Vdd

'50% of Vdd

' Wait for .5 second

' ----- Read the voltage ---------------------------------------GET_ADC:


ADCON0.0 = 1
' Enable ADC - bit 0 of ADCON0 register is the enabl
e bit: 1 = enabled, 0 = disabled.
DelayUS 50
' Wait for capacitors to charge
ADCON0.1 = 1
' Do conversion - bit 1 of ADCON0 register is status
(Go/Done): 1 =Go (start), 0 = Done
While ADCON0.1 = 1 : Wend ' Watch GO/DONE flag 'til conversion is done
ADCON0.0 = 0
' Disable the ADC
Return

http://www.rcgroups.com/forums/showthread.php?t=639405&page=4

Potrebbero piacerti anche