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.

PIC16F877a

Status
Not open for further replies.

Harpua

New Member
Have trouble using TMR0 for a project. We we set the Timer witha prescar of 1:256 and sotre decimal vale of 252 in TMR0 to have it interupt every microsecond. I am having trouble getting it to start here is the initialization code:

TMR0Setup
bcf STATUS, RP1
bcf STATUS, RP0
movlw b'00100111' ;sets prescalar (1:256)
movwf OPTION_REG
movlw b'10100000'
movwf INTCON ;enables Gloabal and timer0 interruputs
call ResetTimer0


ResetTimer0
movlw d'250'
movwf TMR0 ;puts decimal 252 into timer register

bcf INTCON, TMR0IF ;clears the interrupt flag
return



TMR0 does not increment, any suggestions?
 
Your code listing is hard to read. Make use of the "Code" button to insert code in your post.

1. You have to initialize T1CON,TMR1ON.

2. Enable interrupts by setting INTCON1,PEIE and PIE1,TMR1IE.
 
Im using the timer0 module not timer1, its required in the specs of the project. here is code agian.
Code:
		include "p16F877a.inc"
    org 0x00
    errorlevel -302
	errorlevel -305
Temp_W equ 0x20
Temp_STATUS equ 0x21
MS_TIMER equ 0x22
Time1 equ 0x23
Time2 equ 0x24
TimeDiff equ 0x25
HexValue equ 0x26
ASCIIValue1 equ 0x27
ASCIIValue2 equ 0x28
Nibble equ 0x29

	goto Setup
	org 0x04
	call ISR
	org 0x10

Setup
	bcf STATUS, RP1
	bsf STATUS, RP0				;bank select 1
	movlw h'00'	
	movwf TRISB					;sets PORTB to all output
	movlw h'FF'					;sets PORTD to all input
	movwf TRISD
TMR0Setup
	bcf STATUS, RP1
	bcf STATUS, RP0
	movlw b'00000111'			;sets prescalar (1:256)
	movwf OPTION_REG
	bcf INTCON, TMR0IF
	bsf INTCON, 7
	bsf INTCON, 5				;enables Gloabal and timer0 interruputs
	movlw d'252'
	movwf TMR0					;puts decimal 252 into timer register
	bcf INTCON, TMR0IF			;clears the interrupt flag

UARTInitialize
    bsf STATUS, RP0
    movlw b'00011001'
    movwf SPBRG              ;sets 9600 baud rate and 4MHz clock 
    movlw b'00100110'
    movwf TXSTA              ;inititalizes the transmit register
    bcf STATUS, RP0
    movlw b'10010000'
    movwf RCSTA              ;intitializes the reciever resgister

SPIInitialize
	bcf STATUS, RP1
	bsf STATUS, RP0
	movlw b'01000000'
	movwf SSPSTAT			;sets up PIC1 SPI as master

 	movlw b'10010000'
    movwf TRISC              ;sets up PORTC for SPI use(*master)

	bcf STATUS, RP1
	bcf STATUS, RP0
	movlw b'00110000'
	movwf SSPCON


	
CheckBeamBreak
	btfsc PORTD, 0			;checks to see if the copper wire breaks beam
	goto CheckBeamBreak			
	movfw MS_TIMER				
	movwf Time1					;records time of breaking beam

CheckBeamRestore
	btfss PORTD, 0			;checks to see if copper wire has crossed beam
	goto CheckBeamRestore
	movfw MS_TIMER	
	movwf Time2					;stoers  this time
	call CalcDiff
	call DisplayTimes
	goto CheckBeamBreak


ISR
	bcf INTCON, GIE				;disables all interupts
	comf PORTB
SaveSettings
	movwf Temp_W				;saves W register
	swapf STATUS, W				;moves STATUS into W register
	movwf Temp_STATUS			;saves Temp_STATUS
	incf MS_TIMER				;incremments MS_TIMER
ResetTimer0
	movlw d'252'
	movwf TMR0					;puts decimal 252 into timer register
	bcf INTCON, TMR0IF			;clears the interrupt flag
	bsf INTCON, GIE				;enables interupts
RestoreSettings
	swapf Temp_STATUS, W		;Puts Temp_STATUS into W
	movwf STATUS				;restores STATUS register
	swapf Temp_W, F				;swaps nibbles of temp_w
	swapf Temp_W, W				;restores old value of the W register
	retfie
 
The timer0 intialization looks OK.

However, the ISR begins with "call ISR". This means that after retfie is executed, the program proceeds to what follows after the "call ISR" instruction and not back to the interrupted routine. The program falls through to the setup routine. Is this intentional?

BTW, there is no need to set GIE within the ISR because this is taken cared of by retfie. In fact, the only difference between "return" and "retfie" is that retfie sets GIE.
 
after RETFIE, it'll automatically enable GIE bit, you don't have to enable it in program.

You don't have to disable GIE in the program. If the other interrupts occurs, as you RETFIE, it will immediately return to the interrupts.

you should write like this

0004
; start interrupt here
call ISR
retfie
; end of interrupt here

...

; subroutine of interrupts service routine here
ISR
....
return
; go back and continue interrupt functions


As you wrote, the tos will still memorize the (address of ISR +1 ), and as you retfie, it will return to the tos (call ISR +1) . That is return after the call ISR instructions. Because instruction call will put the address of ISR to the tos before what will be done, and RETURN will clear the add of ISR in tos.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top