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.

example of using TMR0 interrupt

Status
Not open for further replies.

kichapi

Junior Member level 2
Joined
Dec 6, 2009
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
india
Activity points
1,408
interrupt

how to configer interrupt function?i used 16f877a pic and how to calculate TMR0? please help me?
 

Re: interrupt

Hi,

It could be very useful to know compiler, your using.

The theory is.

(from data sheet you can find x register address and value)
-Enable global interrupt
-Enable interrupt on timer 0 overflow
also you may need to configure timer (clock, 8 or 16 bit, clock division )

To execute interrupt function you need add interrupt name (which compiler are you using) and under it you can add function to execute. Example #TIMER0_INT
 

interrupt

Hi kichapi,
Here I post an example of using TMR0 interrupt:
[Code written in Microchip ASM]

Code:
	LIST P=16F877A
	__CONFIG _CP_OFF & _DEBUG_OFF & _WRT_OFF & _LVP_OFF & _CPD_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _XT_OSC

	#INCLUDE <P16F877A.INC>
	
	CBLOCK 0x20
		COUNT, PCOUNT
	ENDC

	ORG 0x00
	GOTO MAIN

	ORG 0x04 ;INTERRUPT VECTOR
	GOTO ISR ;INTERRUPT SERVICE ROUTINE
;================================================

ISR
	INCF	COUNT ;INCREMENT CONTENTS OF COUNT
	MOVF	COUNT, W ;MOVE COUNT TO W
	BTFSC	STATUS, Z ;CHECK IF COUNT = 0
	;COUNT = 0, WHEN COUNT INCREMENTS AND OVERFLOWS AT ROLLOVER
	;	FROM 255 TO 0
	INCF	PCOUNT ;EVERY OVERFLOW OF COUNT, INCREMENT CONTENTS
	;OF PCOUNT
	MOVF	PCOUNT, W ;MOVE CONTENTS OF PCOUNT TO W
	MOVWF	PORTB ;MOVE CONTENTS OF W TO PORTB
	BANKSEL	INTCON ;BANK0
	BCF		INTCON, TMR0IF ;CLEAR INTERRUPT FLAG
RETFIE
;================================================

MAIN
	BSF		COUNT, 1 ;COUNT = 1
	CLRF	CCP1CON ;CCP OFF
	BANKSEL	TRISB ;BANK1
	CLRF	TRISB ;ALL OUTPUT
	MOVLW	.7
	MOVWF	CMCON ;COMPARATOR OFF
	MOVWF	ADCON1 ;ADC OFF
	MOVLW	0x82
	MOVWF	OPTION_REG ;PORTB PULL-UP OFF, PRESCALER- 1:8
	BANKSEL	INTCON ;BANK2
	MOVLW	0xA0
	MOVWF	INTCON ;Global interrupt and TMR0 interrupt enabled
	CLRF	PORTB ;CLEAR PORTB
	BANKSEL	TMR0 ;BANK0
	CLRF	TMR0 ;RESTART TMR0
	GOTO	$ ;WAIT FOR INTERRUPT
END
;==================================================
I have commented so that you understand what happens. Every instruction = 1/ (4MHz/4) = 1us
Every (8 * 256) [8 because of prescaler] instructions, interrupt occurs, so interrupt occurs every 2.048 ms. Every (2.048 * 256)ms PCount increments. So PORTB value is incremented every 524ms. Try it out yourself.

Added after 7 minutes:

Same thing in mikroBASIC:
Code:
program TMR0INTEDA

dim COUNT as byte
dim PCOUNT as byte

sub procedure interrupt
    inc(COUNT)
    if (COUNT = 0) then
       inc(PCOUNT)
    end if
    PORTB = PCOUNT
    INTCON.TMR0IF = 0
end sub

main:
     COUNT = 1
     CCP1CON = 0
     TRISB = 0
     CMCON = 7
     ADCON1 = 7
     OPTION_REG = 0x82
     INTCON = 0xA0
     PORTB = 0
     TMR0 = 0
     while true
     wend
end.
 

Dear I am working in one project where I need to get pulses on RA4 of 16F877, I am unable to do so, see if you can help me. I just need to get the pulses and display on LCD. I have uploaded all of my code at

www.evsoft.pk/azhar/Pulses.rar

Thanks in Advance.

-Azhar
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top