# Need help, am noob

Status
Not open for further replies.

#### Kingdom Man

##### New Member
here is some code that Burt has been helping me with, but I have a problem. I want to learn how to set my output high at 1Volt and have my output stay high until it reaches .8Volts. as of now, with this code I can only set my output high or low at one given voltage, so it will trigger high when it hits 1v but the second it goes below 1v it triggers low again.

PHP:
#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

CBLOCK 		0x020
endc
#define _.8VOLT 0x28
#define _1VOLT 0x33
#define _2VOLT 0x66
#define _2_5VOLT 0x7F
#define _3VOLT 0x99
#define _4VOLT 0xCC

ORG			0x000		; processor reset vector
goto		init		; go to beginning of program

ORG			0x004	    ; interrupt vector location

init:
bsf       STATUS,RP0     ; select Register Page 1
movlw     0xFF
movwf     TRISA          ; Make PortA all input
clrf      TRISC          ; Make PortC all output
movlw     0x10           ; A2D Clock Fosc/8
bcf       STATUS,RP0     ; back to Register Page 0

bcf       STATUS,RP0     ; address Register Page 2
bsf       STATUS,RP1
movlw     0xFF           ; we want all Port A pins Analog
movwf     ANSEL
bcf       STATUS,RP0     ; address Register Page 0
bcf       STATUS,RP1
movlw     0x01
movwf     ADCON0         ; configure A2D for Channel 0
MainLoop:
call	adcdelay		  ;delay to charge cap
btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
goto      $-1 movf ADRESH,w ; Copy the display to the LEDs movlw _.8VOLT subwf ADRESH, w BNC LessThan.8VOLT ; Branch if less than 1V movlw _1VOLT subwf ADRESH, w BNC LessThan1VOLT ; Branch if Between 1V and 2V movlw _2VOLT subwf ADRESH, w BNC LessThan2VOLT ; Branch if Between 2V and 2.5V movlw _2_5VOLT subwf ADRESH, w BNC LessThan2_5VOLT ; Branch if Between 2.5V and 3V movlw _3VOLT subwf ADRESH, w BNC LessThan3VOLT ; Branch if Between 3V and 4V movlw _4VOLT subwf ADRESH, w BNC LessThan4VOLT ; Branch if Between 3V and 4V goto MainLoop LessThan.8VOLT: bcf PORTC,0 return LessThan1VOLT: bsf PORTC,0 return LessThan2VOLT: bcf PORTC,0 ; lights led thats in volt range return LessThan2_5VOLT: bsf PORTC,0 return LessThan3VOLT: bcf PORTC,0 return LessThan4VOLT: bsf PORTC,0 return adcdelay: nop ;1 cycle return ;4 cycles (including call) end #### be80be ##### Well-Known Member All you have to do is check for .8volts if no carry check fan for on or turn on fan if carry we are less then .8volts so lets turn the fan off Last edited: #### blueroomelectronics ##### Well-Known Member Why start a new thread on the same subject? #### be80be ##### Well-Known Member Here try this out see what it will do for you Code: #include <p16F690.inc> __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF) CBLOCK 0x020 endc #define _.8VOLT 0x28 ORG 0x000 ; processor reset vector goto init ; go to beginning of program ORG 0x004 ; interrupt vector location init: bsf STATUS,RP0 ; select Register Page 1 movlw 0xFF movwf TRISA ; Make PortA all input clrf TRISC ; Make PortC all output movlw 0x10 ; A2D Clock Fosc/8 movwf ADCON1 bcf STATUS,RP0 ; back to Register Page 0 bcf STATUS,RP0 ; address Register Page 2 bsf STATUS,RP1 movlw 0xFF ; we want all Port A pins Analog movwf ANSEL bcf STATUS,RP0 ; address Register Page 0 bcf STATUS,RP1 clrf ADCON0 movlw 0x01 movwf ADCON0 ; configure A2D for Channel 0 MainLoop: call adcdelay ;delay to charge cap bsf ADCON0,GO ; start conversion btfss ADCON0,GO ; this bit will change to zero when the conversion is complete goto$-1

movf     ADRESH,w       ; Copy the display to the LEDs
movlw     _.8VOLT
BNC     LessThan.8VOLT       ; Branch if less than 1V
BC     killfan

