This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can variable be accessed in interrupt service function?Simple question!

Problem:
I use timer interrup to send data to ASC.In timer interrupt function if do like this (eg. S0TBUF = 0xXX),it works.but if do like this (eg. S0TBUF = data2send),There's always 0xff in S0TBUF.My routine has three parts: main.c gt1.c asc.c,variable data2send is defined in gt1.c. So i doubt that data2send can not be accessed in GT1 TIMER3 service routine.

// gt1.c
#include "MAIN.H"
#include "intrins.h"


static unsigned char data2send = 0xfe;

void GT1_vInit(void)
{
  T3CON = 0x0006;
  T3    = 0x6768;  //  load timer 3 register

  T3IC = 0x005E;

  T2CON = 0x0000;
  T2    = 0x0000;  //  load timer 2 register

  T4CON = 0x0027;
  T4    = 0x6768;  //  load timer 4 register


  T3R = 1;         //   set timer 3 run bit 
}


void GT1_viIsrTmr3(void) interrupt T3INT
{
    data2send = _crol_(data2send,1);
	S0TBUF = data2send;

}

  • Whenever you share write access between ISR and non-ISR code to a variable you must declare the variable volatile.

    volatile static unsigned char data2send;
    Don't give this an initial compile time variable as it adds to the size of your ROM image. I suspect this value changes often so why initialize it at compile time?

    Remember, you cannot really read back the value of Tx_S0BUF since it points to Rx_S0BUF on reads.

  • I have raveled out the problem,the point is that DAvE 2.0 does not config ADDRSELx and BUSCONx for keil correctly,so routine can't access external ram.After I initiated ADDRSEL1 and BUSCON1 my program worked well.

    Thank you for your help,Mark Odell

  • You should not need Dave. Are you an embedded developer or not? :-)

    I'm sure you can add your own init. code to the Dave generated code to set up these extra bits.