PDA

View Full Version : 12f675 A/d Miseries



MARAD75
- 28th December 2006, 23:09
Hello all
Running a 12F675 with 1 channel of a/d. Can someone take a look at this code and help me figure out why the a/d isn't running. (at least doesn't seem so) This thing is driving me crazy! I think it should run but it doesn't.
Symptoms: My test board shows a/d in to pin as 0-5volts, but chip stays locked in "outrange" and led flashes on and off ok.
What can I do to fix the code?
Help will be appreciated-might save me from the rubber room!
Thanks,
Ron

Here's the code:


@_CONFIG_INTRC_OSC_NOCLKOUT &_WDT_ON &_PWRTE_ON &_MCLRE_OFF & BODEN_OFF
'INTERNAL OSCILLATOR (4 MHZ)
'ENABLE WATCH DOG TIMER
'ENABLE POWER UP TIMER
'DISABLE MASTER CLEAR FUNCTION

'************************************************* ***************
'VARIOUS OPERATION PARAMETERS

'GPIO.0 IS A/D INPUT FROM VOLTAGE DIVIDER TO SET HIGH OR LOW
'CHARGE. THIS FUNCTION TURNS ON 4710 MOSFET SWITCH (GPIO.4)
'AND LED (GPIO.2) (STEADY ON) TO INDICATE FULL 3 AMP. CHARGE
'TO BATTERY BETWEEN 12 VOLTS AND 16 VOLTS. OUTSIDE OF THESE
'VOLTAGES, GPIO.4 IS LOW AND GPIO.2 FLASHES ON AND OFF, ONCE
'PER SECOND. GPIO.3 IS MCLRE, PULLED TO Vcc BY 4.7K RESISTOR.
'GPIO.0 AND GPIO.3 ARE INPUTS AND GPIO.2 AND GPIO.4
'ARE OUTPUTS. (OTHERS COME UP AS INPUTS
' HARDWARE CONNECTIONS
'====================
'GPIO.0 A/D INPUT TO ADC CHANNEL 0
'GPIO.1 N/C
'GPIO.2 OUTPUT TO CASE LED(flashes on and off)
'GPIO.3 MCLR PIN
'GPIO.4 HIGH TO PHOTOVOLTAIC MOSFET DRIVER
'GPIO.5 N/C
'=====================

DEFINE OSC 4
DEFINE ADC_BITS 10 '10 BIT A/D CONVERSION RESULT
DEFINE ADC_CLOCK 3 'INTERNAL A/D RC CLOCK
DEFINE ADC_SAMPLEUS 50 'SET SAMPLE TIME IN MICROSECONDS
CMCON = 7 'TURN COMPARITORS OFF
adval0 VAR WORD 'Create adval0 to store result
TRISIO = %001001 ' Set GSIO 0 & 3 TO INPUTS
ANSEL = %01100001 ' Set GSIO 0 TO ANALOG A/D IN W/Frc
' OTHER PINS TO DIGITAL
ADCON0 = %10000001 ' Vcc REF/Turn on A/D Module
PauseUs 50 'Wait 50 MICROSECONDS
START:
ADCON0.1 = 1 'Start Conversion (GO/NOT DONE BIT SET)

WHILE ADCON0.1 = 1:WEND 'MOVE ON WHEN FINISHED WITH A/D CONVERSION
ADCIN 0, ADVAL0 'DEFINE C0NVERSION IN VAR ADVAL0

IF (ADVAL0 <= 906) AND (ADVAL0 >= 604) Then INRANGE
'GO TO FULL 3 AMP RATE (MOSFET TURNED ON)
IF (ADVAL0 <= 603) OR (ADVAL0 >= 907) Then OUTRANGE
'GO TO TRICKLE CHARGE (MOSFET OFF-NO RESISTOR SHUNT IN)
'10 ohm IN SERIES WITH CHARGER.
INRANGE: 'LED ON STEADY-MOSFET TURNED ON
High GPIO.4: High GPIO.2
GoTo START

OUTRANGE:'LED FLASHES ON AND OFF MOSFET OPEN
Low GPIO.4: High GPIO.2
Pause 500: Low GPIO.2: Pause 490: GoTo START

End

Darrel Taylor
- 29th December 2006, 04:17
Hi Ron,

Well there are still several questions to be able to figure out what's wrong. Such as ... what is the voltage of the charging power source? What are the voltage divider resistor values. Where is the reading being taken, before or after the 10 ohm resistor. and probably a few others.

However, there are a few things going on in the program that are suspect, but may not really be the problem.

You seem to be trying to use both the built-in PBP ADCIN command, and manually manipulating the A/D registers. You should pick one method or the other, not both.

