【问题标题】:Keeping Track of Time With Mbed使用 Mbed 跟踪时间
【发布时间】:2014-06-28 15:36:21
【问题描述】:

我正在使用 mbed 平台在 ARM MCU 上对运动控制器进行编程。 我需要确定 while 循环的每次迭代的时间,但我正在努力想出最好的方法来做到这一点。

我有两种可能的方法:

1) 定义每秒可以完成多少次迭代并使用“等待”,这样每次迭代都会在固定间隔之后发生。然后我可以增加一个计数器来确定时间。

2) 在进入循环之前捕获系统时间,然后不断循环,从原始系统时间中减去当前系统时间来确定时间。

我的思路是正确的还是完全错过了?

【问题讨论】:

    标签: time while-loop iteration mbed


    【解决方案1】:

    您的第一个选项并不理想,因为等待和计数器部分会忽略数字,并且您最终会得到关于迭代的不太准确的信息。

    第二个选项是否可行,具体取决于您的实施方式。 mbed 有一个名为“Timer.h”的库,可以轻松解决您的问题。定时器功能是基于中断的(如果您使用 LPC1768,则使用 Timer3)您可以在此处查看手册:mbed.org/handbook/Timer。 ARM 支持 32 位地址作为 Cortex-M3 处理器的一部分,这意味着定时器是 32 位 int 微秒计数器。这对您的可用性意味着什么,这个库最多可以保持 30 分钟的时间,因此它们非常适合微秒到秒之间的时间(如果需要更多的时间,那么您将需要一个实时时钟)。如果您想知道以毫秒或微秒为单位的计数,这取决于您。如果你想要 micro,你需要调用函数 read_us(),如果你想要 milli,你将使用 read_ms()。使用 Timer 中断会影响您的时间 1-2 微秒,因此如果您希望跟踪到该级别而不是毫秒,则必须牢记这一点。

    这是您尝试完成的示例代码(基于 LPC1768 并使用在线编译器编写):

    #include "mbed.h" 
    #include "Timer.h"
    Timer timer;
    
    Serial device (p9,p10);
    
    
    
    int main() {  
        device.baud(19200); //setting baud rate  
        int my_num=10; //number of loops in while  
        int i=0;  
        float sum=0;  
        float dev=0;  
        float num[my_num];  
        float my_time[my_num]; //initial values of array set to zero  
        for(int i=0; i<my_num; i++)  
        {  
            my_time[i]=0; //initialize array to 0  
        }  
    
    timer.start(); //start timer  
     while (i < my_num) //collect information on timing  
     {  
        printf("Hello World\n");  
        i++;  
        my_time[i-1]=timer.read_ms(); //needs to be the last command before loop restarts to be more accurate  
     }  
    timer.stop(); //stop timer  
    sum=my_time[0]; //set initial value of sum to first time input  
    
    for(i=1; i < my_num; i++)  
    {  
        my_time[i]=my_time[i]-my_time[i-1]; //making the array hold each loop time  
        sum=sum+my_time[i]; //sum of times for mean and standard deviation  
    }  
    sum = sum/my_num; //sum of times is now the mean so as to not waste memory  
    
    device.printf("Here are the times for each loop: \n");  
    for(i=0; i<my_num; i++)  
    {  
        device.printf("Loop %d: %.3f\n", i+1, my_time[i]);  
    }   
    
    device.printf("Your average loop time is %.3f ms\n", sum);  
        for(int i=0; i<my_num; i++)  
        {  
            num[i]= my_time[i]-sum;  
            dev = dev +(num[i])*(num[i]);  
        }  
        dev = sqrt(dev/(my_num-1)); //dev is now the value of the standard deviation  
        device.printf("The standard deviation of your loops is %.3f ms\n", dev);  
    
      return 0;  
    }  
    

    您可以使用的另一个选项是 SysTick 定时器功能,它可以实现类似于上面看到的功能,它可以使您的代码更容易移植到任何具有 Cortex-Mx 的 ARM 设备,因为它基于微处理器的系统定时器(在这里阅读更多:http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/Babieigh.html)。这实际上取决于您希望项目的精确度和便携性!

    原文来源:http://community.arm.com/groups/embedded/blog/2014/09/05/intern-inquiry-95

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      相关资源
      最近更新 更多