PDA

View Full Version : ADC on 12f675?



peu
- 14th March 2005, 19:40
I have the attached circuit and the following program:


TRISIO=%1 'Pic 12F675
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
cmcon=7
ansel=1
ADCON0=131


number VAR word

Pause 100
Loop:

ADCIN 0, number ' (0-1023)

IF number > 0 AND number < 400 Then
GPIO.1=1
GPIO.2=0
GPIO.4=0
EndIF
IF number > 401 AND number < 800 Then
GPIO.1=0
GPIO.2=1
GPIO.4=0
EndIF
IF number > 801 AND number < 1024 Then
GPIO.1=0
GPIO.2=0
GPIO.4=1
EndIF


GoTo loop
End


it does not work, any ideas?

Im trying to do my 1st ADC program, a simpler working one that make me understand the concept is welcome too!!!


Thanks a bunch

Dwayne
- 15th March 2005, 14:47
Hello Peu,

Peu>>TRISIO=%1 'Pic 12F675<

TRISIO is your input output switch.

I did not go through all of your code, but this put a flag up to me.

TRISIO=%XXXXXXX is a binary representation of the switches.
I question the usagle of just TRISIO=%1

You are using GPIO.0 as your input, thus I would assign TRISIO one of the following:

TRISIO=%00000001
or
TRISIO=1

Notice I did not put the % in front of the second one.
Because 1 = %00000001

I am not saying this is your entire problem. But you may want to check it out.

Dwayne

peu
- 15th March 2005, 16:38
I tried but it did not worked, I think Im missing something, but I can't figure it out.

pointers to adc examples for the 12f675/683 are welcome. Thanks

Acetronics2
- 15th March 2005, 16:50
Hi,Peu

I remember CMCON = 7 is used on my 16F628 to DISABLE all Analogic inputs ...
Verify if the same config. word for 12F675...

your Pb seems to be here ...

Alain

peu
- 15th March 2005, 17:05
with this new header:

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
'cmcon=7
ADCON0=%11000011 'dec 195
ansel=%001


it still does not work ... any idea?

Bruce
- 15th March 2005, 17:25
All you need to set is ADCON0.7 = 1 for right justification. PBP will
handle channel select bits and ADON.

Try this.


DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
number VAR word

CMCON = 7 ' Comparators off
ANSEL = %00000001 ' GPIO.0 A/D in, rest digital
ADCON0.7 = 1 ' Right justify for 10-bit
TRISIO = %00000001 ' GPIO.0 = input, rest outputs

Pause 100

Loop:
ADCIN 0, number ' (0-1023)
IF (number > 0) AND (number < 400) Then
GPIO 1=1
GPIO.2=0
GPIO.4=0
EndIF
IF (number > 401) AND (number < 800) Then
GPIO.1=0
GPIO.2=1
GPIO.4=0
EndIF
IF (number > 801) AND (number < 1024) Then
GPIO.1=0
GPIO.2=0
GPIO.4=1
EndIF
number = 0
GoTo loop

End

Assuming you have your config fuse options & oscillator setup
properly, this should work.

You can find several more 12F675 A/D examples here;
http://www.microengineeringlabs.com/resources/samples.htm#x4pbp

Acetronics2
- 15th March 2005, 17:53
here's an example ... with explanations !!!

Alain

peu
- 15th March 2005, 17:56
Bruce:

Thanks var word
loop:
thanks=$FFFF ' :)


it works, now I need to adjust the code because the led dont work as expected, but I put a serout after the adcin and the terminal reads the different values OK!!!

goto loop

Bruce
- 15th March 2005, 18:11
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
number VAR word

CMCON = 7 ' Comparators off
ANSEL = %00000001 ' GPIO.0 A/D in, rest digital
ADCON0.7 = 1 ' Right justify for 10-bit
GPIO = %00000000 ' Initialize outputs
TRISIO = %00000001 ' GPIO.0 = input, rest outputs

PAUSE 100

Loop:
ADCIN 0, number ' (0-1023)
IF (number > 0) AND (number < 400) Then
GPIO = %00000010
'GPIO.1=1
'GPIO.2=0
'GPIO.4=0
EndIF
IF (number > 401) AND (number < 800) Then
GPIO = %00000100
'GPIO.1=0
'GPIO.2=1
'GPIO.4=0
EndIF
IF (number > 801) AND (number < 1024) Then
GPIO = %00010000
'GPIO.1=0
'GPIO.2=0
'GPIO.4=1
EndIF
number = 0
GoTo loop

peu
- 15th March 2005, 18:16
MASTER !

I trully appreciate your prompt answers, thank you very much !!! :)

RFsolution
- 16th March 2005, 17:44
Hi

After looking to the examples above I have the following I want to do
As this is my first ADC code I need some tips and maybe some help

I want to do the following

ADC on GPIO.0 8 bit

I have a DC voltage between 0.5V and 2V approx
Which is varying all the time

I need to encode the DC variation by a tone between 67hz and 250hz
with 2-4 hertz incremental/value

The DC values needs to be stored in EEprom at setup
(example button is pushed at powerup to enter setup)
30 DC values should correspond to 30 tones (30 tone's might be set fix in the code)
So example:

0.55V = 67hz
0.64V = 71hz
0.71V = 74hz
etc...

Once setup and running: if a DC value is between or equal next value it has to
output the tone ( set in the code)
with a kind of hysteresis for the tone duration and before next sample)
and a Serout to check the values
Can anyone help me ?

Walter