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.

count pulse in input porta in 15second

Status
Not open for further replies.

nisaq

New Member
anyone can help me?
please give me example how to count pulse in input from porta in 15sec by using assembly language.
i use pic16f84a, 4Mhz, and port a as input. i want count how much input pulse in 15sec.
 
You'll need timer0 for timing the seconds and RB0 for interrupt for the pulse counter. I have a piece of code that Counts pulses per seconds and displays the speed and payout (winch control)

All the elements you need are in there, unfortunately the are no comments as this was ported directly to C as the code was getting too large.

Code:
	LIST p=16f84
	#include "p16f84.inc"	

Statusp		EQU	0Ch
Wdb		EQU	0Dh
Lookdb		EQU	0Eh
Rsdb		EQU	0Fh
Count1000	EQU	10h
Count100	EQU	11h
Count10		EQU	12h
Count1		EQU	13h
Count01		EQU	14h
Low1		EQU	15h
Low2		Equ	16h
InvHig1		EQU	17h
Main1		EQU	18h
Main2		EQU	19h
Main3		EQU	1Ah	
Hig1		EQU	1Bh
Hig2		EQU	1Ch
InvHig2		EQU	1Dh
Temp1		EQU	1Eh
Counter1	EQU	20h
Counter3	EQU	21h
Counter2	Equ	22h
milli		EQU	23h
NMSB		EQU	24h
MSB		EQU	25h
LSB		Equ	26h
Dir		EQU	27h
Multi		EQU	28h
R_NMSB		EQU	29h
R_MSB		EQU	2Ah
R_LSB		EQU	2Bh
divisor		EQU	2Ch
Loop		EQU	2Dh
Hold1		EQU	2Eh
Hold2		EQU	2Fh
Temp2		EQU	30h
Fall		EQU	31h
InvMain1	EQU	32h
InvMain2	EQU	33h
PRE		EQU	34h
PRE1		EQU	35h
PRE2		EQU	36h
StCnt		EQU	37h
		
		org 	0  
		goto 	Start
		org 	4
		goto 	Intrp	;Int vect

		org 	5
Lcddata		addwf	PCL,f
		retlw	32h
		retlw	2Ch
		retlw	08h
		retlw	06h
		retlw	0Ch
		retlw	01h
		retlw	02h
		retlw	0

Text1		addwf	PCL,f
		dt	"Payout=        M",0
		dt	"M/Sec =         ",0
		dt	"F=   ADJ +     - ",0

Start		bsf	STATUS,RP0
		movlw 	b'10011111'
		movwf	TRISB
		movlw	0
		movwf	TRISA
		clrwdt
		movlw	b'01000111'
		movwf	OPTION_REG
			
		bcf	STATUS,RP0
		bsf	PORTB,6
		clrf	Rsdb
		call	Delay20
		call 	Lcdinit
		clrw 
		bcf	PORTB,6
		btfss	PORTB,2
		addlw	1
		btfss	PORTB,3
		addlw	2
		btfss	PORTB,4
		addlw	4	
		bsf	PORTB,6
		movwf	StCnt

		clrf	Lookdb
Setlcd		movf	Lookdb,w
		call 	Lcddata
		andlw	0FFh
		btfsc	STATUS,Z
		goto 	Progst
		call 	Lcdcom
		call	Delay10
		incf	Lookdb,f
		goto 	Setlcd
		movf	PORTB,w

Progst		movlw	.128
		movwf	Multi
		movlw	1
		movwf	Fall
		clrf	Main1
		clrf	Main2
		clrf	Count01
		clrf	Count1
		clrf	Count10
		clrf	Count100
		clrf	Count1000
		
RET		call	Top00
		movlw	00h
		call 	Sendtext
		call	Bot00
		movlw	11h
		call 	Sendtext	
		call	Sendcount
		movlw	b'10110000'
		movwf	INTCON
	
Stop		btfsc	Temp2,0
		call	Sendspeed
		btfsc	Temp2,1
		call	Sendcount
		btfss	PORTB,7
		call	Clear
		btfsc	PORTB,3
		goto	Stop	
		btfsc	PORTB,2
		goto	Stop
		call	Calmode
		goto	Stop

