Searching \ for '[PIC] Help using interrupts in High Tech C & 12F68' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ints.htm?key=interrupt
Search entire site for: 'Help using interrupts in High Tech C & 12F68'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Help using interrupts in High Tech C & 12F68'
2012\06\08@075447 by Ben Barwise

picon face
Hello,

I am trying to do somthing really simple to learn interrupts on PICs
using C but I cant seem to understand how to set bits to enable the
interrupt.

Basically want a button press to change the period of a flashing
light, much like a bike light works, the idea is in the interrupt I
want to make the off period to go to 0 so the light is constantly on
and toggle back, so far I have been trying to make it just change but
I obviously have no idea!

I have been reading the datasheet
ww1.microchip.com/downloads/en/devicedoc/41211d_.pdf

but how the hell do you know what to write if you are not using assembly!


#include<htc.h>


void delay(int timee)
{
int j;
for(j=0;j<timee;j++);
}



void main (void)

{

GPIE = 1;
IOC2 = 1;        
TRISIO0 = 0;
TRISIO1 = 0;
//TRISIO2 = 0;
TRISIO3 = 0;
TRISIO4 = 0;
TRISIO5 = 0;
int off1 = 1000;

while(1)
{

GP0 = 1;
delay(1000);
GP0 = 0;
delay(off1);

}
}

interrupt isr()
{
int off1 = 0;

2012\06\08@082423 by David Meiklejohn

face
flavicon
face
Ben Barwise wrote:
>
> I am trying to do somthing really simple to learn interrupts on PICs
> using C but I cant seem to understand how to set bits to enable the
> interrupt.

Try my tutorials at http://www.gooligum.com.au/tutorials.html

Specifically for interrupts on mid-range PICs (such as the 12F683), see
http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_C_3.pdf

The 12F629 is used in the examples, but interrupts on the 12F683 are exactly
the same (the 12F683 just has more sources available).

I'm in the process of updating my C tutorials to reference the new XC8
compiler, instead of HI-TECH C, but it's just a new name for the same thing,
so the lesson applies to either.


Good luck!

David Meiklejohn
http://www.gooligum.com.au

2012\06\08@113426 by Ben Barwise

picon face
Nice! thats exactly what I was looking for. Cheers!

On 8 June 2012 13:24, David Meiklejohn <spam_OUTdavidTakeThisOuTspamgooligum.com.au> wrote:
{Quote hidden}

>

2012\06\08@133149 by William \Chops\ Westfield

face picon face

On Jun 8, 2012, at 4:54 AM, Ben Barwise wrote:

> interrupt isr()
> {
> int off1 = 0;
> }

You'll need to make off1 a global variable, so that references to that name in the isr and references from the main code actually reference the same data.

Then you'll probably need to make it "volatile" so that the compiler will be warned that it has to keep checking the value, instead of (for instance) moving it into a register at the start of main and just referring to that copy over and over again.

BillW

2012\06\08@142251 by Isaac Marino Bavaresco

flavicon
face
Em 8/6/2012 14:31, William "Chops" Westfield escreveu:
> On Jun 8, 2012, at 4:54 AM, Ben Barwise wrote:
>
>> interrupt isr()
>> {
>> int off1 = 0;
>> }
> You'll need to make off1 a global variable, so that references to that name in the isr and references from the main code actually reference the same data.
>
> Then you'll probably need to make it "volatile" so that the compiler will be warned that it has to keep checking the value, instead of (for instance) moving it into a register at the start of main and just referring to that copy over and over again.
>
> BillW


Do not forget to use a critical section (segment of code with interrupts
disabled) to access off1 from main code, otherwise you may get corrupted
data.


Isaa

2012\06\11@060707 by Michael Rigby-Jones

flavicon
face


-----Original Message-----

Em 8/6/2012 14:31, William "Chops" Westfield escreveu:
{Quote hidden}

From: .....piclist-bouncesKILLspamspam@spam@mit.edu [piclist-bouncesspamKILLspammit.edu] On Behalf Of Isaac Marino Bavaresco
Sent: 08 June 2012 19:23
To: Microcontroller discussion list - Public.
Subject: Re: [PIC] Help using interrupts in High Tech C & 12F683

>Do not forget to use a critical section (segment of code with interrupts
>disabled) to access off1 from main code, otherwise you may get corrupted data.
>
>
>Isaac

If access to the variable is atomic (e.g. it's a single bit or an aligned byte wide value on a PIC) then a critical section should not be needed.

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

2012\06\11@071826 by Isaac Marino Bavaresco

flavicon
face
Em 11/6/2012 07:00, Michael Rigby-Jones escreveu:
{Quote hidden}

Exactly, *atomic* operations don't need to be protected. Unfortunately,
most operations are not atomic, even on single bits or bytes.
For instance: if you need to check the current value and then change it,
then it is not an atomic operation anymore.
Also, his variable is 'int'.


Isaac

2012\06\11@143600 by William \Chops\ Westfield
face picon face

On Jun 11, 2012, at 4:18 AM, Isaac Marino Bavaresco wrote:

> Exactly, *atomic* operations don't need to be protected. Unfortunately,
> most operations are not atomic, even on single bits or bytes.

The operation doesn't need to be any more atomic than the high-level function(s) being performed.   In this case, the main loop code continually reads (only) the variable set in the ISR, so the worst that would happen is that IF the code is interrupted at a particularly bad place (ie when the "int" is half-read), and IF the ISR changes the variable in a way that will result in an incorrect final value after the other half is read, then there will be ONE occurrence of an incorrect off time.

This is probably inconsequential.  The code doesn't need to be a critical section not because it's properly atomic, but because IT ISN'T CRITICAL (in the high-level meaning of the word.)

YMMV, and it's something to be aware of, but…
(note that "delay(0)" is sometimes equivalent to "delay(65536)", so accidentally reading 0 due to atomicity issues might in fact be VERY noticeable.)

BillW

More... (looser matching)
- Last day of these posts
- In 2012 , 2013 only
- Today
- New search...