killfan:
bcf     PORTC,0          ;turn off fan
return

LessThan.8VOLT:
BTFSS   PORTC,0		 ;checks fan for on if on we skip next
bsf     PORTC,0		 ; turns on the fan
return

nop                      ;1 cycle

return                      ;4 cycles (including call)

end

Last edited:

#### Kingdom Man

##### New Member
Thanks Burt, I will try!

#### gaspode42

##### Member
Burt

Don't you need to close the loop after "BDC Killfan" - or the code will run on and kill the fan anyway?

Last edited:

Last edited:

#### Kingdom Man

##### New Member
Burt, I tried this code, and tried modifying it as well. It only sets my output pin high on port c when the input voltage is below .8v otherwise the output remains low above .8v. This is all it seems to do for me.
I need to set output high when input reaches 1v and Higher, but if Voltage goes lower than 1v I still need it to stay high until voltage goes below .8V. I hope I am making sense.

#### gaspode42

##### Member
Burt

I apologize for confusing the issue - I will shut-up !

#### be80be

##### Well-Known Member
Well I no this works I tried it
Code:
#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

CBLOCK         0x020
endc
#define _.8VOLT 0x28

ORG            0x000        ; processor reset vector
goto        init        ; go to beginning of program

ORG            0x004        ; interrupt vector location

init:
bsf       STATUS,RP0     ; select Register Page 1
movlw     0xFF
movwf     TRISA          ; Make PortA all input
clrf      TRISC          ; Make PortC all output
movlw     0x10           ; A2D Clock Fosc/8
bcf       STATUS,RP0     ; back to Register Page 0

bcf       STATUS,RP0     ; address Register Page 2
bsf       STATUS,RP1
movlw     0xFF           ; we want all Port A pins Analog
movwf     ANSEL
bcf       STATUS,RP0     ; address Register Page 0
bcf       STATUS,RP1
movlw     0x01
movwf     ADCON0         ; configure A2D for Channel 0
MainLoop:
call    adcdelay          ;delay to charge cap
btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
goto      \$-1

movf     ADRESH,w       ; Copy the display to the LEDs
movlw     _.8VOLT
BNC     LessThan.8VOLT       ; Branch if less than 1V
BC     killfan

killfan:
bsf     PORTC,0          ;turn off fan
return

LessThan.8VOLT:
BTFSS   PORTC,0		 ;checks fan for on if on we skip next
bcf     PORTC,0		 ; turns on the fan
return

nop                      ;1 cycle

return                      ;4 cycles (including call)

end

#### be80be

##### Well-Known Member
Burt

I apologize for confusing the issue - I will shut-up !
That's all right i stay confused I meant BC not BDC and you made me look agin I fixed it it works I think like he want fan on a .8volts and above
off if it is below it worked that way now

#### Kingdom Man

##### New Member
Thanks Burt, It does work- It does turn on fan at .8V and turn off at .8v. I think I need to explain my issue more, I hope this will make it more clear.
I will be monitoring a thermistor, so as my temp goes up, so my Voltage also goes up. So if 1Volt represents say 25 Degrees C and .8V Represents 20 Degrees C, then what I want to do is monitor my thermistor on the input, and not turn the fan on until it goes above 25 Degrees(1V), then once it has turned on, the fan will start removing heat from the liquid it is cooling. The fan must stay on until the coolant it is cooling has dropped below the temp of 20 Degrees (.8V). Then when the fan has turned off the coolant temp will start to rise again and it will repeat the cycle.

#### Kingdom Man

##### New Member
Yes I believe this is what I need to do, as it gives an example of Hysteresis in the use of a thermostat and a furnace.

#### blueroomelectronics

##### Well-Known Member
A thermostat will need a slightly more complex algorithm, something to replicate the function of a Heat Anticipator.

Also most furnaces don't like to be cycled too fast, 6 times an hour is common.

Last edited:

#### Kingdom Man

##### New Member
ok, but I am looking to learn how to make code that will act like this, can you help?

Status
Not open for further replies.

Replies
22
Views
3K
Replies
6
Views
3K
Replies
4
Views
2K
Replies
6
Views
1K
Replies
61
Views
9K