PDA

View Full Version : problem with a/d in pic12f675



samannikzad
- 25th June 2011, 06:53
hello, can anybody help me in this code? i just wanna read a voltage from chennel one and turn on and off the led, but i can't do it! please help,i am an iranian,and here,in iran,nobody knows about it.. just tell what the problem is,and i simulated it in proteus,but it doesnt work on proteus


trisio.2=0
trisio.0=1
ansel=%00000001
adcon0=%10000011
define osc 8
define adc_bits 10
w0 var word
start:
adcin 0,w0
pause 100
if w0=100
then
gpio.2=1
else
gpio.2=0
endif
goto start
end

Archangel
- 25th June 2011, 08:33
Hi samannikzad, Welcome to the forum. I am not able to troubleshoot your code right now, but I will offer some samples written for the 12F675 using adc. I noticed you do not list your configs. Please refer to the FAQs for how to: Presseting Config fuses in code. The code snippets are fairly well commented Example 1


' This program controls a relay by sensing an input voltage and
' flashes an led vigorisly for no apparent reason ! It serves me
' as a demo for the 12f675 microcontroller using ADC and internal OSC
@ DEVICE pic12F675, INTRC_OSC_NOCLKOUT
@ DEVICE pic12F675, WDT_ON
@ DEVICE pic12F675, PWRT_ON
@ DEVICE pic12F675, MCLR_OFF
@ DEVICE pic12F675, BOD_ON
@ DEVICE pic12F675, CPD_OFF
@ DEVICE pic12F675, PROTECT_OFF

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
DEFINE OSC 4

' * * * * * alias ports * * * * * * *
AD_0 var GPIO.0
AD_1 var GPIO.1
set VAR GPIO.2
Relay VAR GPIO.4
led2 VAR GPIO.5

' * * * * * Declare System variables * * * * *
ledlys VAR BYTE
setpt1 VAR WORD
Counts VAR BYTE
adval0 VAR WORD
adval1 VAR WORD
i VAR BYTE
Relayset VAR BIT
time VAR BYTE


CMCON = 7 'comparators off
ANSEL = %00110011 'GPIO.0,1 - A/D in ,rest digital
ADCON0.7 = 1 'right justify for 10 bit
TRISIO = %00000111 'GPIO.0 = input, rest output


' * * * * Initialise variables and ports * * * * * *

Relay = 0 ' set port GPIO.4 to low status
' * * * * * * * set variables to zero value * * * * * * * *
time = 0
ledlys = 0
Relayset = 0

Main:
GOSUB Calibrate ' establishes base line setting
If set = 1 THEN ' if port GPIO.2 = 1 then
i = 1
FOR i = 1 TO 75 ' counts 7500 ms
led2 = 1
PAUSE 50
led2 = 0
PAUSE 50
NEXT i
IF set = 1 THEN ' Save setpt if GPIO.2 is still high, sets variable
led2 = 1 ' setpt1 to adval0 value then turns on GPIO.4 (relay)
GOSUB setpt ' and sets variable relayset to value of 1, turns off
PAUSE 5000 ' LED2 and sets time variable to zero
Relay = 1
Relayset = 1
led2 = 0
time = 0
ELSE ' OTHERWISE . . . .
Relay = 0
Relayset = 0
time = 0
ENDIF
ENDIF
IF (adval0 > setpt1) AND (Relayset = 0) THEN 'turn on
Relay = 1
Relayset = 1
time = 0
ENDIF
IF (adval0 <= setpt1) AND (Relayset = 1) THEN
time = time + 1
IF time = 6 THEN
Relay = 0
Relayset = 0
ENDIF
ELSE
time = 0
ENDIF
IF ledlys < 2 THEN
ledlys = ledlys +1
ENDIF
IF ledlys => 2 THEN
IF adval1 < %1111001100 THEN ' low battery
i = 1
FOR i = 1 TO 10
led2 = 1
PAUSE 250
led2 = 0
PAUSE 250
led2 = 1
PAUSE 250
led2 = 0
PAUSE 250
NEXT i
ENDIF
led2 = 1
pause 15
led2 = 0
ledlys = 0
ENDIF
NAP 7 ' low power mode for 2.304 seconds or 8 WDT periods
NAP 7 ' Repeat 5 more times for a total sleep of 13.824 Seconds
NAP 7
NAP 7
NAP 7
NAP 7
GOTO main

setpt:
GOSUB Calibrate
setpt1 = adval0
RETURN

Calibrate:
adval0 = 0
ADCIN 0, adval0 'read ad0 mv
ADCIN 1, adval1 'read ad1 Battery
RETURN

end
One more thing DEFINES MUST BE ALL CAPITAL LETTERS no lowercase

I do not know what went wrong with these code tags !

HenrikOlsson
- 25th June 2011, 09:41
Hi,
Couple of things:
1) One of the comparator inputs is on AN0, turn the comparator off, CMCON=7
2) Don't mess with the GO/DONE bit (bit 1 in the ADCON0 register) if you're using ADCIN - it'll handle it for you.
3) The LED will only be on when w0 is 100 and 100 only, might be easy to "set" the value to 100 in a simulator but not in a real circuit. Try IF w0 > 100 instead.
4) I never never seem to remember when to use left- and when to use right justified, if you're like me try both and see what happends.