In this line...
ANSEL = %01100001 ' Set GSIO 0 TO ANALOG A/D IN W/Frc
' OTHER PINS TO DIGITALIt sets the A/D oscillator to FOSC/64, which is listed as "outside of recommended range" in the datasheet.

It then starts a conversion, waits for it to finish, then discards the result. That is followed by an ADCIN statement that changes the oscillator to FRC, (since that was specified in the ADC_CLOCK define), and takes another reading.

Again, this won't cause the problem you're seeing, but it's still not right.

So maybe it's something with the questions in the first paragraph. ??
<br>

MARAD75
- 29th December 2006, 13:33
Hi Darrel

The resistors are set up to provide a range of voltages somewhere between 0 and 4.75 volts to a/d for battery voltage of 0 to 20. I am still tweaking them to push it as close to 5 volts as possible. (will change the a/d numbers in the code accordingly to get as close to 12 and 16 volts as possible)
I am not quite sure I understand your first email! Would you mind changing the code in my example to what it should be and re post it? I would really appreciate this, since this chip seems to have caused many an otherwise good code writer to "come unglued"! I do know that its Data Sheet is based on MPASM and Microchip's language; but I have tried for 10 years, and cannot make sense of that jibberish, anyway!!

Thanks again!
Ron

Darrel Taylor
- 30th December 2006, 04:54
Well, here's what I think you should have...
@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_OFF
'INTERNAL OSCILLATOR (4 MHZ)
'ENABLE WATCH DOG TIMER
'ENABLE POWER UP TIMER
'DISABLE MASTER CLEAR FUNCTION

'************************************************* ***************
'VARIOUS OPERATION PARAMETERS

'GPIO.0 IS A/D INPUT FROM VOLTAGE DIVIDER TO SET HIGH OR LOW
'CHARGE. THIS FUNCTION TURNS ON 4710 MOSFET SWITCH (GPIO.4)
'AND LED (GPIO.2) (STEADY ON) TO INDICATE FULL 3 AMP. CHARGE
'TO BATTERY BETWEEN 12 VOLTS AND 16 VOLTS. OUTSIDE OF THESE
'VOLTAGES, GPIO.4 IS LOW AND GPIO.2 FLASHES ON AND OFF, ONCE
'PER SECOND. GPIO.3 IS MCLRE, PULLED TO Vcc BY 4.7K RESISTOR.
'GPIO.0 AND GPIO.3 ARE INPUTS AND GPIO.2 AND GPIO.4
'ARE OUTPUTS. (OTHERS COME UP AS INPUTS
' HARDWARE CONNECTIONS
'====================
'GPIO.0 A/D INPUT TO ADC CHANNEL 0
'GPIO.1 N/C
'GPIO.2 OUTPUT TO CASE LED(flashes on and off)
'GPIO.3 MCLR PIN
'GPIO.4 HIGH TO PHOTOVOLTAIC MOSFET DRIVER
'GPIO.5 N/C
'=====================

DEFINE OSC 4
DEFINE ADC_BITS 10 '10 BIT A/D CONVERSION RESULT
DEFINE ADC_CLOCK 3 'INTERNAL A/D RC CLOCK
DEFINE ADC_SAMPLEUS 50 'SET SAMPLE TIME IN MICROSECONDS
adval0 VAR WORD 'Create adval0 to store result

CMCON = 7 'TURN COMPARITORS OFF
TRISIO = %001001 ' Set GSIO 0 & 3 TO INPUTS, others to OUTPUT
ANSEL = %00110001 ' Set GSIO 0 TO ANALOG A/D IN W/Frc
ADCON0.7 = 1 ' Right Justify for 10-bit

START:
ADCIN 0, ADVAL0 ' Read A/D channel 0
IF (ADVAL0 <= 906) AND (ADVAL0 >= 604) Then ; INRANGE
High GPIO.4 'GO TO FULL 3 AMP RATE (MOSFET TURNED ON)
High GPIO.2
else ; OUTRANGE
Low GPIO.4 'GO TO TRICKLE CHARGE (MOSFET OFF-NO RESISTOR SHUNT IN)
High GPIO.2 '10 ohm IN SERIES WITH CHARGER.
Pause 500
Low GPIO.2
Pause 490
endif
GoTo START

End

Also, there were some problem's with the CONFIG line. You can't see it in the post because it's all scruched up to the left. But, when you "Quote" post #1 you can see that there's no space between the @ and __CONFIG, and there's only 1 underscore before the CONFIG. There should be 2.

Added: And there should be a space between __CONFIG and the first parameter _INTRC_OSC_NOCLKOUT.

HTH,

