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.

Interupts with TMR1 and RB0 on 16f628

Status
Not open for further replies.

Fletcher

New Member
Im trying to write some code which will flash a led every 0.5seconds using TMR1. Then i want to use a momentary switch connected to rb0 to turn the flashing sequence on and off. Ive got the button interrupt working but now i need to work the tmr1 interrupt into it.

I was just wondering is this looks any good:

Code:
BANK0 MACRO
BCF STATUS,RP0
BCF STATUS,RP1
ENDM

BANK1 MACRO
BSF STATUS,RP0
BCF STATUS,RP1
ENDM

ORG 0X000
GOTO SETUP

ORG 0X004
BTFSC INTCON,INTF	;has a switch been pressed?
CALL SWITCH			;go and turn tmr1 on or off
BTFSC PIR1,TMR1IF	;has tmr1 overflowed?
CALL TIMER			;turn led on or off
RETFIE


SETUP
BANK1
MOVLW 0XD7
MOVWF OPTION_REG	;Sets int on falling edge
MOVLW B'00000001'
MOVWF TRISB			;1 input
MOVLW B'00000001'
MOVWF PIE1			;tmr1 overflow bit
BANK0
MOVLW B'10010000'
MOVWF INTCON		;GIE and INTE set
MOVLW 0X39
MOVWF T1CON			;1:8 prescale, internal osc
MOVLW 0XF8
MOVWF TMR1H			
MOVLW 0X60
MOVWF TMR1L			;setup tmr1 for 0.5sec (i hope)


MAIN
GOTO MAIN			;wait until something happens


TIMER
BTFSS PORTB,7		;is led on?
GOTO THERE			;turn it on if its not
BCF PORTB,7			;turn led off
BCF PIR1,TMR1IF		;reset interrupt
MOVLW 0XF8
MOVWF TMR1H
MOVLW 0X60
MOVLW TMR1L			;reset tmr1
RETURN				
THERE: BSF PORTB,7	;turn led on
BCF PIR1,TMR1IF		;reset interrupt
MOVLW 0XF8
MOVWF TMR1H
MOVLW 0X60
MOVLW TMR1L			;reset interrupt
RETURN

SWITCH
BTFSC T1CON,TMR1ON	;is tmr1 on?
GOTO HERE			;jump is it is
BSF T1CON,TMR1ON	;turn tmr1 on
BCF INTCON,INTF		;reset flag
RETURN
HERE: BCF T1CON,TMR1ON ;turn tmr1 off
BCF INTCON,INTF		;reset flag
RETURN
 
I'd like to go through your code except that why would you bother using interrupts at such low speed events as push buttons press and 0.5 sec flash?

Polling the tmr1 interrupt flag and RB0 would be a lot easier. Besides using interrupts on RB0 would add the problem of switch bounce.
 
Its purely for educational purposes only, and no its not homework. Im just trying to work out how these interrupts work, particually TMR1.

I just want to know whether ive got the basic idea right.
 
OK.

A quick glance at your software shows you're not saving the context during interrupts. This means that WREG and STATUS register altered during interrupt service is not saved on entry to the ISR and not restored prior to RETFIE. This may result in bugs in the main program.

Fix that one first and then I can review your software again.
 
ok i understand that, not really much point for this program is there? Since it just sits in a loop much of the time.

Ive been playing around in mplabsim and the button interrupt works ok, i can hit the button and it will toggle TMR1 on and off. However i cant get TMR1 to interupt when it overflows. PIR1 bit 0 goes high but theres no interrupt.
 
Fletcher said:
Ive been playing around in mplabsim and the button interrupt works ok, i can hit the button and it will toggle TMR1 on and off.

In reality, switch bounce will make it toggle a number of times with every press of the button.

However i cant get TMR1 to interupt when it overflows. PIR1 bit 0 goes high but theres no interrupt.

You also have to set PEIE in INTCON.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top