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
Code:
' 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 !
Bookmarks