MARAD75
- 30th December 2006, 18:33
Darrel,
The code you modified is working perfectly! Thanks very much! I was about to enter the proverbial "rubber room" over this one. I hope these examples will be helpful to others on the forum who have been messed over by the 12F675.
By the way, have a happy new year!
Ron

Darrel Taylor
- 31st December 2006, 12:05
That's right. Bounce it off of us, before bouncing off the walls. :)

Ha Ha, Happy New Year to you too!
<br>

mystified
- 15th December 2008, 03:31
clean
define adc_bits 10 'adc settings
define adc_clock 3 ' adc setting
define adc_sampleus 50 'adc setting
intcon.6=1 'gloabal interrupt
intcon.7=1 'gloabal interrupt
pie1.6=1 'a/d interrupt
pir1.6=1 'a/d interrupt

adcon0=%10001111 'set an3 on ie gpio.4
ansel=%00011000 ' sets fosc/8 and gpio.4[an3] as adc input
cmcon=7 ' turn off comparator function
trisio=%011000 'set gpio.4 as input and others as output



'define variables
rdg var word 'adc reading from an3
mpin var gpio.2 ' music pin output to speaker
led var gpio.0 'led output
tone var word ' tone value for the sound

'initial setting
led=0

'initial interrupt
on interrupt goto testpot
'program

start:
'reset led
adcin 3,rdg 'get volt reading from an3 and place adc value in rdg

goto start

'isr
disable
testpot:

if rdg =<1 then led=0
if rdg =>2 then led=1 'turn on led if adc value is equal to or larger than 1


tone=1+(rdg)*102 'converts adc value to a tone value of 1 to 120
' necessary to divide by 10 to avoid 65536 word limit
'adc of 0=tone of 1, adc of 1023 = tone of 120
sound mpin,[tone,20] 'provides a tone proportional to input volt duration = 20x12=120 min
resume
enable
end

skimask
- 15th December 2008, 05:17
clean
What's that for?


define adc_bits 10 'adc settings
define adc_clock 3 ' adc setting
define adc_sampleus 50 'adc setting

Those aren't any good as they are written (unless there's something wrong with cut-and-paste, which happens sometimes)


tone=1+(rdg)*102 'converts adc value to a tone value of 1 to 120
' necessary to divide by 10 to avoid 65536 word limit
'adc of 0=tone of 1, adc of 1023 = tone of 120
sound mpin,[tone,20] 'provides a tone proportional to input volt duration = 20x12=120 min

Check your math...
If rdg = 1 then
tone = 1 + 1 * 102 = 103
If rdg = 2 then
tone = 1 + 2 * 102 = 205
If rdg = 3 then
tone = 1 + 3 * 102 = 307
if rdg = 500 then
tone = 1 + 500 * 102 = 51001
Where is this 'divide by 10' you speak of?
You declare tone as a word variable, yet the PBP manual states that values from 0-127 produce tones, 128-255 produces white noise, and therefore only values from 0-255 are legal, implying that tone should be a byte variable...
Why not hook up an LCD to make sure your A/D is working in the first place? Then you can actually visualize your values to be output for tones.

Archangel
- 15th December 2008, 18:42
What's that for?



Probably wanted CLEAR, R vs N, Difference between BEAN and BEAR, or Been and Beer :D

mystified
- 16th December 2008, 00:51
I thank you of your time
The code
define adc_bits 10 'adc settings
define adc_clock 3 ' adc setting
define adc_sampleus 50 'adc setting

should set up the a the analog to digital converters (adc)
the output from the adc converter should be 0 to 1023
but i get just 0 to 3
I will work on the sound after I can get the 0 to 1023to work.

Pic_User
- 16th December 2008, 01:23
Hi mystified,

I thank you of your time
The code
define adc_bits 10 'adc settings
define adc_clock 3 ' adc setting
define adc_sampleus 50 'adc setting

should set up the a the analog to digital converters (adc)
the output from the adc converter should be 0 to 1023
but i get just 0 to 3
I will work on the sound after I can get the 0 to 1023to work.

Take a look at this informative thread by Melanie.


... Watch the SPELLING of your DEFINEs

Is your program not working as expected? ...

Do you have any DEFINEs in your program? ...http://www.picbasic.co.uk/forum/showthread.php?t=558

-Adam-

mystified
- 16th December 2008, 01:48
cannot find clock 3 in datasheet it now works thank you

mystified
- 16th December 2008, 02:01
OK , I put clock back to 3 but used upper chase in the define statement and that is what made it work
Thank You very much.

skimask
- 16th December 2008, 02:16
cannot find clock 3 in datasheet it now works thank you

Page 45 of the latest PBP manual

and

Page 44 of DS41190E, PIC12F675 datasheet.

See any similarity between DEFINE ADC_CLOCK 3 and bits 6 thru 4 of ANSEL?