Sendspeed	clrf	NMSB
		movf	Hig1,w
		movwf	LSB
		movf	Hig2,w
		movwf	MSB
		
		btfss	Hig2,7
		goto	Sendspeed2
		movf	InvHig1,w
		movwf	LSB
		movf	InvHig2,w
		movwf	MSB
		
Sendspeed2	call	Mod
		clrf	R_LSB
		clrf	R_MSB
		clrf	R_NMSB
		movlw	0Ah
		movwf	divisor
		call	MUL16X16
		movf	R_LSB,w
		movwf	LSB
		movf	R_MSB,w
		movwf	MSB
		movf	Fall,w
		movwf	divisor
		call	DIV16X8	
		call	Tme1
		call	BCD
		call	Senddigits2	
		bcf	Temp2,0
		return

Sendcount	clrf	NMSB
		movf	Main1,w
		movwf	LSB
		movf	Main2,w
		movwf	MSB
		
		btfss	Main2,7
		goto	Sendcount1
		movf	InvMain1,w
		movwf	LSB
		movf	InvMain2,w
		movwf	MSB
Sendcount1	call	Mod
		call	BCD
		call	Tme
		call	Senddigits
		btfss	Main2,7
		goto	Sendcount2
		movlw	88h
		call	Lcdcom
		movlw	2Dh
		call	Lcdout
		goto	Sendcount3
Sendcount2	movlw	88h
		call	Lcdcom
		movlw	2Bh
		call	Lcdout
Sendcount3	bcf	Temp2,1
		return

Clear		bsf	PORTB,5
		bcf	PORTB,6
		nop
		nop
		nop
		bsf	PORTB,6
		bcf	PORTB,5
		call	Tme
		clrf	Main1
		clrf	Main2
		clrf	Count01
		clrf	Count1
		clrf	Count10
		clrf	Count100
		clrf	Count1000
		bcf	PORTB,5
		bcf	PORTB,6
		nop
		nop
		nop
		bsf	PORTB,6
		bcf	PORTB,5
				

Senddigits	movf	Count1000,w	
		call	Sendnumb
		movf	Count100,w
		call	Sendnumb
		movf	Count10,w	
		call	Sendnumb
		movf	Count1,w
		call	Sendnumb
		movlw	'.'
		call	Lcdout	
		movf	Count01,w	
		call	Sendnumb
		
		return

Senddigits2	movf	Count100,w	
		call	Sendnumb
		movf	Count10,w
		call	Sendnumb
		movlw	'.'
		call	Lcdout	
		movf	Count1,w	
		call	Sendnumb
		movf	Count01,w	
		call	Sendnumb
		return

Senddigits3	movf	Count10,w	
		call	Sendnumb
Senddigits4	movf	Count1,w
		call	Sendnumb
		movf	Count01,w	
		call	Sendnumb
		return

Sendnumb	addlw	'0'
		call	Lcdout
		return

Calmode		bcf	INTCON,GIE
		movf	Main1,w
		movwf	Hold1
		movf	Main2,w
		movwf	Hold2
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		call	Bot00
		movlw	22h
		call	Sendtext
CAL1		clrf	MSB
		bcf	STATUS,C
		movlw	0Dh
		subwf	Fall,w
		btfss	STATUS,Z
		goto	CAL2
		movlw	1
		movwf	Fall
CAL2		movf	Hold1,w
		movwf	Main1
		movf	Hold2,w
		movwf	Main2
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		movf	Multi,w
		movwf	LSB
		call	BCD
		call	Tme3
		call	Senddigits3
		call	Sendcount
		call	Tme2
		movf	Fall,w
		movwf	LSB
		clrf	MSB
		clrf	NMSB
		call	BCD
		call	Senddigits4
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		call	Delay20
		btfss	PORTB,4
		incf	Fall,f
		btfss	PORTB,3
		decf	Multi,f
		btfss	PORTB,2
		incf	Multi,f
		btfsc   PORTB,3
		goto	CAL1
		btfsc	PORTB,2
		goto	CAL1
		call	Delay100
		call	Bot00
		movlw	11h
		call 	Sendtext
		movlw	b'10110000'
		movwf	INTCON	
		return

Mod		call 	MUL16X8
		movlw	7
		movwf	divisor
