PDA

View Full Version : Code Problem?



scottl
- 25th June 2006, 17:52
Can someone see anything wrong with the following code? I can read GPIO.0 but when I try to read any other A/D I get nothing from any of the A/D. I am sure it is my code can someone help? If I can just get it to work I will clean up the code. The goal is the read GPIO.0 and GPIO.1 then subtract the lower from the higher. Any other suggestions?

@ device pic12F675,intrc_osc,wdt_off,protect_off,MCLR_off

dEFINE OSC 4 ' We're using a 4 MHz oscillator
DEFINE OSCCAL_1k 1 ' DEFINE OSCCAL FOR PIC12C671 or PIC12F675

adval VAR WORD 'Create adval to store result
adval1 VAR WORD 'Create adval to store result
adval_C VAR WORD 'Create adval_C for celsius Conversion
adval_F_18 VAR WORD 'Create adval_F for farenheit Conversion
adval_F_32 VAR WORD
sign var word
sign = "+"
check var byte

ANSEL = 49
CMCON=7

High GPIO.4
pause 5
low GPIO.4
pause 500

loop:

ADCON0 = %10000001 ' + (J*8) ' SET A/D Fosc/8 + A/D = ON
PAUSE 10 ' PAUSE 10mS FOR CHANNEL SETUP
adcin GPIO.0, adval

ADCON0 = %10000101
PAUSE 10 ' PAUSE 10mS FOR CHANNEL SETUP
adcin GPIO.1, adval1

adval = (adval * 5) / 10
adval1 = (adval1 * 5) / 10

if adval < adval1 then
adval_C = adval1 - adval
check = 1
else
adval_C = adval - adval1
check = 0
endif

if (check = 1) & (adval_C > 32) then
sign = "-"
else
sign = "+"
endif

adval_F_18 = (adval_C * 18)/10
adval_F_32 = (adval_F_18 + 32)

SerOut2 GPIO.5,84, [sign, DEC adval_F_32,13,10]

Pause 1000' Wait 1 seconds
GoTo loop
end

Thanks in advance,

Scott

Archilochus
- 25th June 2006, 18:12
Hi Scott,
I'm not too familiar with the '675, but don't you need to set your ANSEL register so that GP.1 is an analog input? It looks like only GP.0 is set as analog in.

Arch

scottl
- 25th June 2006, 19:47
Arch,

You are correct! I have changed this to ANSEL = 51 or ANSEL = %00110011. I have changed this but have the same problem! It only reads 32 over SEROUT2 no change.

If I rem out the ADCIN1 I get the correct value via ADCIN0 checked by multimeter. It only happens when I add ADCIN1. Does it look like the ADCON0 register?

Any other suggestions?

Thanks,

Scott

Archilochus
- 25th June 2006, 21:23
I've not used ADCIN much, but here's some other things to check:

I don't see anywhere in your code that sets the pins as inputs - you might try setting up the TRISIO register as needed.

In the PBP manual it shows an example like this:
"ADCIN 0, B0 ; Read channel 0 to B0"

The 'channel' is selected with a number rather than port pin name - don't know if that makes any difference or not.

Arch

modifyit
- 26th June 2006, 04:37
Here is another way of aproaching the A/D based on some code Melanie posted a long time ago, this uses direct access to the pics registers instead of using PBP's routines:


Analog_In:
ANSEL=%00110001
ADCON0=%10000001;
' Enable ADC Module on pin AN0
PauseUS 50;
' 50uS Pause to allow sampling Capacitor to charge
ADCON0.1=1
' Start Conversion
While ADCON0.1=1:Wend
' Wait for conversion to complete
AnalogIn.Highbyte=ADRESH
AnalogIn.Lowbyte=ADRESL
' Read 16-bit Result
RETURN


By changing ADCON0 to different values you should be able to read analog in on all the other analog pins