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.

Problem with reading Inputs for PIC12F629

Status
Not open for further replies.

npatel

New Member
Posted: Fri Mar 19, 2004 9:55 am Post subject: Problems with reading Input Pin

--------------------------------------------------------------------------------

Hi,

I have been up all night trying to figure out how to read an input, I have done everything that I think should be done, but nothing happens.

I have attached the code for anyone to review. I am using the PIC12F629 chip, and for some reason no matter what I do the input pins are never read.

Code:
STATUS 		Equ		0x03		;Status Register position in Memory Map
GPIO  		Equ		0x05		;General Purpose Register position in Memory Map
TRISIO		Equ		0x85		;Tris Register position in Memeory Map
WPU			Equ		0x95

			Bsf		STATUS, 5	;Sets Status register position 5 to 1 - Switch to bank 1
			Movlw	b'011110'	; Make GP0 an output
			Movwf	TRISIO		; moves the value above to the following location
			Bcf 	STATUS, 5	;Clears Status register position 5 to 0 - Switch to bank 0

Loop		Btfss 	GPIO, 1
			Goto 	TurnOff

			Movlw	b'111110'
			Movwf	GPIO
			Goto 	Loop

TurnOff		Movlw	b'111111'
			Movwf	GPIO
			Goto	Loop

			End

Any help would be greatly appreciated
 
The 12F629 has an analog comparator that by default uses GP0 and GP1 as analog inputs to the internal comparator only, to use these pins as digital I/O you should disable the comparator by setting CMCON register's CM2:CM0 bits to 111. (you might want to check this in the 6. Section of the datasheet.)
 
hi

i am very noob in PIC's so i started with the simple PIC12f675 and i am using it in the circuit of inverter for 11 watts CFL. Actually i need a very simple program and get it worked in MPLAB. It uses the internal comparator in it. Actually the logic is like that whenever the input DC voltage is 0 (ground level) at GP0, then at GP1 and GP2 the output is on for 6us and off for 12us continuously such that ON and OFF times are alternate at the two ports. And if the input DC voltage is above 0 (ground level) then at GP1 and GP2 the output is on for 10us and off for 20us continuously such that ON and OFF times are alternate at the two ports.

Actually can someone also provide me codes of how to initiate the program ?


I would have made this program myself but i have very little time left to work on the software part so i thought if any of u pro's might help.

My mail ID is imran.a.khan@ril.com

Please HELP !!
 
Last edited:
You could do it like this you just have to make the delay for the time you want but maybe
this will help you
Code:
	list      p=12f675           ; list directive to define processor
	#include <p12f675.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.


   		cblock 0x20
		d1
		endc
;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    init              ; go to beginning of program
	

		ORG     0x004             ; interrupt vector location
init:
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
		CLRF 	GPIO              ;Init GPIO
		MOVLW 	07h               ;Set GP<2:0> to
		MOVWF 	CMCON             ;digital IO
		BSF 	STATUS,RP0        ;Bank 1
		CLRF 	ANSEL             ;Digital I/O
		MOVLW 	b'00000001'       ;Sets inputs and outputs
		MOVWF 	TRISIO            ;and set outputs 
		BCF 	STATUS,RP0 		  ;Bank 0
		clrf  	GPIO	
                goto main	             
main:
		btfss 	GPIO,0
		call	lowtest
		btfsc   GPIO,0
		call 	hightest
		goto	main
lowtest:
		bsf     GPIO,1
		call	Delay
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay2
                bcf	GPIO,2
		return
hightest:
		bsf     GPIO,1
		call	Delay2
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay2
		call    Delay2
                bcf	GPIO,2
		return
Delay2:
		call	Delay
		call	Delay
	    return
Delay:
			;196 cycles
	movlw	0x41
	movwf	d1
Delay_0:
	decfsz	d1, f
	goto	Delay_0

			;4 cycles (including call)
	return
		END                       ; directive 'end of program'
 
Last edited:
I pulled out a 12f675 and tried it you get a fast toggle if it is low on GPIO,0 and a slow toggle if it is high on GPIO,0 .
You'll want to change the delays to suite your timing here i did it with longer delays so you can see it work
Code:
list      p=12f675           ; list directive to define processor
	#include <p12f675.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.


   		cblock 0x20
		d1
		endc
;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    init              ; go to beginning of program
	

		ORG     0x004             ; interrupt vector location
init:
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
		CLRF 	GPIO              ;Init GPIO
		MOVLW 	07h               ;Set GP<2:0> to
		MOVWF 	CMCON             ;digital IO
		BSF 	STATUS,RP0        ;Bank 1
		CLRF 	ANSEL             ;Digital I/O
		MOVLW 	b'00000001'       ;Sets inputs and outputs
		MOVWF 	TRISIO            ;and set outputs 
		BCF 	STATUS,RP0 		  ;Bank 0
		clrf  	GPIO	
                goto main	             

