Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

capture module problem

Status
Not open for further replies.

amitaiwe

Member level 3
Joined
Feb 19, 2014
Messages
57
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
547
capture module not working

Hi to all and thanks to all how helped my till now in my project,

I'm using a PIC16F877 and for some reason it's capture module doesn't work.
A loop in my code which job is to wait for the flag announcement of capturing
isn't set and there's a infinite loop of waiting for that to happen.

A DMM which is connected to the CCP2 leg shows a frequency but I'm not sure that
its a true value there, (although I have already seen that it measures exact frequency
and duty cycle) since it shows sometimes also a frequency when disconnected.

have I done the configuration and capture loop correctly?
The other sections of the code work properly.
Thank,
Amitai

Code:
LIST	P=PIC16F877
    	
include	<P16f877.inc>
org		0x00

	__CONFIG _LVP_OFF &_WDT_OFF &_PWRTE_OFF &_CP_OFF & _HS_OSC	

	reset:
	nop
	goto	start
	org		0x20

start:




; configuration
;-----------------------------------

; used_ registers:
	MUX_sel			EQU H'0021'  		; for not transmitting '000000000'
	del1			EQU H'0030'  		; for delay
	del2			EQU H'0031'  		; for delay
	to_transmit		EQU H'0050'  		; Frequency data register
	led1			EQU H'0070'  		; for led delay
	demo_tr			EQU H'0072'  		; demo transmit
	loop_mux		EQU H'0074'  		; demo transmit

	banksel		ADCON1
	movlw		0x07				; ADC PINS configured as a digital I/O									
	movwf		ADCON1

;UART/*/*/
	banksel		TRISC
	bsf	 	 	TRISC, 6 
	bsf		 	TRISC, 7	
	
	banksel  	TXSTA
	movlw    	0xA7        		; master, transmit EN, Async mode, Transmit Shift Register empty, BRGH =1
	movwf    	TXSTA	       		; Baud Rate = Fosc/(16(SPBRG+1)) , BRGH (baud rate) ignored in sync
	
	banksel		SPBRG
	movlw	 	0x03		   		; = d"16" which corresponds to 125Kbps with 8MHz clock
	movwf	 	SPBRG
	
	banksel  	RCSTA
	movlw    	0x80		  		; enable UART	
	movwf    	RCSTA
	
;PWM, timer2/*/*/
	banksel		TRISC
	bcf			TRISC, 2 			; make the CCP1 pin an output.
	
	banksel		T2CON
	movlw		0x04
	movwf		T2CON				; no post/Prescaler (1:1) , TMR2 ON
	
	banksel		PR2					; output frequency = 15000hz 	=			
	movlw		0x84
	movwf		PR2					; 1 / [(PR2) + 1] • 4 • TOSC •(TMR2 prescale value)
	
	banksel		CCPR1L				;duty cycle =(CCPR1L:CCP1CON<5:4>) • TOSC • (TMR2 prescale value)
	movlw		0x42				;           = 267 *1/8MHz = 50% d.c.
	movwf		CCPR1L				
	banksel		CCP1CON
	movlw		0x3C
	movwf		CCP1CON

;capture, timer1 /*/*/
	banksel		TRISC
	bsf			TRISC, 1 			; make the CCP2 pin an input.

	banksel		T1CON
	movlw		0x09				; Timer1 Input Clock Prescale 1:1, en TMR1
	movwf		T1CON				; , int clk (fosc/4) , oscillator en
	banksel		CCP2CON
	movlw		0x07
	movwf		CCP2CON				; Capture mode, every 16th rising edge 
	banksel		PIE2
	bcf			PIE2, CCP2IE		; avoid false interrupts 
	
;MUX select
	banksel		TRISD
	bcf			TRISD, 0
	bcf			TRISD, 1
	bcf			TRISD, 2

;LED general delay
	bcf			TRISD, 4
	bcf			TRISD, 5
	banksel		PORTD	
	bcf			PORTD, 4
	bcf			PORTD, 5
	
; end configuration --------------------------------------




;************ MAIN PROGRAM ***************
;*****************************************

main:
	
;MUX init
	movlw		0x07
	movwf		MUX_sel
	movlw		0x08
	movwf		loop_mux
	
;transmit check /*/*/*/*
    movlw		0x05					; demo check
	movwf		demo_tr
	tra:
	CALL		DELAY
	movf		demo_tr, w
	movwf		to_transmit 
	CALL 		transmitting_data
	decfsz		demo_tr
	goto		tra   	
	
main_loop:

Led_flicker:
	movlw 		0x30
	movwf		led1
	t2:
	CALL		DELAY
	decfsz		led1
	goto		t2
		
	bcf			PORTD,5
	bsf			PORTD,4		
		
	banksel		led1		
	movlw 		0x30
	movwf		led1
	t1:
	CALL		DELAY
	decfsz		led1
	goto		t1

	bcf			PORTD,4
	bsf			PORTD,5
;---end Led flicker

;capture frequency/*/*
	banksel		TMR1H	
	clrf		TMR1H
	clrf		TMR1L
	
	banksel		PIR2
	bcf			PIR2, CCP2IF
	check_capture:		
	btfss		PIR2, CCP2IF					; 1  = A TMR1 register capture occurred (must be cleared in software)
	goto		check_capture
	bcf			PIR2, CCP2IF
	
;transmitting data	
	banksel		CCPR2H							; transmit MSB
	movf		CCPR2H, w
	banksel		to_transmit
	movwf		to_transmit
	sublw		0xFF				; if data = FF change to FE
	btfsc		STATUS, Z
	CALL		data_change	
	CALL 		transmitting_data
	
	banksel		CCPR2L							; transmit LSB
	movf		CCPR2L, w
	banksel		to_transmit
	movwf		to_transmit
	sublw		0xFF				; if data = FF change to FE
	btfsc		STATUS, Z
	CALL		data_change
	CALL 		transmitting_data
		
;MUX select/*/*/
	
	movf		MUX_sel,w
	banksel		PORTD
	movwf		PORTD
	decf		MUX_sel, f
	decfsz		loop_mux
	goto		main_loop
	movlw		0x07
	movwf		MUX_sel
	movlw		0x08
	movwf		loop_mux
	
	movlw		0xFF							; send FF at end of buffers
	movwf		to_transmit
	CALL		transmitting_data
goto main_loop

	
				
;******** END MAIN LOOP ********************		

transmitting_data:	
	banksel		TXREG
	movwf   	TXREG					    	; writing to TXREG clears TXIF
    trans_wait2:
	btfss		PIR1, TXIF						; wait for the frame to be transmitted
	goto		trans_wait2
	CALL		DELAY
	return
	
data_change:
	movlw		0xFE
	movwf		to_transmit
	return
	
DELAY:
	movlw		0xFF
	movwf		del1
	movlw		0xFF
	movwf		del2
	
	loopa:
	decfsz		del1, f
	goto		loopa
	loopb:
	decfsz		del2, f
	goto		loopb
	return
		
end
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top