Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Code problem: bypass not working?

Status
Not open for further replies.
I've wrote this code and everything works fine until I try to bypass the output on cfan. I want to turn it on with the pushbutton even if the ad result in cooling_fan0 should turn it off, but when I add the "btfsc cfbf goto on0" to the off0 routine the output just flickers once when I push the button and stays off. Otherwise, the rest of the program works fine. Can anyone help with some advise or show me what I'm doing wrong? Sorry for the mess!


Code:
#DEFINE		CFBF		FLAG_TEMP,4
#define		cfan		ctemp,7

start
			bsf			status,5
			movlw		d'255'			;porta inputs
			movwf		trisa			;
			movlw		b'11111111'		;
			movwf		trisb			;
			movlw		d'0'			;portc outputs
			movwf		trisc
			movlw		b'11010111'		;
			movwf		option_reg
			movlw		b'00001100'		;setup ad inputs and V ref
			movwf		adcon1			;ad control, bank 1
			bcf			status,5
			CLRF		INTCON
			clrf		porta
			clrf		portb
			clrf		portc
			CLRF		CTEMP
			CLRF		FLAG_TEMP
			clrw
MAIN
			call		COOLING_FAN0
			CALL		BUTTON_CHECK
			goto		main
;================================================
COOLING_FAN0							;
			call		ADREAD0			;
			bcf			status,c	
			SUBLW		d'127'			;
			btfss		status,c		; 
			GOTO		off0
			GOTO		on0
CF0			return

;===============================================

off0		BTFSC		CFBF
			GOTO		ON0
			bcf			cfan
			movfw		ctemp
			movwf		portc
			goto		CF0

;===============================================

on0			bsf			cfan
			movfw		ctemp
			movwf		portc
			goto		CF0			

;==================================================
COOLING_FAN_BYPASS
			CALL		DEBOUNCE
			BTFSS		CFPB
			RETURN
CFBLOOP		BTFSC		CFPB
			GOTO		CFBLOOP
			MOVFW		CTEMP
			XORLW		B'10000000'
			MOVWF		CTEMP
			MOVWF		PORTC
			MOVFW		FLAG_TEMP
			XORLW		B'0001000'
			MOVWF		FLAG_TEMP
			RETURN
;=====================================================
BUTTON_CHECK
			BTFSC		A1PB
			CALL		ACCES1_TOGGLE
			BTFSC		CFPB
			CALL		COOLING_FAN_BYPASS
			RETURN
;====================================================
 
I'm assuming its an 18F series PIC as you have no banksel statements. If true then use the LAT instruction instead of writing to the PORT.

If it's a 16F then you need to add your banksel statements or bsf STATUS, RP0 sort of things.
 
hi,
As you are controlling STATUS,5 [RP0] I have assumed a 16F877 so that I could put your code thru my PIC sim.

On assembly, MPASMWIN throws up many errors, which assembler are you using? and will you post the FULL code as an attachment?
 
Ok here goes. I put it in txt as I have trouble picking out which mplab file to attach. Sorry for the mess!
 

Attachments

  • cooling fan.txt
    15.6 KB · Views: 164
hi hotrod,
Looked thru your code, have changed it from rel to absolute, I dont want to edit my linker.

It assembles OK.

If you havn't already done so, I would suggest you download the free MPLAB IDE 7.x assembler from www.microchip.com

Also goto www.oshonsoft.com and download the trial version of the PIC simulator, I have run your program in this PIC sim it appears to work.

The PIC sim has an option to assemble using MPASMWIN, it comes with MPLAB.

Attached abs ver of your code.
 
Last edited:
problem found

I have a problem with this part not setting the cfan (portc,7). the led just flickers once when the button is pushed. Am I not doing the bypass correctly? it seems like it should work, but it doesn't.

In the Process of writing this I found the Problem!
I can't count!

Thanks for the help ericgibbs and your time

Code:
COOLING_FAN_BYPASS
			CALL		DEBOUNCE
			BTFSS		CFPB
			RETURN
