PDA

View Full Version : 16F616 A to D



Tobias
- 1st May 2009, 06:25
I can't seem to get the A/D to work correctly. I have tried different configs yet no luck. The pin I am using to measure voltage is RC0/AN4/C2IN+. So I configure the pin to analogue with ANSEL.4=1, disable the comparator with CM2CON0.7=0 and select the analogue bit with ADCON0.5-2.

I check voltage at the pin with my scope and its .650v. Yet debugging I get a value of '0' with any input from 0-5v. Any input is appreciated.



INCLUDE "modedefs.bas"

FF var PortC.0
FrontOut var PortC.1
RearOut var PortC.2

Voltage var word
Perfect var word

define OSC 20
DEFINE DEBUG_REG PortC 'SET DEBUG PIN PORT
DEFINE DEBUG_BIT 5 'SET DEBUG PIN BIT
DEFINE DEBUG_BAUD 9600 'SET DEBUG BAUD RATE
DEFINE DEBUG_MODE 1 'SET DEBUG MODE 0=TRUE / 1=INVERTED

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 10 ' Set sampling time in uSec

TRISC.0 = 1 'FF (Flip/Flop)
TRISC.1 = 0 'FrontOut
TRISC.2 = 0 'RearOut

'0 Digital 1 Analogue
ANSEL.6=0 'RearOut
ANSEL.5=0 'FrontOut
ANSEL.4=1 'FF

CM2CON0.7=0 'Disable comparator

ADCON0.7=1 'Right Justified
ADCON0.6=0 ''Voltage reference is VDD
ADCON0.5=0 'Turns on AN4 to analogue
ADCON0.4=1
ADCON0.3=0
ADCON0.2=0
ADCON0.0=1

Perfect = 512

Loop:
GOSUB Get_Voltage
gOSUB Compare
debug "Voltage=", DEC VOltage, cr
Goto Loop

Get_Voltage:
ADCIN FF, Voltage
Return

Compare:
If Voltage < Perfect then
Low FrontOut
endif

If Voltage > Perfect then
Low RearOut
endif
Return

Archangel
- 1st May 2009, 07:15
You have capture compare registers to deal with, and others. CCP1CON, CM1CON0,CM2CON0, CM2CON1, ADCON1

Tobias
- 1st May 2009, 15:37
Am I doing the ADCON0.x correctly?

I added CM1CON0.7=0 so both comparators are disabled.

I still get Voltage=0 when I debug.


INCLUDE "modedefs.bas"

FF var PortC.0
FrontOut var PortC.1
RearOut var PortC.2

Voltage var word
Perfect var word

define OSC 20
DEFINE DEBUG_REG PortC
DEFINE DEBUG_BIT 5
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 1

Define ADC_BITS 10
Define ADC_CLOCK 3
Define ADC_SAMPLEUS 10

TRISC.0 = 1 'FF (Flip/Flop)
TRISC.1 = 0 'FrontOut
TRISC.2 = 0 'RearOut

'0 Digital 1 Analogue
ANSEL.6=0 'RearOut
ANSEL.5=0 'FrontOut
ANSEL.4=1 'FF
CM1CON0.7=0
CM2CON0.7=0 'Disable comparator

ADCON0.7=1 'Right Justified
ADCON0.6=0 'Voltage reference is VDD
ADCON0.5=0 'Turns on AN4 to analogue
ADCON0.4=1
ADCON0.3=0
ADCON0.2=0
ADCON0.0=1

CCP1CON.3=0
CCP1CON.2=0
CCP1CON.1=0
CCP1CON.0=0

Perfect = 512

Loop:
GOSUB Get_Voltage
gOSUB Compare
debug "Voltage=", DEC VOltage, cr
Goto Loop

Get_Voltage:
ADCIN FF, Voltage
Return

Compare:
If Voltage < Perfect then
Low FrontOut
endif

If Voltage > Perfect then
Low RearOut
endif
Return

mister_e
- 1st May 2009, 16:37
When you use ADCIN you must use the Channel #, not the PORT pin name.


ADCIN 4, Voltage ' PORTC.0 = AN4