这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » C语言编程

共4条 1/1 1 跳转至

C语言编程

院士
2006-09-17 18:14:16     打赏
C语言编程



关键词: 语言编程    

院士
2006-12-22 22:43:00     打赏
2楼
问       #include <reg52.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_3=P1^3;
      sbit    P1_4=P1^4;
      sbit    P2_4=P2^4;
      sbit    P2_5=P2^5;
      sbit    P2_6=P2^6;
      sbit    P2_7=P2^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;
       TH1=0x06;
       TL1=0x06;
       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++)
           nop();
        }
      /*--------------写一字节---------------*/
      void write_byte(uchar mdata)
      {
       uchar i;
       SCK=0;
       nop();
       for(i=0;i<8;i++)
       {
        if(mdata & 0x80) SI=1;
        else SI=0;
        SCK=1;
        mdata=mdata<<1;
        SCK=0;
        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;
        nop();
       }
       return(result);
      }

     /*----------------清看门狗----------外部调用-----*/
     void clr_wchdog(void)
     {
       CS=0;
      _nop_();
      CS=1;
     }
    /*----------------写状态寄存器-----外部调用------*/
    void wreg_5045(uchar mcode)
    {
      CS=0;
      write_byte(WREN);
      CS=1;
      nop();
      CS=0;
      write_byte(WRSR);
      write_byte(mcode);
      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)
      {
        uchar  ch;
        P1_0=0;
        _nop_();
        P1_0=1;
        P0=ch;
        dely(20);
      }
      /*计算*/
     float  cout(uint x,uint y)
     {
       uint x,y;
       float  s;
       float  z;
       z=x/y;             /*------?-------*/
       s=3.87*z;
       return(s);
      }
     /*设备1工作处理程序*/
     void sn()
     {
      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=count(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*/
     {
      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=count(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,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(d|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();
              if(P2.6)
                break;
              }
               }while(1)
        }
       }
"?"处是否正确?是否能够实现?请大家狠批! 1: 不对吧传进来的参数x,y为什么又重新定义了一次呢?很奇怪。 2: 谢了,这个我现在明白了! 3: 还是希望大家多多指教!!! 4: reuint x,y;
       float  s;
       float  z;
       z=x/y;             /*------?-------*/
如果x=3.y=2
不要以為z=1.5 5: 请问coke兄长我该如何处理啊? 6: 变量的类型不同,要类型转换 7: z=(float)x/(float)y;             /*------?-------*/ 8: 谢谢 9: y有没有可能为0啊?

菜鸟
2014-11-14 12:55:00     打赏
3楼

菜鸟
2017-03-23 14:10:58     打赏
4楼
大神c语言这么好,帮忙也程序呗,关于蓝牙定位的,三段程序分主从,然后可以运行得到被测者的位置信息

共4条 1/1 1 跳转至

回复

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