View previous topic :: View next topic |
Author |
Message |
Slimy
Joined: 25 Nov 2009 Posts: 2
|
Interrupt and variable trouble |
Posted: Wed Nov 25, 2009 3:17 pm |
|
|
I have problems with interrupts. In the handler, the variable is changed but not saved in the future if I use sleep in MAIN () examples.
Code: |
volatile int nRepeatCounter ;
int* pnRepeatCounter = &nRepeatCounter;
void main()
{
init();
enable_interrupts(INT_RB);
*pnRepeatCounter = 4;
sleep();
}
#INT_RB
void buttons()
{
disable_interrupts(INT_RB);
if(!input(DOWN_BUTTON))
{
if(*pnRepeatCounter == 0) *pnRepeatCounter= 100;
(*pnRepeatCounter)--;
}
}
|
nRepeatCounter will already 3! if write volatile int nRepeatCounter = 4 and coment it in main likewise.
If I comment sleep() it normal work. How true save values interrupt from handler? I can't use normal other func without it.
sorry for my bad english. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 25, 2009 3:21 pm |
|
|
Always post your PIC and compiler version. |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 25, 2009 3:30 pm |
|
|
There are a whole suite of problems with what is posted though...
First, there is no while...true in the main, so the code will fall off the end.
Second, port B is not read in the interrupt handler, so if it wasn't for disabling the interrupt, it'd be called for ever.
Third, Port B is not read in the main, so the handler _will_ almost certainly be called as soon as the interrupt is enabled.
I think if these were fixed, the problem would actually disapear, though it is not clear what the 'problem' really is...
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Nov 26, 2009 3:20 am |
|
|
To follow on from Ttelmah
init is not defined.
There are no fuse definitions. (Is the WDT enabled ?)
DOWN_BUTTON is not defined.
Not sure if the settings you are using allow wakeup from an interrupt.
Why are you using a pointer to the variable ?
How are you monitoring the value ? |
|
|
Slimy
Joined: 25 Nov 2009 Posts: 2
|
|
Posted: Thu Nov 26, 2009 4:20 am |
|
|
This is not all code of course all defined. Think problem
will be fix like this
Code: |
while(1)
{
sleep;
}
|
but not sure .
I have next problem. I have a structure
Code: |
typedef struct {
long button : 2; // 2 bit button
long codeLow : 6; // 10 bit code
long codeHigh : 3 ; //
long reverce : 1 ;
long trueButton : 1;
long trueCode : 1 ;
long occupy : 1 ;
}frame_struct;
frame_struct frame; |
Need to convert it in long type where minor bit is button and major occupy. How do it?
I try like this
Code: |
long frameToLong()
{
return (frame.codeHigh << 9) | (frame.codeLow << 2) | frame.button;
}
|
but not sure thats its best way and it will true work.
Who knows what will happen if one of the fields will increase in the cycle of overflow? Will the transfer of the senior field, or simply start with 0 |
|
|
|