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++);
}
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.
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.
> 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
>
>
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.
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.
Em 8/6/2012 14:31, William "Chops" Westfield escreveu: {Quote hidden}
> 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.
>
>
>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.
=======================================================================
Em 11/6/2012 07:00, Michael Rigby-Jones escreveu: {Quote hidden}
>
> -----Original Message-----
>
> 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
> From: .....piclist-bouncesKILLspam.....mit.edu [EraseMEpiclist-bouncesspam_OUTTakeThisOuTmit.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
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'.
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.)