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.

Timer 0 interrupt on Pic 16F629

Status
Not open for further replies.

blackman

New Member
I am trying to learn how to do interrupts on the 16F629.

The basic idea is that I'll have a LED attached to one of the
output pins (other than GP3) which will flash as Timer0 keeps
overflowing.

My code doesn't work for some reason. Could somebody clue
me in?

Code:
LIST	p=PIC12F629           ;tell assembler what chip we are using
include "P12F629.inc"         ;include the defaults for the chip
__config b'11000110010100'    ;sets the configuration settings (oscillator type etc.)

cblock 0x20 		      ;start of general purpose registers
endc
	
	org 0x0000                    ;org sets the origin
	goto Initialize

	org 0x0004
INT
	bcf STATUS, RP0               ;select bank 0
	bcf INTCON, T0IF	
	comf GPIO, f
	retfie

Initialize

	bcf STATUS, RP0               ;select bank 0
	movlw 0x07
	movwf CMCON                   ;turn comparators off (p.37)

	bsf STATUS, RP0               ;select bank 1
	movlw b'00001000'
	movwf TRISIO

	movlw 0x00			
	movwf OPTION_REG

	movlw b'10100000'
	movwf INTCON

	bcf STATUS, RP0               ;select bank 0
	movlw 0x00				;set everything off
	movwf GPIO
	clrf TMR0

Infinite
	goto Infinite

END
 
Do you need to configure the GPIO pins as I/O pins by telling the processor that they are not A/D converter inputs? Just wondering.
 
Papabravo said:
Do you need to configure the GPIO pins as I/O pins by telling the processor that they are not A/D converter inputs? Just wondering.
I'm using the 12f629 which does not have an A/D converter...
 
The message title and the text says 16F629, whilst the code says 12F629. You might want to make the correction.

In any case it would not surprise me if the default for the GPIO pins is something else besides general purpose I/O. Look for the definition of the power on default value for each register that affects the GPIO pins.

You must read the datasheet with extraordinary care in order to get it right.
 
Papabravo said:
The message title and the text says 16F629, whilst the code says 12F629. You might want to make the correction.

In any case it would not surprise me if the default for the GPIO pins is something else besides general purpose I/O. Look for the definition of the power on default value for each register that affects the GPIO pins.

You must read the datasheet with extraordinary care in order to get it right.

Sorry about that. Not quite sure how I messed that up. I'm using the 8-pin 12F629.

btw, at the end of my Initalize routine, I put 0x00 into GPIO.
 
for a start, you aren't setting any particular delay in the int service routine, so the led is going to be flashing so fast it's doubtful if you will see it...
You are not storing the Oscillator calibration value as set from the factory either with the code you are using, so unless you have read it prior to starting, and noted it down, or your programmer has picked up on it and corrected it for you before you erased the device...
Also, the 629 as opposed to the 627, does not have the a/d convertor...so there's no point in trying to set it, or disable it in your code. If you plan on using the 675 later, comment out the bits of code specific for it, and put them back in again when using it. The Comparator is also switched off after a reset, so unless you wish to use it...

I'll have a look at the datasheet and perhaps post something useful for you...
 
Last edited:
You are interrupting too fast to see your LED blinking.

I modified your code to set the prescaler to 256 and added a counter to count 15 IRQs giving 256*15 uS delay (about 1 second).

Code:
	LIST	p=PIC12F629           ;tell assembler what chip we are using
	include "P12F629.inc"         ;include the defaults for the chip
	__config b'11000110010100'    ;sets the configuration settings (oscillator type etc.)

	cblock 0x20 		      ;start of general purpose registers
Count
	endc
	
	org 0x0000                    ;org sets the origin
	goto Initialize

	org 0x0004
INT
	decfsz	Count,f
	goto	DoneIRQ
	movlw	.15
	movwf	Count
	comf	GPIO, f
DoneIRQ
	bcf	INTCON,T0IF	
	retfie

Initialize

	bcf STATUS, RP0               ;select bank 0
	movlw 0x07
	movwf CMCON                   ;turn comparators off (p.37)

	bsf STATUS, RP0               ;select bank 1
	movlw b'00001000'
	movwf TRISIO

	movlw 0x07;		set prescaller to 256			
	movwf OPTION_REG

	movlw b'10100000'
	movwf INTCON

	bcf STATUS, RP0               ;select bank 0
	movlw 0x00				;set everything off
	movwf GPIO
	clrf TMR0

Infinite
	goto Infinite

	END

If your going to extend this code further I would suggest reading about IRQ context saving (section 9.5).

Mike.
 
I'm embarassed that I'm such an idiot. It is correct that the
LED is just flashing too fast for a slow human like myself to notice.

For the various settings of the pre-scalar rate, I measured the
period of the flashing using a scope and got:

1:2 850 uS
1:4 1750 uS
1:8 3540 uS
1:16 6720 uS

Thanks for all the replies.
 
Ok, here's somthing I wrote which will hopefully clear up a few things for folks completely new to using PIC's and to programming them...

It's a real newbie type approach to stuff, so I don't want folk with plenty of experience using PIC's and "C" critisizing it, you already know how to do this stuff, so you can either add to it, as long as you completely document what you have done, or you can leave it alone. I am well aware that there are loads of ways to do things that achieve the same result, and this piece of code could be well optimised, but for the sake of clarity it has not been, so please don't optimise it, unless for your own personal use, I don't want loads of copies of this all over the place all doing things differently because that will only confuse someone completely new to it all.

Ok, enjoy it for what it's worth...ok..so that didn't work....I'll zip it up...
and try again

I forgot to mention that this was written in assembly, and was compiled and tested in the simulator using the freely available Microchip MPlab version 7.42

ok that's it, if you have any problems using it, or understanding it, let me know here and I'll try to answer your questions....
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top