PDA

View Full Version : A Simple IOC Routine (Interrupt On Change)



lugo.p
- 8th March 2010, 16:26
Hi,

here is a simple Interrupt On Change program, that turns on a led on portC for pic16f684, it cost me a week to find this answer, I only hope that this help to solve problems easier for others...

[code]'configuración

ANSEL = 0 'I/O digitals
OPTION_REG = 7 'Enable pullups
IOCA.0 = 1 'Enable Interrupt on Change for RA0
WPUA.0 = 1 'Enable pull up RA0
TRISC = 0 'portC output
TRISA = 7 'portA <2:0> input <5:3> output

INTCON.0 = 0 'clear IOC flag
INTCON.3 = 1 'Enable IOC interrupt
INTCON.7 = 1 'Enagle GIE
'variables
I var byte 'missmatch variable for IOC
inicio:
if INTCON.0 = 1 THEN 'Change on portA ocurred?
PORTC.5 = 1 'turn On led
pause 500 'wait half second
PORTC.5 = 0 'turn On led
I = PORTA 'end missmatch
INTCON.0 = 0 'clear IOC flag for next interrupt
ELSE 'if no change is ocurred
PORTC.5 = 0 'led continues off
ENDIF '

GOTO inicio 'loop[\code]

PS. Sorry for my English. I can't make my code seccion work

Regards,
Luis Lugo

rmteo
- 8th March 2010, 16:43
Change the "[\code]" to a forward "[/code]".


'configuración

ANSEL = 0 'I/O digitals
OPTION_REG = 7 'Enable pullups
IOCA.0 = 1 'Enable Interrupt on Change for RA0
WPUA.0 = 1 'Enable pull up RA0
TRISC = 0 'portC output
TRISA = 7 'portA <2:0> input <5:3> output

INTCON.0 = 0 'clear IOC flag
INTCON.3 = 1 'Enable IOC interrupt
INTCON.7 = 1 'Enagle GIE
'variables
I var byte 'missmatch variable for IOC
inicio:
if INTCON.0 = 1 THEN 'Change on portA ocurred?
PORTC.5 = 1 'turn On led
pause 500 'wait half second
PORTC.5 = 0 'turn On led
I = PORTA 'end missmatch
INTCON.0 = 0 'clear IOC flag for next interrupt
ELSE 'if no change is ocurred
PORTC.5 = 0 'led continues off
ENDIF '

GOTO inicio 'loop

Acetronics2
- 8th March 2010, 17:12
Hi, Luis

But ... Where's your interrupt stubb gone ???

you should turn INTCON.7 to OFF ... as an interrupt occuring sends you to PAUSE asm routine library ... @ Prog mem location 4 !!!


here you do not use the interrupt system but just flag the Change in I/Os ... and further test the flag.

it would really be a miracle if your program shows you what you were looking for ...

Alain

lugo.p
- 8th March 2010, 17:37
Hi, Luis

But ... Where's your interrupt stubb gone ???

you should turn INTCON.7 to OFF ... as an interrupt occuring sends you to PAUSE asm routine library ... @ Prog mem location 4 !!!


here you do not use the interrupt system but just flag the Change in I/Os ... and further test the flag.

it would really be a miracle if your program shows you what you were looking for ...

Alain

The trick here is that i'm not using normal interrupt, all I do for this is, if a change occurs on RA0 turn on RC5 half second and then turn it off. the GIE turn off when a change on RA0 happen and GIE = 1 again when the missmatch ends.

Only simulate and you'll see

Acetronics2
- 8th March 2010, 17:43
Hi, Luis

I 've already made much worse than that ...

Darrel can confirm !!!

But your code goes straight to the pBp libraries @ location 4 , if you enable GIE ... ( I verified it with MPSIM !!! )

Program pointer passes over ALL the PAUSE library ... luckily it's the only routine here...

DANGER !!!

Alain

rmteo
- 8th March 2010, 18:06
Sitting in a loop while waiting for something to happen (in this case, polling the RAIF flag) isn't really what interrupts are about.

To be really useful, it should actually interrupt whatever your program is currently doing when RA0 changes state. For example, let's say you have a routine the is blinking PORTC.5 at a slow rate of 500mS ON and 500mS OFF continuously in a loop. If the state of RA0 changes (eg. by using a switch or sensor), then you want the blink rate to go faster rate, say 100ms ON and 100ms OFF.

lugo.p
- 8th March 2010, 19:53
Acetronics, i dunno understand, i'm newbie and my english is not excellent, but since i'm going to use IOC for a button and external INT over RA2 for measure a pulse, when i get there, i think i'll see the problem.

Meanwhile if i'm going to use INT over RA2 to measure pulse width, i do need to send first a dummy pulse before the pulse to measure?