/Henrik.

samannikzad
- 28th June 2011, 15:16
http://www.mediafire.com/i/?mt35ijkbt5x8u1z

input gpio.0
output gpio.1
output gpio.2
define osc 8
define adc_clock 3
include "modedefs.bas"
ansel=%00000001
adcon0.7=1
define adc_bits 10
w0 var byte
w1 var word
w2 var word
cmcon=7
start:
adcin 0,w1
pause 100
w2=w1
for w0=65 to 90
serout gpio.1,t9600,[254,128]
serout gpio.1,t9600,[#w1]
pause 1000
serout gpio.1,t9600,[254,1]
next w0
goto start
end


thank you very very much men
i used your tricks(henrik) and used your examples(archangel).
but it isnt working yet.
please check my example out.
can you find an error?
oh,thank you veryyyyyy much!
in the software all the critical words are with capital letters,when i copied them they turned to lowercase.
oh, excuse me if my english speaking is weak!!!

samannikzad
- 28th June 2011, 15:22
input gpio.0
output gpio.1
output gpio.2
define osc 8
define adc_clock 3
include "modedefs.bas"
ansel=%00000001
adcon0.7=1
define adc_bits 10
w0 var byte
w1 var word
w2 var word
cmcon=7
start:
adcin 0,w1
pause 100
w2=w1
for w0=65 to 90
serout gpio.1,t9600,[254,128]
serout gpio.1,t9600,[#w1]
pause 1000
serout gpio.1,t9600,[254,1]
next w0
goto start
end
http://www.mediafire.com/i/?mt35ijkbt5x8u1z


hello
thank you very very much men.
i did what you said,but it didn't work.
please check this example out,it doesn't work neither.
can you find any errors in it?
thank you very very much to help me.

samannikzad
- 28th June 2011, 15:27
http://www.mediafire.com/imgbnc.php/86a5e4a19d90eacd317b9c3bdb9d85db52ac82343ed5663adb d5a358a041e9d86g.jpg

samannikzad
- 28th June 2011, 15:30
note:
the lines:
"w2 var word" and "w1=w2"
must be ommited and are not needed!

mister_e
- 28th June 2011, 15:51
Have you tried it on real hardware or you really trust a sim?

Still in your previous example you have your DEFINE in lowercase while they need to be un UPPERCASE.... and where are your configuration fuses?

Archangel
- 29th June 2011, 09:39
http://www.mediafire.com/i/?mt35ijkbt5x8u1z

i
oh,thank you veryyyyyy much!
in the software all the critical words are with capital letters,when i copied them they turned to lowercase.
oh, excuse me if my english speaking is weak!!!
From last up,
I am sure your English is truly better than my Farsi, and your English needs no apologies, it is good enough.
As FOR CHANGING TO LOWERCASE, You have been punked by MCS . MCS Automaticaly shows reserved words as UPPERCASE, but they remain lowercase in the code, this forum will show a copy paste in it's raw format, so go back and retype the define statements in ALL UPPERCASE LETTERS like so:
DEFINE OSC 8
DEFINE ADC_CLOCK 3

and report back here with the results. Mister_e asked about the config fuses
study this post and solve some of your headaches:
http://www.picbasic.co.uk/forum/showthread.php?t=543

samannikzad
- 6th July 2011, 12:57
hello,
i did what you said.
i wrote t with capslock on, and i set the fuse bits and settings on
my programmer software(practical) or in the simulating software.
this is a simple program.but it doesn't work neither
in simulatin software nor on the breadboard(practical)
see:
INPUT GPIO.0
OUTPUT GPIO.1
OUTPUT GPIO.2
DEFINE OSC 4
DEFINE ADC_CLOCK 3
INCLUDE "MODEDEFS.BAS"
ANSEL=%00000001
ADCON0.7=1
DEFINE ADC_BITS 10
W1 VAR WORD
CMCON=7
GPIO.1=1
GPIO.2=0
PAUSE 1000
GPIO.1=0
GPIO.2=1
PAUSE 1000
GPIO.1=0
GPIO.2=0
START:
ADCIN 0,W1
PAUSE 100
IF W1>500 THEN
GPIO.1=1
GPIO.2=0
PAUSE 100
ELSE
GPIO.1=0
GPIO.2=1
PAUSE 100
ENDIF
GOTO START
END



but it doesn't work yet.
there is sth that i should say.
my microcode studio's version is 2.3.0.0
and it had no support libraries for 12f675.
then i downloaded "12f675.bas" and "m12f675.inc" and copied them to the mcs
folders in my drives(near other inc and bas files).
is there any problems with this process?
and i can drive the adc module on 16f877a,but not on 12f675.
since the library files were present in the software.
thank you for helping me from the other side of the world!
thank you

mackrackit
- 6th July 2011, 22:10
then i downloaded "12f675.bas" and "m12f675.inc"

That is most likely the main problem, you do not have a legal copy. PicBasicPro is not a download.

samannikzad
- 7th July 2011, 15:14
oh thank mackrackit
and i have to find a legal,not a cracked version
of course it's too hard to find it!

mackrackit
- 7th July 2011, 15:49
This is a good place to start
http://store.melabs.com/prod/software/PBP.html

You might be able to change the chip you are using and get by with the demo version.