zoukankan      html  css  js  c++  java
  • "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.

  • 相关阅读:
    D
    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals
    Mondriaan's Dream POJ
    Traveling by Stagecoach POJ
    HDU1024_Max Sum Plus Plus
    stl_vector去重方法
    POJ3255次短路模板
    springMVC和spring上下文的关系
    @Resource @Autowired 区别
    XPath语法
  • 原文地址:https://www.cnblogs.com/stevenxiu/p/5824704.html
Copyright © 2011-2022 走看看