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.

basic pic 12f675 project help

Status
Not open for further replies.

pigman

New Member
Hey, I'm doing a little project to run a pump based on a couple of float switch inputs.. It is pretty basic but so is my PIC programming ability thus far... Here is my code so far, you can see where I stopped as I am having trouble figuring out how to use btfss or btfsc to read the digital inputs which will be either high or low, IE float switch up or down. The main goal is to have a float at the top of the tank and another half way down, the pump will turn on when both floats are HIGH however won't turn off until both floats are LOW so as to avoid the pump coming on and off all the time.

Code:
list P=12F675
	#include P12F675.inc

;Program Configuration Register 
		__CONFIG    _CPD_OFF & _CP_OFF & _BODEN_OFF& _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT	
; 	  GPIO Pins = xx543210
; Definitions
#define GPTRIS	B'00000110'   ; Set GP1 and GP2 to input all other as output

#define IN1		B'00000010'		;Digital Input GP1
#define IN2		B'00000100'		;Digital Input GP2
#define OUT1	B'00000001'		;Digital Output GP0

;-----------------------------------------------------------
; Program Memory
;-----------------------------------------------------------

		ORG		0x000		; RESET Vector

		nop
;		goto	INITALIZE

INITALIZE
		bsf		STATUS, RP0
		
		call	0x3FF
		movwf	OSCCAL

; GPIO Ports, Store GPTRIS Value into TRISIO direction register
		movlw	GPTRIS
		movwf	TRISIO		; Write to TRISIO
; Interupt on Change Register IOCB
;	GPIO Pins =   xx543210
		movlw	B'00000000'	; IOCB Disabled on all ports
		movwf	IOCB
; Peripheral Interrupt Enable Register PIE1
		clrf	PIE1		; Peripheral Interupts Disabled
;---------------------------------------- 
; Analog-to-Digital Converter (A/D) Module (Section 7.0) (PIC12F675 Only) 
; 
; The analog-to-digital converter (A/D) allows conversion of an analog 
; input signal to a 10-bit binary representation of that signal. The 
; PIC12F675 has four analog inputs multiplexed into one sample and hold 
; circuit. There are two registers to control the functions of the A/D 
; module: 
;   A/D Control Register (ADCON0) 
;   Analog Select Register (ANSEL) 
; 
; Note: When using GPIO pins as analog inputs, ensure the TRISIO register 
;       bits are set (= 1) for input. 
;		Ports=    xxx4x210
;        movlw   b'00000000'			; Disable Analog input
;        movwf   ANSEL
CLRF ANSEL		; disable analog
CLRF ADCON0		; disable ADC
;---------------------------------------- 
; Comparator Module (Section 6.0) 
; 
; The PIC12F629/675 devices have one analog comparator. The inputs to 
; the comparator are multiplexed with the GP0 and GP1 pins. There is  
; an on-chip Comparator Voltage Reference that can also be applied to 
; an input of the comparator. In addition, GP2 can be configured as 
; the comparator output. The Comparator Control Register (CMCON) 
; contains bits to control the comparator. The Voltage Reference 
; Control Register (VRCON) controls the voltage reference module. 
 
        ; CM2:CM0 = 111 - Comparator Off (lowest power) OFF
        movlw   b'00000111'			; Comparator inputs grounded 
        movwf   CMCON 

;---------------------------------------- 
; Start Main Program
;---------------------------------------- 

CHECKLEVEL
		movlw	b'00000000' ;ensure outputs low
		movwf	GPIO
;		movf	IN1,w		;place IN1 (GP1) status into w
;		sublw
;		btfsc	f,b

Hope someone can help :)
Cheers
 
Last edited:
Code:
             clrf     GPIO         ;make all outputs LOW
             btfsc    GPIO,1      ;see if IN1 is HIGH
             goto     It is HIGH
             goto     It is LOW
 
Last edited:
Colin,

Thanks for that mate, could I just get some clarification? This would be checking GP1 ? Would the below check GP2?


Cheers
Josh

Code:
btfsc GPIO,2
goto It is HIGH
goto It is LOW
 
Last edited:
btfsc GPIO,2
goto It is HIGH
goto It is LOW

The above checks GPIO2 You don't need to clear GPIO at all - at any time - for the above test(s) to work. I just put it in to show that your two instructions could be replaced by a single instruction.
 
Last edited:
Colin, Thanks for that! yes I realised that the clrf GPIO was just clearing what I was using two command to write after I'd posted. :| bit of a dull moment.

Thanks for your help guys, I think I've managed to get it sorted. If your keen here is the code I have now. Just the juicey bit. Plus the schematic

Code:
CHECKLEVEL
CHECKTOPFLOAT
		clrf	GPIO			; ensure outputs low
		btfsc	GPIO,1			; check IN1 is high or low IF high skip next step. SWITCH ON = LOW
		goto	RUNPUMPCHECK
		goto	CHECKTOPFLOAT	; repeat check until top float high

RUNPUMPCHECK					; Run pump for until 
		clrf	GPIO			; ensure outputs low
		call	Delay2s			; run delay for 2 seconds to ensure top float wasn't a false full indication
		btfsc	GPIO,1			; check IN1 is high or low IF high skip next step. SWITCH ON = LOW
		goto	RUNPUMPCHECK2
		goto	CHECKLEVEL		; return to the beginning. false alarm
PUMPCHECK2
		btfsc	GPIO,2			; check IN2 is high or low IF high skip next step. SWITCH ON = LOW
		goto	RUNPUMP			; pump run
		goto	FLOATERROR		; pump run error with middle float

RUNPUMP
		movlw	PUMP			; set GP0 high PUMP = B'00000001'
		movwf	GPIO			; write GP0 
		btfsc	GPIO,2			; check middle float (IN2) is high or low IF high skip next step. SWITCH ON = LOW
		goto	RUNPUMP			; repeat RUNPUMP until middle switch goes LOW
		goto	CHECKLEVEL		; turn output(pump) off and begin checking process again 

FLOATERROR
		clrf	GPIO			; ensure outputs low
		call	Delay2s			; Delay 2 seconds
		movlw	LED1			; 
		movwf	GPIO			;
		call	Delay2s			; Delay 2 seconds
		goto	FLOATERROR		; Repeat



; SUBROUTINES

; Delay 2 seconds
Delay2s 
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay2s_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay2s_0

			;4 cycles (including call)
	return



END
 

Attachments

  • grey-water-pump-control.pdf
    12.6 KB · Views: 313
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top