main:
		btfss 	GPIO,0
		call	lowtest
		btfsc   GPIO,0
		call 	hightest
		goto	main
lowtest:
		bsf     GPIO,1
		call	Delay
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay2
	        bcf	GPIO,2	
		return
hightest:
		bsf     GPIO,1
		call	Delay2
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay2
		call	Delay2
		bcf	GPIO,2
		return
Delay
			;499994 cycles
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return
Delay2:
			;999990 cycles
	movlw	0x07
	movwf	d01
	movlw	0x2F
	movwf	d02
	movlw	0x03
	movwf	d03
Delay_01
	decfsz	d01, f
	goto	$+2
	decfsz	d02, f
	goto	$+2
	decfsz	d03, f
	goto	Delay_01

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return


		END                       ; directive 'end of program'
 
Last edited:
This has 6us ans 12us delays for low and 10us and 20 for high
Code:
   list      p=12f675           ; list directive to define processor
	#include <p12f675.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.


   		cblock 0x20
		d1
		endc
;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    init              ; go to beginning of program
	

		ORG     0x004             ; interrupt vector location
init:
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
		CLRF 	GPIO              ;Init GPIO
		MOVLW 	07h               ;Set GP<2:0> to
		MOVWF 	CMCON             ;digital IO
		BSF 	STATUS,RP0        ;Bank 1
		CLRF 	ANSEL             ;Digital I/O
		MOVLW 	b'00000001'       ;Sets inputs and outputs
		MOVWF 	TRISIO            ;and set outputs 
		BCF 	STATUS,RP0 		  ;Bank 0
		clrf  	GPIO	
                goto main	             

main:
		btfss 	GPIO,0
		call	lowtest
		btfsc   GPIO,0
		call 	hightest
		goto	main
lowtest:
		bsf     GPIO,1
		call	Delay6
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay12
	    bcf	GPIO,2	
		return
hightest:
		bsf     GPIO,1
		call	Delay10
		bcf	GPIO,1
		bsf	GPIO,2
		call 	Delay20	
		bcf	GPIO,2
		return
Delay6:
			;2 cycles 6uS delay
	goto	$+1

			;4 cycles (including call)
	return
Delay12:
			;8 cycles
	goto	$+1
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return
Delay10:
			;6 cycles 10uS delay
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return
Delay20:
			;16 cycles
	movlw	0x05
	movwf	d1
Delay_0:
	decfsz	d1, f
	goto	Delay_0

			;4 cycles (including call)
	return


		END                       ; directive 'end of program'
 
Pic12f675

This is the prog that i am working on rite nw


list p=12f675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions

errorlevel -302 ; suppress message 302 from list file

__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.


cblock 0x20
d1
endc
;**********************************************************************
ORG 0x000 ; processor reset vector
goto init ; go to beginning of program


ORG 0x004 ; interrupt vector location
init:
call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
bcf STATUS,RP0 ; set file register bank to 0
CLRF GPIO ;Init GPIO
MOVLW 07h ;Set GP<2:0> to
MOVWF CMCON ;digital IO
BSF STATUS,RP0 ;Bank 1
CLRF ANSEL ;Digital I/O
MOVLW b'00000001' ;Sets inputs and outputs
MOVWF TRISIO ;and set outputs
BCF STATUS,RP0 ;Bank 0
clrf GPIO
goto main
main:
btfss GPIO,0
call 33khz
btfsc GPIO,0
call 55khz
goto main
33khz:
bsf GPIO,1
call Delay10
bcf GPIO,1
bsf GPIO,2
call Delay20
bcf GPIO,2
return
55khz:
bsf GPIO,1
call Delay6
bcf GPIO,1
bsf GPIO,2
call Delay12
bcf GPIO,2
return
Delay6:
;2 cycles 6uS delay
goto $+1

;4 cycles (including call)
return
Delay12:
;8 cycles
goto $+1
goto $+1
goto $+1
goto $+1

;4 cycles (including call)
return
Delay10:
;6 cycles 10uS delay
goto $+1
goto $+1
goto $+1

;4 cycles (including call)
return
Delay20:
;16 cycles
movlw 0x05
movwf d1
Delay:
decfsz d1, f
goto Delay

;4 cycles (including call)
return


END ; directive 'end of program'

BUT this works perfectly well when the input on the GPIO,0 is high or it is low

WHAT i actually need is that the input when it is more than 0 volts(NOT HIGH), which could be 1 volt or 2 volt which in digital language is recorded as low, it should toggle the pins GPIO, 1 and GPIO, 2 with 10us for high and 20us as low
AND when the input is 0 volts it should toggle the pins GPIO, 1 and GPIO, 2 with 6us for high and 12us as low.

So the internal comparator use is required in this application where the comparator has to compare the GPIO, 0 input with 0 volts which i am actually not sure how to use.
SO PLEASE HELP !
 