CFBLOOP		BTFSC		CFPB
			GOTO		CFBLOOP
			MOVFW		CTEMP
			XORLW		B'10000000'
			MOVWF		CTEMP
			MOVWF		PORTC
			MOVFW		FLAG_TEMP
			XORLW		B'0001000' ;how many bits are in a byte?
			MOVWF		FLAG_TEMP
			RETURN
;=====================================================
 
Code:
MAIN
			call		COOLING_FAN0
			call		ledpwm
			CALL		LED_LEVEL_COOL
			CALL		BUTTON_CHECK
			goto		main

I'm not sure that you're bypassing the temperature control. If you press the button, on the next iteration of the main routine the fan will be controlled by COOLING_FAN0 again. Am I correct?
 
Yeah, but if the cfbf (cooling fan bypass flag) is set it should jump to on0 instead.
Probably not the best way of doing it but I finally got it to work, I just need to adjust the values for the actual on/off.
I'm using the cooling_fan0 ad read routine to also control a red blue and green led to show what the temperature range is and the pwm is for the ipc dimmer at night when the headlights are on.
 
The first 2 lines of off0

Code:
COOLING_FAN0							;FOR TEMP SENDING UNIT
			call		ADREAD0			;
			bcf			status,c	
			SUBLW		d'127'			;CALIBRATE FOR COOLING FAN ON/OFF
			btfss		status,c		; SEE IF CARRIED OVER 255
			GOTO		off0
			GOTO		on0
CF0			return
;===============================================

off0		BTFSC		CFBF              
			GOTO		ON0
			bcf			cfan
			movfw		ctemp
			movwf		portc
			goto		CF0

;===============================================

on0			bsf			cfan
			movfw		ctemp
			movwf		portc
			goto		CF0
 
I've got a problem with this part of the code I assume. everything works as expected on the easypic 4, everything works on the breadboard until I get to this part of the program in which it is supposed to save portc values switch off portc,6,5,4 and turn on portc,7,0,1,2,3 -blink the led on portc,0- check the AD again, if the value doesn't send it back to overtemp then return the previously saved values to portc and return. whew! But, when it returns from the overtemp routine it does not set the portc,6 value to the prevous value before the routine. I'm just wondering is there a better way to write this part of the code and why would it work on the easypic and not on the bread board in just that part of the program? Everything else seems to work fine.
Any help/pointers would be apprieciated!:)
Sorry, it also just flashes portc on and off when in this part of the program, it flashes everything 13 times and then sets the portc0,1,2 and just flashes the portc,3 3 times and then repeats. That is why I think it is in the code, but, it didn't do it on the easypic 4.?????

Code:
OVERTEMP
			MOVFW		PORTC
			MOVWF		OVERT_TEMP		;SAVE PORTC VALUES FOR LATER 
OTLOOP		MOVLW		B'10001111'		;TURN ON FAN, SHUT OFF FUEL PUMP, TURN OFF ACCESSORIES, AND TURN ON ALL LEDS
			MOVWF		CTEMP
			MOVWF		PORTC			;
			CALL		DELAY2			;	
			BCF			PORTC,0
			CALL		DELAY2
			MOVFW		CTEMP
			CALL		DELAY2
			BCF			PORTC,0
			CALL		DELAY2
			CALL		ADREAD0
			BCF			STATUS,C
			ADDWF		HOT,0
			BTFSS		STATUS,C
			GOTO		OTLOOP
			MOVFW		OVERT_TEMP		;RETURN PREVIOUS PORTC VALUES TO PORTC
			MOVWF		CTEMP
			MOVFW		CTEMP
			MOVWF		PORTC
			RETURN

ADREAD0
			movlw		b'10000001'		;osc set to 32, ch 0 and turn on ad
			movwf		adcon0			;adcon1 set to b'00001100'-left just.
convert0	call		sampledelay
										;
			bsf			adcon0,2			;start conversion
loopad0		btfsc		adcon0,2			;ad over
			goto		loopad0			;no then loop
			movfw		adresh			;yes then get ad value
			MOVWF		ADTEMP0			;TEMP FILE FOR OTHER USE
			return
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top