ruijc
- 4th June 2010, 21:27
Hi all,
I'm having some issues reading 2 analog voltages with Pic 16F88.
I have successfully make it work with only AN0, but when using 2 or more ports just doesn't want to work. Something is missing and i cant see it.
The hardware is simple:
Pic has only 3 connections ( output to RS232 and 2 analog inputs ). I have a 330K resistor from VDD to AN0 and a 47K resistor from VDD to AN1.
Measuring voltage on these 2 pins i have AN0 = 1.19V and AN1=4.82V
If my math is correct i should have readings like AN0= 244 and AN1=986 ( 10Bits calculation ).
Instead i have AN0= 1011 and AN1=1014 ( most of the times both reading are the same ).
Here's the code:
[code]
'****************************************
'DEFINEs
@ DEVICE PIC16F88, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88, MCLR_OFF
@ DEVICE PIC16F88, PROTECT_OFF
DEFINE OSC 8
@ DEVICE PIC16F88, CPD_OFF
@ DEVICE PIC16F88, LVP_OFF
@ DEVICE PIC16F88, BOD_OFF
@ DEVICE PIC16F88, PWRT_OFF
@ DEVICE PIC16F88, WDT_OFF
@ DEVICE PIC16F88, CCPMX_OFF
@ DEVICE PIC16F88, FCMEN_OFF
@ DEVICE PIC16F88, IESO_OFF
@ DEVICE PIC16F88, DEBUG_OFF
@ DEVICE PIC16F88, LVP_OFF
'***************************************
OSCTUNE=0
OSCCON = %01111110
INTCON = 0
PIE1 = 0
PIE2 = 0
CMCON = %00001111 'comparators off
CVRCON = 0
CCP1CON = 0
'*******************************************
'ADC
DEFINE ADC_BITS 10 ' Set number of bits in result ( 8 or 10 bits )
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
'******************************************
'ADC parameters
ADCON0 = %11000000 ' clock source
ANSEL =%00000011 ' Porta.1 and .2 used by ADC
ADCON1 =%10000011 ' Right justify
'***************************************
INCLUDE "modedefs.bas"
DEFINE DEBUG_REG PORTA
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 1
'*************************************
' variables
loop VAR WORD
som VAR WORD
luz VAR WORD
vals VAR WORD
vall VAR WORD
A VAR word
B VAR word
'****************************************
PORTA=0
PORTB=0
TRISA=%10110111
TRISB=%11000100
'***************************************
'PINS
Si var PORTA.0 'input1
Li var PORTA.1 'input2
a2 var PORTA.2 'nc
com var PORTA.3 'RS232
A4 var PORTA.4 'nc
MCLR var PORTA.5 'MCLR
A6 var PORTA.6 'nc
A7 var PORTA.7 'nc
B0 var PORTB.0 'nc
b1 var PORTB.1 'nc
But var PORTB.2 'Button
B3 var PORTB.3 'nc
b4 var PORTB.4 'nc
Led var PORTB.5 'Led
PGC var PORTB.6 'PGC
PGD var PORTB.7 'PGD
'***************************************
clear
'***************************************
INIT:
high led
pause 50
debug "Starting...",13,10
debug $0D, $0A
PAUSE 50
for loop=0 to 7
toggle led
next
pause 1500
'**********************************************
start:
gosub s1
gosub s2
gosub out
goto start
'*******************************************
S2:
ADCON0 = %11001000
luz=0
vall=0
for loop=0 to 15
adcin PORTA.1,vall
pause 80
luz=luz+vall
next
luz=luz/16
return
'*********************************************
S1:
ADCON0 = %11000000
som=0
vals=0
for loop=0 to 15
adcin PORTA.0,vals
pause 80
som=som+vals
next
som=som/16
return
'*********************************************
out:
debug 13,10
debug "AN0 = ",dec som,13,10
debug 13,10
debug "AN1 = ",dec luz,13,10
debug 13,10
debug 13,10
debug 13,10
return
'******************************************
end
[\code]
Any help would be appreciated
I'm having some issues reading 2 analog voltages with Pic 16F88.
I have successfully make it work with only AN0, but when using 2 or more ports just doesn't want to work. Something is missing and i cant see it.
The hardware is simple:
Pic has only 3 connections ( output to RS232 and 2 analog inputs ). I have a 330K resistor from VDD to AN0 and a 47K resistor from VDD to AN1.
Measuring voltage on these 2 pins i have AN0 = 1.19V and AN1=4.82V
If my math is correct i should have readings like AN0= 244 and AN1=986 ( 10Bits calculation ).
Instead i have AN0= 1011 and AN1=1014 ( most of the times both reading are the same ).
Here's the code:
[code]
'****************************************
'DEFINEs
@ DEVICE PIC16F88, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88, MCLR_OFF
@ DEVICE PIC16F88, PROTECT_OFF
DEFINE OSC 8
@ DEVICE PIC16F88, CPD_OFF
@ DEVICE PIC16F88, LVP_OFF
@ DEVICE PIC16F88, BOD_OFF
@ DEVICE PIC16F88, PWRT_OFF
@ DEVICE PIC16F88, WDT_OFF
@ DEVICE PIC16F88, CCPMX_OFF
@ DEVICE PIC16F88, FCMEN_OFF
@ DEVICE PIC16F88, IESO_OFF
@ DEVICE PIC16F88, DEBUG_OFF
@ DEVICE PIC16F88, LVP_OFF
'***************************************
OSCTUNE=0
OSCCON = %01111110
INTCON = 0
PIE1 = 0
PIE2 = 0
CMCON = %00001111 'comparators off
CVRCON = 0
CCP1CON = 0
'*******************************************
'ADC
DEFINE ADC_BITS 10 ' Set number of bits in result ( 8 or 10 bits )
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
'******************************************
'ADC parameters
ADCON0 = %11000000 ' clock source
ANSEL =%00000011 ' Porta.1 and .2 used by ADC
ADCON1 =%10000011 ' Right justify
'***************************************
INCLUDE "modedefs.bas"
DEFINE DEBUG_REG PORTA
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 1
'*************************************
' variables
loop VAR WORD
som VAR WORD
luz VAR WORD
vals VAR WORD
vall VAR WORD
A VAR word
B VAR word
'****************************************
PORTA=0
PORTB=0
TRISA=%10110111
TRISB=%11000100
'***************************************
'PINS
Si var PORTA.0 'input1
Li var PORTA.1 'input2
a2 var PORTA.2 'nc
com var PORTA.3 'RS232
A4 var PORTA.4 'nc
MCLR var PORTA.5 'MCLR
A6 var PORTA.6 'nc
A7 var PORTA.7 'nc
B0 var PORTB.0 'nc
b1 var PORTB.1 'nc
But var PORTB.2 'Button
B3 var PORTB.3 'nc
b4 var PORTB.4 'nc
Led var PORTB.5 'Led
PGC var PORTB.6 'PGC
PGD var PORTB.7 'PGD
'***************************************
clear
'***************************************
INIT:
high led
pause 50
debug "Starting...",13,10
debug $0D, $0A
PAUSE 50
for loop=0 to 7
toggle led
next
pause 1500
'**********************************************
start:
gosub s1
gosub s2
gosub out
goto start
'*******************************************
S2:
ADCON0 = %11001000
luz=0
vall=0
for loop=0 to 15
adcin PORTA.1,vall
pause 80
luz=luz+vall
next
luz=luz/16
return
'*********************************************
S1:
ADCON0 = %11000000
som=0
vals=0
for loop=0 to 15
adcin PORTA.0,vals
pause 80
som=som+vals
next
som=som/16
return
'*********************************************
out:
debug 13,10
debug "AN0 = ",dec som,13,10
debug 13,10
debug "AN1 = ",dec luz,13,10
debug 13,10
debug 13,10
debug 13,10
return
'******************************************
end
[\code]
Any help would be appreciated