"static variable" defined in a function

 

When I read codes of single chip, I find the following Timer Interrupt Function:

 

void Timer_1(void) interrupt 3
{	 
    static uint16 ms_count=0;
	static uchar _100us_count=0;
	//TH1     =   0xA9;   
    //TL1     =   0x9A;

	Uart_timeout_count++;
	if((Uart_timeout_count == 1000) && (rec_flag)) //´®¿Ú½ÓÊÕ1Ã볬ʱ
	{
		rec_flag = 0;
	}

	if(ms_count++ >= 1000)
	{
	     ms_count = 0;
		 if(rec_flag)
		 {
		     LED5 = ~LED5;
		 }
		 else
		 {
		     LED5 = 1;
		 }
	}
	 

 

	 TH1     =   0xF7;   
     TL1     =   0x5C; //10KHZƵÂʶ¨Ê±

	 _100us_count++;

	 if(_100us_count <= Left_Speed) //×ó²àÕ¼¿Õ±È£¬1KHz
	 {
	     MOTOR_A_EN = 1;
	 }
	 else
	 {
	     MOTOR_A_EN = 0;
	 }

	 if(_100us_count <= Right_Speed)	//ÓÒ²àÕ¼¿Õ±È£¬1KHz
	 {
	     MOTOR_B_EN = 1;
	 }
	 else
	 {
	     MOTOR_B_EN = 0;
	 }

	 if(10 == _100us_count)
	 {
	      _100us_count = 0;
		  MOTOR_A_EN = 0;
		  MOTOR_B_EN = 0;
	 }
	 	
}

  

static uint16 ms_count=0;
static uchar _100us_count=0;

 

These two variables will be defined only once.

After following call, it points to the same addresses.

 

Actually we can make use of Global Variable to realize the same effect.

 

But Global Variable is dangerous and vulnerable to be changed out of this effective region.

Even if you defined a Static Global Variable only in this .obj. You have protected from changed by other .obj. However, it is still vulnerable to be changed in this very .obj by other functions.

 

So "static" is local.

"static" is proprietary.

 

posted @ 2016-08-31 09:43  steven_xiu  阅读(225)  评论(0编辑  收藏  举报