DIV128	bcf 	STATUS,C
		rrf	R_NMSB,f
		rrf	R_MSB,f
		rrf	R_LSB,f
		decfsz	divisor,f
		goto	DIV128
		
		movf	R_LSB,w
		movwf	LSB
		movf	R_MSB,w
		movwf	MSB
		return

MUL16X8		clrf	R_LSB
		clrf	R_MSB
		clrf	R_NMSB
		movf	Multi,w
		movwf	divisor
MUL16X16	movlw	9
		movwf	Loop
MUL16		btfsc	divisor,0
		goto    Add
MUL2		bcf	STATUS,C
		rlf	LSB,f
		rlf	MSB,f
		rlf	NMSB,f
		bcf	STATUS,C
		rrf	divisor,f
		decfsz	Loop,f
		goto 	MUL16
		return

Add		bcf	STATUS,C
		movf	LSB,w
		addwf	R_LSB,f
		btfsc	STATUS,C
		goto	IncM
Add1		bcf	STATUS,C
		movf 	MSB,w
		addwf	R_MSB,f
		btfsc	STATUS,C
		incf	R_NMSB,f
		bcf	STATUS,C
		movf	NMSB,w
		addwf	R_NMSB,f
		goto	MUL2		
IncM		bcf	STATUS,C
		incf	R_MSB,f
		btfsc	STATUS,C
		incf	R_NMSB,f
		goto	Add1

DIV16X8		clrf	R_NMSB
		clrf	R_MSB
		clrf	R_LSB	
		movlw	.17
		movwf	Loop
Div1		rlf	LSB,f
		rlf	MSB,f
		decfsz	Loop,f
		goto	Div2
		goto	Div4
Div2		rlf	R_NMSB,f
		movf	divisor,w
		subwf	R_NMSB,w
		movwf	R_NMSB
		btfsc	STATUS,C
		goto	Div3
		movf	divisor,w
		addwf	R_NMSB,f
		bcf	STATUS,C
		goto	Div1
Div3		bsf	STATUS,C
		goto	Div1
Div4		movf	LSB,w
		movwf	R_LSB
		movf	MSB,w
		movwf	R_MSB
		return

Sendtext	movwf	Lookdb
Sx1		movf	Lookdb,w
		call 	Text1
		andlw	0FFh
		btfsc	STATUS,Z
		return
		call 	Lcdout
		incf	Lookdb,f
		goto	Sx1
		
Lcdinit		bcf	Rsdb,4
		movlw	033h
		movwf	Temp1
		call	Sendnib
		call	Delay10
		call	Sendnib
		return

Tme		movlw	089h
		goto	Lcdcom
Tme1		movlw	0C9h
		goto	Lcdcom
Tme2		movlw	0C2h
		goto	Lcdcom
Tme3		movlw	0CBh
		goto	Lcdcom
Top00		movlw	080h
		goto	Lcdcom
Bot00		movlw	0C0h
Lcdcom		bcf	Rsdb,4
		goto	Lt1
Lcdout		bsf	Rsdb,4
Lt1		movwf	Temp1
		call	Sendnib
		call	Sendnib
		return

Sendnib		swapf	Temp1,F
		movf	Temp1,W
		andlw	0Fh
		iorwf	Rsdb,w
		movwf	PORTA
		bsf	PORTB,5
		bcf	PORTB,5
		movlw	032h
		movwf	Counter1
Sb1		decfsz	Counter1,F
		goto	Sb1
		return

Delay100	movlw	064h
		goto	Dy3
Delay20		movlw	2
		goto	Dy3

Delay10		movlw	1
Dy3		movwf	Counter3
		movlw	5
		clrf	Counter1
Dy2		movlw	0Ch
		movwf	Counter2
Dy1		decfsz	Counter1,F
		goto	Dy1		
		decfsz	Counter2,F
		goto	Dy1
		decfsz	Counter3,F
		goto	Dy2
		return	

Sub16        	COMF 	LSB               ; Take two's complement
             	COMF 	MSB               ; of n2. If zero, n2L
             	INCF 	LSB
             	BTFSC 	STATUS,Z          ; overflowed, so carry 
             	INCF 	MSB               ; into n2H.
