PDA

View Full Version : Simple Reset code needed for pic12f675 Please Help!



JeromyJones
- 28th November 2013, 04:33
I have a code I found on the internet that is a simple hot/cold detector with 2 led's. Well the problem is if a new ambient temperature is introduced it will just stay on the color of the ambient temperature unless I turn the unit off and back on again to reset the unit. Well I have found an electrical work-around using a 555 timer to reset the circuit every 30 seconds which is what I desire. However I am getting tired of building the 555's every time I build them so I know someone out there can write a simple code to do this for me automatically through the pic chip12... I have contacted the original owner of the code and he said he doesn't have the time to even look at it, so Im hoping someone here can help me. It would be greatly appreciated! Basically I need the chip to turn off then back on every 30 seconds as if I would do it with the on/off switch manually.
Here is a sample of the code im using.


@ Device PIC12F675,PROTECT_OFF,INTRC_OSC_NOCLKOUT,MCLR_OFF
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50


' Requires Pic Basic Pro to compile for the Microchip 12F675.




CMCON=7
GPIO=%00000000
TRISIO=%010110
ANSEL=%00111110
ADCON0=%11001100


t VAR WORD
c VAR WORD
cl VAR WORD
m VAR BYTE
hh VAR BYTE
ch VAR BYTE
u VAR BYTE
d VAR BYTE


u=0
d=0
hh=2
ch=1
cl=0


pause 250
ADCIN 3, t
ADCIN 2, c
if(t>c) then
cl=t-c
m=1
endif
if(t<c) then
cl=c-t
m=0
endif


loop:


ADCIN 3, t
ADCIN 2, c
if m=1 then
t=t-cl
else
t=t+cl
endif


if u>10 then
GPIO.0=0
GPIO.5=1
endif


if d>10 then
GPIO.5=0
GPIO.0=1
endif


if t>(c+ch) then
u=u+1
d=0
goto loop
else
if t<(c-hh) then
d=d+1
u=0
goto loop
endif
endif


u=0
d=0
GPIO.0=0
GPIO.5=0
nap 2
GoTo loop

Archangel
- 28th November 2013, 08:50
Hi Jeromy,
I do not think this code as it is will do what you want because it appears you can only use 1 AD channel at a time. I am posting a revised code which is untested but will give you some idea how to implement 2 channels of AD converter.


#CONFIG
__CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_ON & _WDT_ON

#ENDCONFIG
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50


' Requires Pic Basic Pro to compile for the Microchip 12F675.




CMCON=7 ; analog comparators off
GPIO=%00000000
TRISIO=%010110 ;PORTS 1,2 & 4 AS INPUTS
; _________________________________
; | ____________________________ A/D Conversion Clock Select bits UPPER NIB
; | | |
ANSEL=%00111110 ;ANALOG SELECT BITS LOWER NIB

ADCON0=%11001111 ; AN3 ENABLED

;================================================= =================
;create variables *************************************************
t VAR WORD ; AD CONVERTER
c VAR WORD ; AD CONVERTER
cl VAR WORD
m VAR BYTE
hh VAR BYTE
ch VAR BYTE
u VAR BYTE
d VAR BYTE

;================================================= ================
; initialize values into variables *******************************
u = 0
d = 0
hh = 2
ch = 1
cl = 0
;================================================= ================
; start up ************************************************** *****

pause 250
ADCON0=%11001111 ; AN3 ENABLED
ADCIN 3, t ;CHANNEL 3 is GPIO.4 PIN 3 OF PART
PAUSE 250
ADCON0=%11001011 ; AN2 ENABLED
ADCIN 2, c ;channel 2 is GPIO.2 PIN 5 OF PART
PAUSE 250
if(t > c) then
cl = (t-c)
m = 1
endif
if(t < c) then
cl = (c-t)
m = 0
endif

;================================================= ================
; loop ************************************************** *********
mainloop:


ADCON0=%11001111 ; AN3 ENABLED
ADCIN 3, t ;CHANNEL 3 is GPIO.4 PIN 3 OF PART
PAUSE 250
ADCON0=%11001011 ; AN2 ENABLED
ADCIN 2, c ;channel 2 is GPIO.2 PIN 5 OF PART
PAUSE 250
if m = 1 then
t = (t - cl)
else
t = (t + cl)
endif


if u > 10 then
GPIO.0=0
GPIO.5=1
endif


if d > 10 then
GPIO.5 = 0
GPIO.0 = 1
endif


if t >(c + ch) then
u = (u + 1)
d = 0
goto mainloop
else
if t <(c - hh) then
d = (d + 1)
u = 0
goto mainloop
endif
endif


u=0
d=0
GPIO.0=0
GPIO.5=0
nap 2
GoTo mainloop


end


Notice the config statements are for PBP 3. and MPASM
Your configs were for an earlier version using PBP assembler. If you use PBP add a WDT ON statement to your config.

Oh and Jeromy, please confine your problems of a project within a single thread unless the problem is different, which is to say do not open multiple threads for a single problem.

JeromyJones
- 28th November 2013, 16:50
Thank you, I really appreciate the help!

Archangel
- 28th November 2013, 19:04
You are welcome, I hope it is helpful.
I changed the word loop to mainloop as loop is a reserved word in PBP 3.xx
notice ADCON0
bits 2 & 3 select the channel, bit 0 turns the AD on off and bit 1 is the GO/Done bit set to go, 0 means done which means you can check it to see if done.

Art
- 24th December 2013, 11:39
If you really wanted to go the long way round like that,
you could use a variable to count the number of times your main loop is run,
then when it reaches a certain value, simply use a spare pin to pull the MCLR.
But it's better to fix it.