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.

  • 相关阅读:
    异步编程
    写代码写至最有面向对象味道
    GitHub上整理
    用CQRS+ES实现DDD
    前端开发
    让低版本的IE浏览器 强制渲染为IE8 或者 以上 浏览器模式
    NHibernate系列
    hadoop搭建开发环境及编写Hello World
    Linux date -s(转)
    即时编译和打包您的 Groovy 脚本(转)
  • 原文地址:https://www.cnblogs.com/stevenxiu/p/5824704.html
Copyright © 2011-2022 走看看