PDA

View Full Version : 12F675 compare voltage help.



geckogrotto
- 31st January 2007, 18:41
I am trying to figure out how to use a 12F675 to compare voltage.
This will eventually be used to meaure a current load.

I am sending the output to serial and checking it via hyperterm just so I can see whats going on.

I have the chip connected like this.
1 + 3
2 NC
3 NC
4 NC
5 NC
6 pin2 serial.
7 testing connected to ground/pos/nothing.
8 ground and serial ground.

Here is my code most copied off this forum.


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
' Internal Oscillator
' Enable watch dog timer
' Enable power up timer
' Disable MCLR pin
' Enable brown out detect

DEFINE OSCCAL_1K 1
INCLUDE "modedefs.bas"
CMCON = 7 ' turn off analog comparator
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

TRISIO = %000001 ' Set GSIO 0 & 3 TO INPUTS, others to OUTPUT
ANSEL.0 = 1 ' Set GSIO 0 to ANALOG
ADCON0.7 = 1 ' Right Justify for 10-bit
I var byte







Pause 2000


loop:
ADCIN 0, ADVAL0 ' Read A/D channel 0
SerOut GPIO.1,N2400,["Result ",#adval0,13,10]

Pause 200
GoTo loop


When I have pin 7 connected to ground.
I get all 0's
When I have pin 7 connected to +
I get Result 1020
When I disconnect 7 from + I get Result 600 and it slowly goes down to 113 to 140.

Can someone explain to me what this is telling me?

dhouston
- 31st January 2007, 18:55
The ADC is 10-bits so the highest possible reading is 1023. Your 1020 indicates 3V (if that's your Vcc).

The 600 that fades indicates the ADC input is floating.

The 0 reading indicates the 0V.

geckogrotto
- 31st January 2007, 20:58
Ok thanks that helps but what does it mean its floating?

That mean the VCC is floating or when the ADC is connected to nothing its floating?

dhouston
- 31st January 2007, 21:39
The ADC input is floating and the reading is meaningless if it has no connection.

geckogrotto
- 31st January 2007, 21:42
Thank you very much.

geckogrotto
- 1st February 2007, 16:29
I am stuck again.

I multiply by 1000 to shift where the decimal will be.
The problem is my first calculation is
1022*1000 comes out 38960 instead of 1022000 How do I keep my whole number?


I tried this as well
dummy VAR WORD
adval0=1019
dummy = adval0*1000*3
adval0 = div32 1023


Gives me 111... Should be giving me 2988 or something...

mister_e
- 1st February 2007, 17:42
Try this...


adval0 = adval0*3000
adval0 = div32 1023

geckogrotto
- 1st February 2007, 21:10
Thank you much Mister_e!

geckogrotto
- 2nd February 2007, 00:08
:( Stuck again. I'm just not understanding this math

adval0 = 2904
result = adval0 / 15100
remainder = adval0 // 15100
result = 0
remainder is 2904
How do I get the acutal number it should be 0.192xxx?


I'm looking at dig but not sure if thats the right thing to use because it will not always be 2 digits...

adval 0 = 2904*10
result = adval/151
192
then use dig to move the places... and do something like
if > 9999
if > 999
to figure out where the . should be... Is this the best way to do it?

skimask
- 2nd February 2007, 00:20
:( Stuck again. I'm just not understanding this math

adval0 = 2904
result = adval0 / 15100
remainder = adval0 // 15100
result = 0
remainder is 2904
How do I get the acutal number it should be 0.192xxx?

You aren't going to get .192xxx.
PBP only deals with integer math, as stated in the PBP manual.
You'll have to write your own routines to get anything past the decimal point.

geckogrotto
- 2nd February 2007, 00:21
Ok how do I do that. I had edited my post while you were posting.

skimask
- 2nd February 2007, 00:44
Ok how do I do that. I had edited my post while you were posting.

I see the edit, and you're on the right track, pretty much the only track.

geckogrotto
- 2nd February 2007, 00:53
Ok thanks,
I just didn't want to go down that road if there was a better way,

skimask
- 2nd February 2007, 01:39
Ok thanks,
I just didn't want to go down that road if there was a better way,

Well, you could use the floating point math routines on melabs website, microchip's site, etc.etc. or write your own.

The trick to doing it the way you've got set up above is to make sure that no individual variables overflow or underflow, no matter what values end up going into them. It just takes a bit of thinking thru it all...

geckogrotto
- 2nd February 2007, 01:49
Well, you could use the floating point math routines on melabs website, microchip's site, etc.etc. or write your own.

The trick to doing it the way you've got set up above is to make sure that no individual variables overflow or underflow, no matter what values end up going into them. It just takes a bit of thinking thru it all...



I would like to not use those as I want to learn how to do this, but I sure could use some help i'm a bit lost here. Maybe just working on it too long.

The number I expect is 1-3 digits. Example

If the number is 123 it would be 1.23
If its 12 .12
If its just 1 then would be .01

I just can't seem to wrap my mind around how to actually accomplish this.

skimask
- 2nd February 2007, 02:17
I would like to not use those as I want to learn how to do this, but I sure could use some help i'm a bit lost here. Maybe just working on it too long.

The number I expect is 1-3 digits. Example

If the number is 123 it would be 1.23
If its 12 .12
If its just 1 then would be .01

I just can't seem to wrap my mind around how to actually accomplish this.


In post #9 and post #15, you've practically got the answer for yourself.
Cook on it for a bit, roll it around in your brain, see what you come up with.

mister_e
- 2nd February 2007, 02:44
You should have a look at DEC and DIG modifier and keep your maths as is.

geckogrotto
- 2nd February 2007, 03:03
Ok guys thanks for the input I think im going to call it a night and take a fresh look at it again tomorrow.

Hopefully it will all fall into place :)

Bruce
- 2nd February 2007, 13:04
Try this;


X VAR WORD
Y VAR WORD

AD_10BIT:
FOR X = 0 TO 1023 ' Simulate 0 TO 5V
Y = X */ 1251 ' For 10-bit A/D (5V/1023)*256 = 1.251
SEROUT GPIO.1,N2400, ["X = ",DEC X," : = ",DEC Y DIG 3,".",DEC3 Y,"V",13,10]
NEXT

geckogrotto
- 2nd February 2007, 16:46
This is what I came up with.


adval0 = 4828
adval0 =adval0/151
'should give me 31 in this example.

if adval0 > 99 then
'if adval was say 131 result would be 1. and remainder should calculate to 3*10+1 being 31. total of 1.31

result= adval0 dig 2
remainder= adval0 dig 1*10
remainder= remainder + adval0 dig 0
else
if adval0 < 99 then
result= 0
remainder= adval0
endif
endif



Look about right?