View previous topic :: View next topic |
Author |
Message |
nash7 Guest
|
interrupts |
Posted: Tue Jul 19, 2005 11:44 am |
|
|
does anybody knows if you can change a general variable in a interrupts so you can use it in the main program?and how you can make that?
Thanks a lot... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jul 19, 2005 1:08 pm |
|
|
If the variable is declared outside/before main() then the scope will be global and it can be used in a ISR.
Code: |
#include <18f452.h>
#case
#use delay(clock=16000000)
#fuses hs,nowdt,noprotect,nolvp
#use rs232(baud=19200,xmit=PIN_B4,invert,stream=DEBUG)
#zero_ram
BOOLEAN idle_bus;
//---------------- MAIN --------------------
void main(void)
{
idle_bus=FALSE;
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
fprintf(DEBUG,"Starting\n\r");
while(1)
{
fprintf(DEBUG,".");
if(idle_bus)
{
fprintf(DEBUG,"Bus is idle\n\r");
idle_bus=FALSE;
}
}
}
//=======================timer 1 ISR============================//
#INT_TIMER1 // This is called when timer 1 overflows
void timer1_isr()
{
idle_bus=TRUE;
}
|
|
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Tue Jul 19, 2005 1:29 pm |
|
|
Keep in mind that when you do use a global variable, especially a 16-bit variable, that it takes more than one assembly instruction to access it. In other words, access to that variable is not "atomic".
So when I do access 16-bit variables outside an isr, I disable interrupts briefly.
e.g.
Code: |
disable_interrupts(GLOBAL);
if (GLOBAL_my_16bit_var < 2000)
{
...
}
enable_interrupts(GLOBAL);
|
The reason why I disable interrupts is because the interrupt can hit anywhere in the assembly code while in the 'middle' of the if() statement above, causing unexpected results. |
|
|
nash7 Guest
|
|
Posted: Wed Jul 20, 2005 3:55 am |
|
|
thanks a lot, I had a mistake and I thought that maybe it was because of the interrupt's variable, and it wasn't as you told me...
thanks again |
|
|
|