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

Binary clock/counter with interrupt, 16F690

Status
Not open for further replies.

Emil09

New Member
So I've got some code where the clock part functions but the external interrupt doesn't. The microcontroller counts up in binary on 4 LCDs, and should stop counting on the interrupt which comes on RA5. Unfortunately, nothing happens when I ground RA5. Thoughts?

#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

cblock 0x20
Display
W_Save
STATUS_Save
endc

org 0
goto init

ISR:
movwf W_Save
movf STATUS,w
movwf STATUS_Save

btfss PORTA,5 ;test bit, skip if set
goto ExitISR

btfsc PORTA,5
goto Start_Stop
clrf Display
goto ExitISR

Start_Stop:
clrf RABIF ;clear flag
bcf OPTION_REG,PSA ;prescaler is assigned to the Timer0 module

ExitISR:
movf STATUS_Save,w
movwf STATUS

swapf W_Save,f
swapf W_Save,w
retfie


init
banksel OSCCON ;select oscillator control
bcf OSCCON,6 ;set postscaler to 250 kHz

bsf STATUS,RP0
movlw b'00000111' ; configure Timer0. Sourced from the Processor clock;
movwf OPTION_REG ; Maximum Prescaler
clrf TRISC ; Make PortC all output
clrf Display
bcf STATUS,RP0
bsf INTCON,7 ;global interrupt enabled
banksel PORTA
bsf TRISA,5
bsf IOCA,5 ;interrupt on change enabled

;btfss PORTA,5 ;test bit, skip if set

ForeverLoop:
btfss INTCON,T0IF ; wait here until Timer0 rolls over
goto ForeverLoop
bcf INTCON,T0IF ; flag must be cleared in software
incf Display,f ; increment display variable
movf Display,w ; send to the LEDs
movwf PORTC
goto ForeverLoop


end
 

Attachments

ericgibbs

Well-Known Member
Most Helpful Member
hi emil,
Look at this edited code.
The problem was in the Init subr.

Setting PORT,5 hi/lo now generates an interrupt, also stop/start

Code:
	list  p=16F690         ; list directive to define processor
	#include <p16F690.inc> 
	errorlevel  -302 , -207             ; suppress message 302 from list file


	__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

	cblock	0x20
	Display
	W_Save
	STATUS_Save 
	endc

	org	0
	goto	init

	org 0x0004
ISR:
	movwf	W_Save
	movf	STATUS,w
	movwf	STATUS_Save
	
	btfss	PORTA,5		;test bit, skip if set
	goto	ExitISR

	btfsc	PORTA,5
	goto	Start_Stop
	clrf	Display
	goto	ExitISR

Start_Stop: 
	bcf	INTCON,RABIF		;clear flag
	bcf	OPTION_REG,PSA	;prescaler is assigned to the Timer0 module

ExitISR: 
	movf	STATUS_Save,w
	movwf	STATUS

	swapf	W_Save,f
	swapf	W_Save,w
	retfie


init
	banksel	OSCCON		;select oscillator control
	bcf	OSCCON,6	;set postscaler to 250 kHz

	bsf	STATUS,RP0
	movlw	b'00000000';111'	; configure Timer0. Sourced from the Processor clock;
	movwf	OPTION_REG	; Maximum Prescaler
	clrf	TRISC		; Make PortC all output
	clrf	Display
	bcf	STATUS,RP0
	bsf	INTCON,7	;global interrupt enabled
	
	bsf 	INTCON,RABIE
	bsf 	STATUS,RP1
	clrf 	ANSEL
	
	bcf 	STATUS,RP0
	bcf	STATUS,RP1
	bsf	TRISA,5

	bsf	STATUS,RP0
	bcf 	STATUS,RP1	
	bsf	IOCA,5		;interrupt on change enabled
	bcf 	STATUS,RP0
	bcf	STATUS,RP1
	
;btfss PORTA,5 ;test bit, skip if set  

ForeverLoop: 
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	ForeverLoop
	bcf	INTCON,T0IF	; flag must be cleared in software
	incf	Display,f	; increment display variable
	movf	Display,w	; send to the LEDs
	movwf	PORTC
	goto	ForeverLoop


	end
 

ericgibbs

Well-Known Member
Most Helpful Member
Fantastic! Thanks a lot Mr. Gibbs! :)
hi emil,

Note: that the TMR0 prescaler has been shortened to suit an Oshonsoft simulation.

Reload the TIMR0 with your original value.

Code:
bsf	STATUS,RP0
	movlw	b'00000[COLOR="RoyalBlue"]000';111[/COLOR]'	; configure Timer0. Sourced from the Processor clock;
	movwf	OPTION_REG	; Maximum Prescaler
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top