• 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.

just getting around with PIC assembler

Status
Not open for further replies.

t.man

New Member
hi guys!,
i'm just trying to find my way with basics around PIC and assembly language.
now i'm trying timer1 on compare match interrupt but it doesn't seem doing that.

may you come to my rescue?
Code:
;timer1 in clear timer1 on interrupt
;developer: ????
;for PIC: 16f877a
;


	processor 16f877a
	include "p16f877a.inc"
	
	errorlevel -302, -207;supress error message 302
	__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

	cblock 0x20
			W_TEMP
			STATUS_TEMP
			PCLATH_TEMP
	endc
	org 0x00
	goto main


	org 0x04
	goto ISR
	

init
	banksel TRISB
	clrf TRISB ;all outputs
	banksel PORTB
	clrf PORTB;all output pins low
	movlw 0x0A
	banksel CCP1CON
	movwf CCP1CON;Compare mode, generate software interrupt on match
	banksel PIE1
	bsf PIE1, CCP1IE;enable CCP interrupt
	banksel INTCON
	bsf INTCON, GIE;enable interrupts
	banksel CCPR1L
	movlw .40
	movwf CCPR1L
	clrf CCPR1H;
	movlw b'00101001'
	banksel T1CON
	movwf T1CON; set ups
	banksel TMR1L
	clrf TMR1L
	banksel TMR1H
	clrf TMR1H
	retlw 0x00

main
	call init

loop
	goto loop

ISR
	swapf STATUS,W
	movwf STATUS_TEMP
	movlw 0x01
	xorwf PORTB,f;toggle
exit
	swapf STATUS_TEMP,W
	movwf STATUS
	retfie

	


	end
 

Gayan Soyza

Active Member
:D
To trigger you must first clear the flag bit in the initialization section & on every interrupt.
You just using this line.You must clear the flag bit next to this line.
Code:
bsf PIE1, CCP1IE;enable CCP interrupt
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
The reason the interrupt is not happening is because the peripheral interrupt bit isn't set. Try adding,
Code:
	bsf INTCON, PEIE
Mike.
 

t.man

New Member
The reason the interrupt is not happening is because the peripheral interrupt bit isn't set. Try adding,
Code:
	bsf INTCON, PEIE
Mike.

that solves the problem
i modified the code like this:
Code:
;timer1 in CCP mode
;developer: ????
;for PIC: 16f877a
;


	processor 16f877a
	include "p16f877a.inc"
	
	errorlevel -302, -207;supress error message 302
	__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

	cblock 0x20
			W_TEMP
			STATUS_TEMP
			PCLATH_TEMP
	endc

	org 0x00
	goto main


	org 0x04
	goto ISR
	

init
	bsf STATUS,RP0; select bank1
       clrf TRISB; all pins outputs

	bcf STATUS,RP0; bank0
	clrf PORTB;all output pins low
	movlw b'00001001';timer1 osc. on
	movwf T1CON
	movlw b'00001011'
	movwf CCP1CON
	movlw .40;clear timer1 on 40
	movwf CCPR1L
	clrf CCPR1H
	bcf PIR1,CCP1IF

	bsf STATUS,RP0; bank1
	bsf PIE1, CCP1IE;enable ccp interrupt
	bsf INTCON, PEIE;pherpheral interrupts
	bsf INTCON, GIE;enable global interrupts

	bcf STATUS,RP0;back to bank0
	clrf TMR1L
	clrf TMR1H
	retlw 0x00

main
	call init

loop
	goto loop

ISR
	swapf STATUS,W
	movwf STATUS_TEMP
	movlw 0x01
	xorwf PORTB,f;toggle
exit
	swapf STATUS_TEMP,W
	movwf STATUS
	bcf PIR1,CCP1IF
	retfie

	


	end
now i want to define a frequency at which this interrupt occurs
am i right to say:
Code:
timer target count = [timer clock frequency]/[4 * target frequency] - 1
e.g if i have 4mhz processor, and my target frequency is 20hz, then timer target count would be:

Code:
timer target count = 4mhz/[4 * 20hz]- 1 = 49,999
kind regards,
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Yes, and so you can do,
Code:
	movlw	low(49999)
	movwf	CCPR1L
	movlw	high(49999)
	movwf	CCPR1H
Mike.
 

t.man

New Member
Yes, and so you can do,
Code:
	movlw	low(49999)
	movwf	CCPR1L
	movlw	high(49999)
	movwf	CCPR1H
Mike.
just noted i have to add the preceding "dots"
how do i make a decimal default?
Code:
movlw	low(.49999)
	movwf	CCPR1L
	movlw	high(.49999)
	movwf	CCPR1H
 

Mike - K8LH

Well-Known Member
Roman, there's also an 'upper()' operator to capture the b23..b16 portion of the constant operand. So, a matched set of sorts for setting TBLPTRL, H, and U (grin)...

Have fun, Mike
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top