12f675

Can someone tell me if this prog. should work.
I have tried to write the prog using ADC

list p=12f675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions

errorlevel -302 ; suppress message 302 from list file

__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.



;***** VARIABLE DEFINITIONS (examples)

; example of using Shared Uninitialized Data Section
INT_VAR UDATA_SHR 0x20
w_temp RES 1 ; variable used for context saving
status_temp RES 1 ; variable used for context saving


;************************************************* *********************
RESET_VECTOR CODE 0x000 ; processor reset vector
goto main ; go to beginning of program


INT_VECTOR CODE 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt


; these first 4 instructions are not required if the internal oscillator is not used

call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
; set file register bank to 0

; ======================= DELAY =================================
DELAY1
CLRF TMR0 ; START TMR0
LOOPBB MOVF TMR0,W ; READ TMR0 INTO W
SUBLW .200 ; TIME -W
BTFSS STATUS,Z ; CHECK TIME W = 0
GOTO LOOPBB
RETLW 0 ; RETURN AFTER TMR0 = 100

main
bsf STATUS,RP0 ; select bank 0
movlw .9 ;B'00001001' ; SET BIT 0 as input ;
movwf TRISIO


movlw B'00000111'
movwf OPTION_REG


movlw B'00010001' ;set bit 0 as analogue input
movwf ANSEL

bcf STATUS,RP0
movlw B'00000111'
movwf CMCON

bcf STATUS,RP0
movlw B'00000001' ; left justified (bit7=0) /255
movwf ADCON0
clrf GPIO

;======================== START OF PROGRAM ============================
BEGIN

bsf ADCON0,GO
WAIT btfsc ADCON0,GO
goto WAIT
bsf STATUS,RP0
movf ADRESL,W
sublw .000 ; 8 bit ADC convertion = 255/5 x 0v = 000
; == 10 BIT ADC CONVERTION _ 0volts
btfsc STATUS,C ; 1,023 / 5volt vdd = 204.6 x voltage in
goto 50khz ; e.g_ 0volts x 204.6 = 000
goto 33khz

33khz:
bsf GPIO,1
call Delay10
bcf GPIO,1
bsf GPIO,2
call Delay20
bcf GPIO,2
return
50khz:
bsf GPIO,1
call Delay6
bcf GPIO,1
bsf GPIO,2
call Delay12
bcf GPIO,2
return
Delay6:
;2 cycles 6uS delay
goto $+1

;4 cycles (including call)
return
Delay12:
;8 cycles
goto $+1
goto $+1
goto $+1
goto $+1

;4 cycles (including call)
return
Delay10:
;6 cycles 10uS delay
goto $+1
goto $+1
goto $+1

;4 cycles (including call)
return
Delay20:
;16 cycles
movlw 0x05
movwf d1
Delay:
decfsz d1, f
goto Delay

;4 cycles (including call)
return


END ; directive 'end of program'


Suggestions are greatly welcome !
 
Last edited:
I don't think you have read the datasheet It takes 3 pins just to use the COMPARATOR
gp0 gp1 and gp2 And with ADC I don't see you getting a 6 uS low switch.I would use a opamp to bring it up to a logical high and the last code I posted should work.
 
12f675

In my application i cant use the opamp as the input voltage is not constant, it varies from 1 v to 4 v so i am left with no other option than to use ADC. Can u tell me how this can be done with ADC.
When the input is 0 v the output at GP1 and 2 should produce an alternate on/off pulse whose ON time is 5us and OFF time of 15us.
Otherwise the output at GP1 and 2 should produce an alternate on/off pulse whose ON time is 10us and OFF time of 20us.

Moreover since the duty cycle is not 50%
so when the GP1 is ON for the first time for 5us and after that it goes off for 15us, at that moment GP2 should not go ON immediately but after 5us it should go ON for
5us and OFF while the GP1 completes its cycle such that the pulses remain synchroized with each other.

And same thing with 10us and 20us

I am attaching an image from the oscilloscope of the pulse. Please see to it

Some1 please provide the schematic to this
 

Attachments

  • DSC00843.JPG
    DSC00843.JPG
    3.9 MB · Views: 300
Last edited:
This pulse generation part i have finished. Please tell me if it is correct

33khz:
bsf GPIO,1
call Delay10
bcf GPIO,1
call Delay5
bsf GPIO,2
call Delay10
bcf GPIO,2
call Delay5

goto 33khz
55khz:

bsf GPIO,1
call Delay5
bcf GPIO,1
cal Delay5
bsf GPIO,2
call Delay5
bcf GPIO,2
cal Delay5
return
Delay5:
;1 cycles
goto $+0.5

;4 cycles (including call)
return

Delay10:
;6 cycles 10uS delay
goto $+1
goto $+1
goto $+1

;4 cycles (including call)
return
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top