Add16        	MOVF 	LSB,w             ; Add the LSBs. If carry,
             	ADDWF 	PRE1
             	BTFSC 	STATUS,C          ; increment MSB of sum. 
             	INCF 	PRE2                  
             	MOVF 	MSB,w             ; Add the MSBs  
             	ADDWF 	PRE2
             	return                  


Intrp		movwf	Wdb
		movf	STATUS,w
		bcf	STATUS,5
		movwf	Statusp
		bcf	STATUS,C
		
		btfsc	INTCON,T0IF		
		goto	Tim
		
		btfsc	PORTB,1
		goto	Inccount
		btfss	PORTB,1
		goto	Deccount

Int2		bsf	Temp2,1
		movf	Main1,w
		movwf	InvMain1
		comf	InvMain1,f
		movf	Main2,w
		movwf	InvMain2
		comf	InvMain2,f
		bcf	INTCON,INTF
		goto	Int4
Int3		bsf	Temp2,0
		movf	Hig1,w
		movwf	InvHig1
		comf	InvHig1,f
		incf	InvHig1,f
		movf	Hig2,w
		movwf	InvHig2
		comf	InvHig2,f
		bcf	INTCON,T0IF
Int4		bsf	INTCON,GIE
		movf	Statusp,w	
		movwf	STATUS
		swapf	Wdb,f
		swapf	Wdb,w
		retfie
	
Tim		movlw	0FAh
		subwf	TMR0,f
		movlw	010H
		addwf	milli,f
		btfss	STATUS,C
		goto 	Int3
	
		movlw	0FAh
		subwf	milli,f
		
		movf	Low1,w
		movwf	LSB
		movf	Main1,w
		movwf	PRE1
		movwf	Low1
		movf	Low2,w
		movwf	MSB
		movf	Main2,w
		movwf	PRE2
		movwf	Low2
		call	Sub16
		movf	PRE1,w
		movwf	Hig1
		movf	PRE2,w
		movwf	Hig2
		goto	Int3
		
Inccount	bsf	Dir,0
		movf	StCnt,w
		bcf	STATUS,C
		subwf	PRE,w
		btfsc	STATUS,C
		goto 	Int6
		incf	PRE,f
		goto	Int2

Int6		clrf	PRE
		incf	Main1,f
		btfss	STATUS,Z
		goto	Int2
		incf	Main2,f
		goto	Int2

Deccount	bcf	Dir,0
		movf	PRE,w
		btfsc	STATUS,Z
		goto 	Int7
		decf	PRE,f
		goto	Int2
		
Int7		movf	StCnt,w
		movwf	PRE
		movf	Main1,w
		btfsc	STATUS,Z
		decf	Main2,f
		decf	Main1,f
		goto	Int2
	
BCD		swapf	MSB,w	
        	andlw   0x0F     
        	addlw   0xF0	
        	movwf   Count100
        	addwf   Count100,f	
        	addlw   .226	
        	movwf   Count10
        	addlw   .50	
       	 	movwf   Count01
        	movf    MSB,w	
        	andlw   0x0F	
        	addwf   Count10,f	
        	addwf   Count10,f	
        	addwf   Count01,f	
        	addlw   .233	
        	movwf   Count1	
        	addwf   Count1,f	
        	addwf   Count1,f	

        	swapf   LSB,w	
        	andlw   0x0F	
        	addwf   Count1,f	
        	addwf   Count01,f	
	        rlf     Count1,f	
	        rlf     Count01,f	
	        comf    Count01,f	
	        rlf     Count01,f	
	        movf    LSB,w	
        	andlw   0x0F	
        	addwf   Count01,f	
        	rlf    	Count100,f	
	        movlw   0x07	
	        movwf   Count1000
		movlw   .10	
Lb1		addwf   Count01,f	
        	decf    Count1,f	
        	btfss   3,0
	        goto   	Lb1	
	
Lb2		addwf   Count1,f	
        	decf    Count10,f	
        	btfss   3,0
         	goto   	Lb2	
Lb3		addwf   Count10,f	
        	decf    Count100,f
        	btfss   3,0
         	goto   	Lb3	
Lb4	        addwf   Count100,f	
        	decf    Count1000,f	
        	btfss   3,0
         	goto   	Lb4	
	        retlw   0

		END

It runs on a pic16f84a at 4mhz Cheers Ian

Oh yes! there are some math routines for anyone who needs them...
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top