原创 转灯箱控制器

2008-2-27 17:01 2090 8 8 分类: MCU/ 嵌入式

5b069cba-8071-4ba9-847f-2a83d6c326f9.jpgc8f47f2a-0352-4019-a8f5-8b489002c808.jpg


/******************************************************************
** 文件名:旋转灯箱控制器
** Copyright (c)2003/3 


** 创建人:李军    leejun@qingdaonews.com
** 日 期:2006-1-3
** 修改人:
** 日 期:
** 描 述:    晶震:6mhz
**            芯片:89c51
                  三个接近开关:上限位,下限位,中间停留位
              1)此程序未验证。
              2)顺序编写程序,不是循环运行,程序稳固性有待改进。
* 版 本:01
**----------------------------------------------------------------
******************************************************************
     #include <AT89S51.h>
      #include <absacc.h>
     #include <intrins.h>
     #include <stdio.h>


     #define True   1
     #define Flase  0
     #define UpFlag     1
     #define DownFlag   0
     #define AllStopFlag 0x55
     extern void  timer_ini(void);
     //extern void  delay_ms(unsigned char i);
     //如果上升限位= 0,则到顶了
     #define   NearSwitchUpLimit  P10
     //如果下降限位 = 0,到底了
     #define   NearSwitchDownLimit P16
     //如果中间控位 = 0,是中间一幅。
     #define   NearSwitchMiddleContrl     P13


     #define   RelayUp          P20
     #define   RelayDown          P21
     #define   StopUp            RelayUp = 0;
     #define   StopDown          RelayDown = 0;
     #define   StartUp           RelayUp = 1;
     #define   StartDown         RelayDown = 1;
     led_segment [10]={0x3f,0x30,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};  // led的段码(不带小数点) 0,1,2,3,4,5,6,7,8,9


     uchar  data time_flag;
     uchar  data count,time_count;
     uchar data UpDownStateFlag;   // 上升,下降状态标志位。
                                   //  == 1,  up ;    == 0, down;    ==0x55,stop
  void delay_ms(uchar i)
        {uint j;
          do  {
               do   {
                     j--;
                    }while(j==0);
               i--;
              }while(i==0);


        }


 /**************** sub timer0_ini **********************/
 void timer0_ini(void)
      {
        IE="0x82";  // 只打开T0中断


        TH0=0x00;TL0=0x00;    //2us*50000 = 100s
        TMOD="0x01";       // 定时器0 ,方式1。
        TF0=Flase;
        TR0=True;


        time_flag = 0;
      }
 /*******************中断子程序****************************/


  void tmr0 (void) interrupt 1 using 3     // 0.5ms a interrupt
          {
            static uint i;
            i++;
            TF0=Flase;
            TH0=0;TL0=0;
            if (i > 400)
                {time_flag = 1;
                 i = 0;
                 }


            }


 void clrwdt(void)       // Clear the watchdog ,
 {
        WDTRST = 0x1E;
        WDTRST = 0xE1;
 }


 


 /*********************************************************/


main()
{ uchar temp_i,x,y;   //通用


      clrwdt();
       timer0_ini();
       UpDownStateFlag = 0;
//**************** main program ***************************************
    
       do
       {
              clrwdt();
              if (NearSwitchUpLimit == 0)     // 如果上升限位= 0;
                     { StopUp; StopDown;
                       timer0_ini();            //开始计时
                       while(time_flag ==1)
                            { clrwdt();
                             RelayUp = RelayDown = 0;}  //计时不完,停止运行。 //电机全停止
                            }
                        while(NearSwitchUpLimit != 0)
                             {StartDown;
                             }
                         StartDown;   
                      }  
  //-----------------------------------------------------------------
              if (NearSwitchMiddleContrl == Flase)  //运行到中间时
                     {
                               StopUp; StopDown;    
                               timer0_ini();               //开始计时
                      while(time_flag ==1)
                            { clrwdt();
                             StopUp; StopDown;}  //计时不完,停止运行。 //电机全停止
                            }
                      while( NearSwitchMiddleContrl == True)
                             {StartDown; }
                             StartDown;
                      }


                     
 //-------------------------------------------------------------------


              if ( NearSwitchDownLimit ==0)     //下降传感器感应中
                     { StopDown;
                       timer0_ini();               //开始计时
                         while(time_flag ==1)
                            { clrwdt();
                             StopUp; StopDown;}  //计时不完,停止运行。 //电机全停止
                            }
                         while(NearSwitchDownLimit != 0)
                             { StartUp;}   
                        StartUp;
                   }
//______________________________________________________________________                                 
      if (NearSwitchMiddleContrl == Flase)  //运行到中间时
                     {
                               StopUp; StopDown;   
                               timer0_ini();               //开始计时
                      while(time_flag ==1)
                            { clrwdt();
                             StopUp; StopDown;}  //计时不完,停止运行。 //电机全停止
                            }
                      while( NearSwitchMiddleContrl == True)
                             {StartUp; }
                           
                      StartUp;
                      }                 
 
       }while(1);
   //-----------------------------------------------------------------------
    }

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
8
关闭 站长推荐上一条 /3 下一条