跟踪一下WM_TIMER消息,注意返回值,你会发现变速齿轮的秘密

解决方案 »

  1.   


    Windows的定时器最大分辨率是55毫秒,即每秒钟最多有18次激活定时器。你要0.1毫秒就来一下,那是,......不可能的——哭也没用
      

  2.   

    void Ddelay(unsigned long n)   // 延时函数,n单位:us
    {
    unsigned long Told;
    unsigned long nn,old_Clock, new_Clock, low, high, v_8253;
    unsigned long old_8253, new_8253, int_Time;   nn =(unsigned long)((float)n*1000/840);   // nn单位: 840ns */
    _outp(0x43,0x00);    // 锁定8253计数值
    low = _inp(0x40);     // 读取8253计数值
    high = _inp(0x40);
    v_8253 = low + 256 * high;
    old_8253 = v_8253;
    old_Clock=clock();
    Told=old_Clock;
    int_Time=0;
    while(int_Time < nn)
    {
    _outp(0x43,0x00);
    low = _inp(0x40);
    high = _inp(0x40);
    v_8253 = low + 256 * high;
    new_8253 = v_8253;
    new_Clock=clock();
    if(old_8253< new_8253 && new_Clock-Told< 55) 
    int_Time=(old_8253-new_8253)+
    ((new_Clock-old_Clock)/55+1)*65536lu;
      else 
      int_Time=(old_8253-new_8253)+
      ((new_Clock-old_Clock)/55) * 65536lu;
      Told=new_Clock;
    }
    }
    若步进电机工作频率为400HZ,则发送num个脉冲的程序段如下:
    send_pio=0x00;         // 初始化待发送的数据
    _outp(0x360,0x8a);      // 初始化8255
    for(i=0;i< num;i++){
        send_pio & = 0xfd;  //d0位为脉冲输出位
        _outp(0x366,send_pio);   // 输出低电平
        Ddelay(1250);          // 延时半个周期
        send_pio | = 0x01;    
        _outp(0x366,send_pio);    // 输出高电平
        Ddelay(1250);           //  延时半个周期
    另利用该方法可以求出某段程序代码精确的执行时间。程序段如下:
    unsigned t1,t2,t3,T1,T2,T;
    _outp(0x43,0x00);             // 锁定8253计数值
    t1=_inp(0x40)+256*_inp(0x40);   // 读取8253计数值
    T1=clock(); 
        ……                // 待计时的程序段
    _outp(0x43,0x00);
    t2=_inp(0x40)+256*_inp(0x40);
    T2=clock();
    if(t1< t2 && T2-Told >55)
        t3=t1-t2+(T2-T1+1)*65536;  
        else   t3=t1-t2+(T2-T1)*65536;   
    T=840*t3;                  // T即为程序的执行时间