PDA

View Full Version : ADCIN - AD settings are wrong



teverett
- 2nd December 2006, 18:49
Photocell connected to PortA.0. I am using Picbasic Pro, ADCIN command. My programs just counts (incrementing a 2 or 4 “some-arbitrary” number) and then stops. It is suppose to tell me a digital value as read from the Photocell. Can someone tell me what I have done wrong? I am assuming I have my AD settings wrong but I do not know.

Using internal clock 8 MHz
10 bit resolution

Terence

‘------- Code below ------------------

'************************************************* ***************
'* Name : Test-Analog Photosensor.BAS *
'* Author : Terence *
'* Notice : Copyright (c) 2006 *
'* : All Rights Reserved *
'* Date : 12/01/2006 *
'* Version : *
'* Notes : Chip 16F767 with 24C32A Mem *
'* Reason : Testing A.0 ADCIN A/D converter *
'* Photocell 4K - 11K Ohm 5.10mm PDV-P8101-ND (Digi-key) *
'* PhotoCell Connected to Pin2 (PortA.0) + 10K to ground *
'* PhotoCell also connected to 5 VDD *
'* *
'************************************************* ***************
'---------------- Includes ----------------------
Include "modedefs.bas"

'---------------- Set I/O ----------------------
TrisA = %00000001 ' set inputs and outputs for A's ports
TrisB = %00000001 ' set inputs and outputs for B's ports
TrisC = %00000000 ' set inputs and outputs for C's ports

'---------------- Defines for Display -----------
Define DEBUG_REG PORTB ' Debug Port A
Define DEBUG_BIT 1 ' *** Debug pin Bit-1 ***
Define DEBUG_BAUD 9600 ' *** Baud Rate 9600 8 mhz ***
Define DEBUG_MODE 1 ' Set Serial Mode 1=Inverted
Define DEBUG_PACING 300 ' Delay 'in Us' between characters sent
' Remember all defines must be in UPPERCASE !!!!!!!
'---------------- Defines for ADCIN -----------
Define ADC_BITS 8 ' Set resolution of conversion
Define ADC_CLOCK 4 ' Set clock source (x/FOSC or FRC)
Define ADC_SAMPLEUS 50 ' Set sampling time (in uS)

'--------------- OSC Settings -------------------
Osccon = $78 ' set the timing $68 4 MHz -$78 8 MHz
Define OSC 8 ' 7E for 16F767

'----------------- E_Byte --------------------------
Define I2C_SCLOUT 1 'No need for the pullup resistor on the SCL pin

'---------------- Define Analog ------------------
Adcon0 = %11000111 ' set up for reading analog inputs
Adcon1 = %01001110 ' set up for reading analog inputs
Adcon2 = %00010000
CMCON = 0 ' set up for reading analog inputs
'-------------------- Var --------------------------
ADVAL var word
Addr Var Word ' 16-bit memory address within the Eeprom
PA1 var PortA.1
PA2 var PortA.2
PA3 var PortA.3
PA4 var PortA.4
PA5 var PortA.5
PB1 var PortB.1
PB2 var PortB.1
PB3 var PortB.1
PB6 var PortB.1
PB7 var PortB.1
PC0 var PortC.1
PC3 var PortC.1
PC6 var PortC.1
PC7 var PortC.1
'---------------------- Init -------------------------
Gosub SetUnUsedPinsToLow:
'---------------------- Main --------------------------
Main
gosub ReadPhotoSensor
Debug 254,1
Debug 254,2
Debug "Adval ",Dec Adval
Pause 1000
Goto Main
'-------------------------- Read Photo -----------------------------
ReadPhotoSensor: ' Determines Photo Sensor (On/Off)
ADCIN 0, ADVAL ' Set PortA.0 to be read for Photo
Return

'----------------------- Set Used Pins -----------------------
SetUnUsedPinsToLow:
low PA1
low PA2
low PA3
low PA4
low PA5
low PB1
low PB2
low PB3
low PB6
low PB7
low PC0
low PC3
low PC6
low PC7
Return
'------------------------ End ---------------------------
End

sayzer
- 2nd December 2006, 18:59
1. You need 10-bit res, but you set it to 8.
May not be the problem though.

2. Add @ DEVICE PIC16F767, INTRC_OSC to the first line.
Also this may not be the problem.
------------------

mister_e
- 2nd December 2006, 19:12
how the Photocell is connected? any pull-up/down?

1.CMCON=0? maybe CMCON=7.. but they're already off at POR so..
2.your ADCON0 setting use Channel 8 but adcin should override this for you
3.adcon1 should be set for right justified results
4. the maximum input impedance for analog source is 2.5K (section 12.1) so.. maybe you'll need something between the PhotoCell and the PIC a/d input. sometimes it works, sometimes not. look at your Photocell specs or measure it. What's the value when there's no light? maybe a little bit high ;)

teverett
- 3rd December 2006, 05:12
I made the changes and you were both right. Impedance was wrong and the settings were wrong -- now it works … Thank you very much.

Terence

sayzer
- 3rd December 2006, 08:57
Now, you will not know which setting corrected it.


------------------------------

teverett
- 4th December 2006, 16:32
I believe that the CMCON = 7 was key. I had tried all the other settings backward and forward trying to get it working before posting to this forum (Adcon0, Adcon1, Adcon2) but I had forgot about CMCON.

Once it was working - most of the resistors I tested on the Photocell (breadboard) came back with results. But once I started looking into the resistance values on my prototype, I found that an incorrect connection was made under a capacitor (SMT) which gave me false readings. I rewired and changed the settings - it then started working.

So what this forum gave me was several clues to finding solutions two different problems. I really appreciate all the time and effort you guys put in to helping everyone here online.

You guys make using PICs and Picbasic Pro possible!

Thanks again!
Terence