电源设计技术实用资料点击免费获取>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 用C编程的请进

共2条 1/1 1 跳转至

用C编程的请进

院士
2006-12-22 22:43:00    评分
用C编程的请进



关键词: 编程     请进    

院士
2006-12-22 22:43:00    评分
2楼
问 #include <reg51.h>
      #include <stdio.h>
      #define  uchar   unsigned char
      #define  uint    unsigned int
      /*#define  nop()    _nop_();_nop_();*/
      /*-----------x5045指令-----------------*/
      #define WREN 0x06 /*写允许*/
      #define WRDI 0x04 /*写禁止*/
      #define RDSR 0x05 /*读状态寄存器*/
      #define WRSR 0x01 /*写状态寄存器*/
      #define READ 0x03 /*读书据*/
      #define WRITE 0x02 /*写数据*/
      sbit    P1_0=P1^0;
      sbit    P1_2=P1^2;
      sbit    P1_3=P1^3;
      sbit    P1_4=P1^4;
      sbit    P1_5=P1^5;
      sbit    P1_6=P1^6;
      sbit    P2_4=P2^4;
      sbit    P2_5=P2^5;
      sbit    P2_6=P2^6;
      sbit    P2_7=P2^7;
      sbit    P3_7=P3^7;
      /*-----------SPI通讯接口-----------------*/
      sbit SI=P3^4;
      sbit SCK=P3^6;
      sbit SO=P3^7;
      sbit CS=P1^7;
      /*全局变量*/
      uchar    a,b,c1,c2;
      uint     tma,tmb,tm0,tm1,e0,e1;
      bit      d0,d1;
      /*外部中断0*/
      void   road0() interrupt 0 using 1
      {
       if(d0==0)    /*是第一次吗?第一次初始化定时器0开始计时*/
       { TH0=0;
         TL0=0;
         d0=1;}
       TR0=1;
       a=++a;       /*计次数*/
       if(a<=2)     /*小于2次记录时间*/
        { tma=tm0;}
       c1=5;        /*每次中断,关闭中断一定时间*/
       EX0=0;
       }
      void   road1() interrupt 2 using 2/*同外部中断0*/
      {
       if(d1==0)
       { TH1=0;
         TL1=0;
         d1=1;}
       TR1=1;
       b=++b;
       if(b<=2)
        { tmb=tm1;}
       c2=5;
       EX1=0;
       }
      /*定时器0中断*/
      void     cut0() interrupt 1 using 1
      {
       TR0=0;
       if(d0==1)   /*只有外部中断产生后才计时*/
         tm0=++tm0;
       TR0=1;
       if(--c1<1)   /*是否放开中断*/
         EX0=1;
       if(--e0==0)  /*超时等待复位*/
       TR0=0;
       while(1);
       }
      void     cut1() interrupt 3 using 2/*同上*/
      {
       TR1=0;
       if(d1==1)
         tm1=++tm1;
       TR1=1;
       if(--c2<1)
         EX1=1;
       if(--e1==0)
       TR1=0;
       while(1);
       }
       void dely(uint y)
       {
        uint i=0;
        for(;i<=y;i++)
         {;}  ;
        }
      /*--------------写一字节---------------*/
      void write_byte(uchar mdata)
      {
       uchar i;
       SCK=0;
       dely(1);/* nop(); */
       for(i=0;i<8;i++)
       {
        if(mdata & 0x80) SI=1;
        else SI=0;
        SCK=1;
        mdata=mdata<<1;
        SCK=0;
        dely(1);/*     nop();   */
       }
      }
      /*-----------读一字节------------------*/
      uchar read_byte(void)
      {
       uchar i,result;
       result=0;
       for(i=0;i<8;i++)
       {
        SCK=1;
        result=result<<1;
        if(SO) result|=0X01;
        SCK=0;
        dely(1);/*nop();*/
       }
       return(result);
      }

     /*----------------清看门狗----------外部调用-----*/
     void clr_wchdog(void)
     {
       CS=0;
      dely(1);/*_nop_();*/
      CS=1;
     }
    /*----------------写状态寄存器-----外部调用------*/
    void wreg_5045(uchar mcode)
    {
      CS=0;
      write_byte(WREN);
      CS=1;
      dely(1);/* nop();*/
      CS=0;
      write_byte(WRSR);
      write_byte(mcode);
      dely(1);/* nop(); */
      CS=1;
      dely(10);
     }
     uchar  read(void)
     {
      uchar   h;
      P2_7=0;
     ;/* _nop_();*/
      P2_7=1;
      while(P2_5);
      h=P2;
      P2_4=0;
      return(h);
      }
      void  trm(uchar ch)
      {
        P1_0=0;
        ;/*  _nop_();*/
        P1_0=1;
        P0=ch;
        dely(20);
      }
      /*计算*/
     float  cout(uint x,uint y)
     {
       float  s;
       float  z;
       z=x/y;             /*------?-------*/
       s=3.87*z;
       return(s);
      }
     /*设备1工作处理程序*/
     void sn()
     {
      uchar  h;
      float  s,t_m;
      while(P1_3)/*设备1进行不到位等待*/
        {
         if(P2_6)/*是否有停止信号*/
           {
            dely(10);
            if(P2_6)
              break;
            }
          }
       t_m=tm0; /*记录设备1的工作时间*/
       do{         /*再次确认*/
          if(P2_6)
          {
           dely(10);
           if(P2_6)
             break;
           }
          }while(1);
        P1_2=1;   /*指示设备1的状态*/
        h=read(); /*读传感器信号*/
        if(a>2)   /*设备1通过的检测点数量*/
          {
           s=cout(tma,t_m);/*计算距离*/
           if(s<1.5)/*进程的预测处理*/
             trm();
           if(a>4)
             trm(  );
           }
         if(a<2)
            trm();
         s=cout(tma,t_m);
         if(s<3.1)
           {
            if(h&0x02)
              trm();
            trm();
            }
         if(s>5.5)
           {
            if(h&0x02)
              trm();
            trm();
             }
         if(h&0x02)
            trm();
         trm();
         }
     void szx()/*设备2,同1*/
     {
       uchar   h;
       float   s,t_n;
      while(P1_4)
        {
         if(P2_6)
           {
            dely(10);
            if(P2_6)
              break;
            }
          }
       t_n=tm1;
       do{
          if(P2_6)
          {
           dely(10);
           if(P2_6)
             break;
           }
          }while(1);
        P3_7=1;
        h=read();
        if(b>2)
          {
           s=cout(tmb,t_n);
           if(s<1.5)
             trm();
           if(b>4)
             trm(  );
           }
         if(b<2)
            trm();
         s=cout(tmb,t_n);
         if(s<3.1)
           {
            if(h&0x01)
              trm();
            trm();
            }
         if(s>5.5)
           {
            if(h&0x01)
              trm();
            trm();
             }
         if(h&0x01)
            trm();
         trm();
         }
     void main()
     {
      P1_2=0;/*开蜂鸣器*/
      dely(200);
      P0=0xff;
      P1=0xff;
      P2=0xff;
      P3=0xff;
      clr_wchdog();
      wreg_5045(0);
      P1_5=0;
      P1_6=0;
      while(1)
     {
      uchar  h,ch;
      uint   t_m=0;
      uint   t_n=0;
      float  s;
      TCON=0x05;
      do{
      a=0;b=0;c1=0;c2=0;tm0=0;tm1=0;tma=0;tmb=0;d0=0;d1=0;/*初始化*/
      e0=400;e1=400;
      TMOD=0x22;/*设置定时器0、1为都方式2*/
      IE=0x8f;}
      while(d0|d1|P2_6);/*等待开始信号*/
      do{
         if(d0&d1)/*是否是2个设备要求同时工作*/
           {
            P1_2=0;/*开状态指示*/
            P3_7=0;
            do{
               if(!P1_3)/*检测设备1的2级信号*/
                {
                 d0=1;
                 t_m=tm0;
                 break;
                 }
                if(!P1_4)/*检测设备2的2级信号*/
                 {
                  d1=1;
                  t_n=tm1;
                  break;
                  }
                if(P2_6)/*检测终止信号*/
                 {
                  dely(10);
                  if(P2_6)
                  break;
                  }
                 }while(1);
            }
          if(d0)/*不是2个设备,是1吗*/
           {
            P1_2=0;
            sn();
            }
          if(d1)/*不是2个设备,是2吗*/
            {
             P3_7=0;
             szx();
             }
          if(P2_6)
             {
              dely(8);
              if(P2_6)
                break;
              }
               }while(1);
        };
       }  

提示:
TO "52" BankArea(0x0000,0x7FFF)
RAMSIZE(256)
*** FATAL ERROR L210: I/O ERROR ON INPUT FILE:
    EXCEPTION 0021H: PATH OR FILE NOT FOUND
    FILE: C:\新建文件夹\C51\LIB\L51_BANK.OBJ
Target not created
1: 请问我该如何修改啊 2: 编译器找不到你包含的头文件 正常的KEIL C路径应该是:C:\keil c\C51\
可能是编译器对“新建文件夹”不识别 3: 用的什么编译器?反正像comp51就不行,文件及路径中不能有中文,也不能超过8个字符 4: 谢谢,各位,请继续指教 5: 换用汇编不就得了省事的做法 ,有精神了做点儿别的事好了 6: 那种不支持长文件名的编译器还是不要用的好rt

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]