millis() 函数输出使用 Serial.print()
millis() function output using Serial.print()
这是代码
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(millis());
}
这是输出。
15:26:28.813 -> 0 15:26:28.813 -> 0 15:26:28.932 -> 0 15:26:28.932 ->
0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932
-> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 ->
0 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932
-> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 4 15:26:28.932 -> 7 15:26:28.932 -> 10 15:26:28.932 ->
14 15:26:28.932 -> 18 15:26:28.932 -> 22 15:26:28.932 -> 26
15:26:28.932 -> 30 15:26:28.932 -> 34 15:26:28.932 -> 38 15:26:28.932
-> 43 15:26:28.932 -> 47 15:26:28.932 -> 51 15:26:28.932 -> 55 15:26:28.959 -> 59 15:26:28.959 -> 64 15:26:28.959 -> 68 15:26:28.959
-> 72 15:26:28.959 -> 76 15:26:28.959 -> 80 15:26:28.990 -> 84 15:26:28.990 -> 89 15:26:28.990 -> 93 15:26:28.990 -> 97 15:26:28.990
-> 101 15:26:28.990 -> 106 15:26:28.990 -> 111 15:26:29.029 -> 116 15:26:29.029 -> 121 15:26:29.029 -> 126 15:26:29.029 -> 133
15:26:29.029 -> 138 15:26:29.029 -> 143 15:26:29.029 -> 148
15:26:29.029 -> 153 15:26:29.064 -> 158 15:26:29.064 -> 163
15:26:29.064 -> 168 15:26:29.064 -> 174 15:26:29.064 -> 179
15:26:29.064 -> 184 15:26:29.064 -> 189 15:26:29.101 -> 195
15:26:29.101 -> 200 15:26:29.101 -> 205 15:26:29.101 -> 210
15:26:29.101 -> 216 15:26:29.101 -> 221 15:26:29.101 -> 226
15:26:29.136 -> 231 15:26:29.136 -> 236 15:26:29.136 -> 241
15:26:29.136 -> 246 15:26:29.136 -> 251 15:26:29.136 -> 257
15:26:29.136 -> 263 15:26:29.173 -> 268 15:26:29.173 -> 273
15:26:29.173 -> 278 15:26:29.173 -> 283 15:26:29.173 -> 288
15:26:29.173 -> 293 15:26:29.173 -> 299 15:26:29.210 -> 304
15:26:29.210 -> 309 15:26:29.210 -> 314 15:26:29.210 -> 319
15:26:29.210 -> 324 15:26:29.210 -> 330 15:26:29.210 -> 335
15:26:29.248 -> 340 15:26:29.248 -> 346 15:26:29.248 -> 351
15:26:29.248 -> 356 15:26:29.248 -> 361 15:26:29.248 -> 366
15:26:29.248 -> 371 15:26:29.284 -> 376 15:26:29.284 -> 381
15:26:29.284 -> 387 15:26:29.284 -> 392 15:26:29.284 -> 398
15:26:29.284 -> 403 15:26:29.284 -> 408 15:26:29.320 -> 413
15:26:29.320 -> 418 15:26:29.320 -> 423 15:26:29.320 -> 429
15:26:29.320 -> 434 15:26:29.320 -> 439 15:26:29.320 -> 444
15:26:29.354 -> 449 15:26:29.354 -> 454 15:26:29.354 -> 459
15:26:29.354 -> 465 15:26:29.354 -> 471 15:26:29.354 -> 476
15:26:29.387 -> 481 15:26:29.387 -> 486 15:26:29.387 -> 491
15:26:29.387 -> 496 15:26:29.387 -> 501 15:26:29.387 -> 506
15:26:29.387 -> 512 15:26:29.422 -> 517 15:26:29.422 -> 522
15:26:29.422 -> 528 15:26:29.422 -> 533 15:26:29.422 -> 538
15:26:29.422 -> 543 15:26:29.422 -> 548 15:26:29.456 -> 553
15:26:29.456 -> 559 15:26:29.456 -> 564 15:26:29.456 -> 569
15:26:29.456 -> 574 15:26:29.456 -> 579 15:26:29.492 -> 584
15:26:29.492 -> 589 15:26:29.492 -> 595 15:26:29.492 -> 601
15:26:29.492 -> 606 15:26:29.492 -> 611 15:26:29.492 -> 616
15:26:29.529 -> 621 15:26:29.529 -> 626 15:26:29.529 -> 631
15:26:29.529 -> 636 15:26:29.529 -> 642 15:26:29.529 -> 647
15:26:29.529 -> 652 15:26:29.567 -> 657 15:26:29.567 -> 663
15:26:29.567 -> 668 15:26:29.567 -> 673 15:26:29.567 -> 678
15:26:29.567 -> 684 15:26:29.567 -> 689 15:26:29.602 -> 694
15:26:29.602 -> 699 15:26:29.602 -> 704 15:26:29.602 -> 709
15:26:29.602 -> 714 15:26:29.602 -> 719 15:26:29.602 -> 724
15:26:29.638 -> 731 15:26:29.638 -> 736 15:26:29.638 -> 741
15:26:29.638 -> 746 15:26:29.638 -> 751 15:26:29.638 -> 756
15:26:29.638 -> 761 15:26:29.672 -> 766 15:26:29.672 -> 772
15:26:29.672 -> 777 15:26:29.672 -> 782 15:26:29.672 -> 787
15:26:29.672 -> 792 15:26:29.672 -> 798 15:26:29.706 -> 803
15:26:29.706 -> 808 15:26:29.706 -> 814 15:26:29.706 -> 819
15:26:29.706 -> 824 15:26:29.706 -> 829 15:26:29.742 -> 834
15:26:29.742 -> 839 15:26:29.742 -> 844 15:26:29.742 -> 849
15:26:29.742 -> 855
我正在尝试开发一种算法,使用和不使用巨型阵列来存储 3 轴加速度数据以计算在任一轴上行进的距离。
这是我能从 Arduino 那里得到的最准确的时间吗?
此输出根本不准确。
我用错了吗?
为什么在这些间隔之后输出这个定时?
我在哪里可以找到更多相关信息?
Is this the most accurate timing I can get from and Arduino?
还有micros()
。请注意,准确性不是分辨率。
Am I using it wrong?
没有
Why is this timing output after these intervals?
millis()
的输出只是放在内部缓冲区中。在伪代码中,它通常是这样工作的:
char internal_buffer[SOME_SIZE];
void Serial::println(SOME_TYPE stuff) {
if (internal_buffer_is_full()) {
// actively wait for at least space for stuff to place
internal_buffer_wait_for_space_for_stuff(stuff);
}
internal_buffer += stuff; // just add the data to internal buffer
if (!hardware::is_writing()) {
hardware::start_writing(internal_buffer); // writes __asynchronously__!
}
}
首先,所有内容都经过缓冲。然后异步缓冲区被写入输出,主线程继续它的操作。当缓冲区已满时,主线程会主动等待缓冲区中至少有space,然后再将要写入的数据写入缓冲区。预计您会看到短暂的“刷新”,其中 buffered 输出一点点,然后缓冲继续。
如果要刷新缓冲数据,有Serial.flush()
您的测试主要由 USB 串行通信的性能、粒度和不确定时序决定,而不是 millis() 的精度 - 此外,时序由连接的 PC 提供,这不是真实的-time 系统并拥有自己的缓冲行为只会增加另一个变量。
millis()
和 micros()
都与电路板上的时钟源一样准确,典型的 crystal 振荡器的精度约为 50ppm 或 0.005%,陶瓷谐振器可能为 0.5 %,而典型的 TCXO 约为 2ppm(0.0002%)。
在调用这些函数之间执行的任何代码都需要时间,并且 println()
输出到 USB CDC/ACM 串行并在 PC 上捕获等操作将特别不确定。
如果您真的想确认精度,您应该使用更快、更确定的输出(例如数字 IO)并确定引脚范围。例如:
void setup()
{
pinMode(2, OUTPUT);
}
void loop()
{
digitalWrite( 2, millis() & 1 ) ;
}
这将以 1:1 占空比(1ms 低:1ms 高)以 500Hz 切换引脚
这是代码
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(millis());
}
这是输出。
15:26:28.813 -> 0 15:26:28.813 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 0 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 1 15:26:28.932 -> 4 15:26:28.932 -> 7 15:26:28.932 -> 10 15:26:28.932 -> 14 15:26:28.932 -> 18 15:26:28.932 -> 22 15:26:28.932 -> 26 15:26:28.932 -> 30 15:26:28.932 -> 34 15:26:28.932 -> 38 15:26:28.932 -> 43 15:26:28.932 -> 47 15:26:28.932 -> 51 15:26:28.932 -> 55 15:26:28.959 -> 59 15:26:28.959 -> 64 15:26:28.959 -> 68 15:26:28.959 -> 72 15:26:28.959 -> 76 15:26:28.959 -> 80 15:26:28.990 -> 84 15:26:28.990 -> 89 15:26:28.990 -> 93 15:26:28.990 -> 97 15:26:28.990 -> 101 15:26:28.990 -> 106 15:26:28.990 -> 111 15:26:29.029 -> 116 15:26:29.029 -> 121 15:26:29.029 -> 126 15:26:29.029 -> 133 15:26:29.029 -> 138 15:26:29.029 -> 143 15:26:29.029 -> 148 15:26:29.029 -> 153 15:26:29.064 -> 158 15:26:29.064 -> 163 15:26:29.064 -> 168 15:26:29.064 -> 174 15:26:29.064 -> 179 15:26:29.064 -> 184 15:26:29.064 -> 189 15:26:29.101 -> 195 15:26:29.101 -> 200 15:26:29.101 -> 205 15:26:29.101 -> 210 15:26:29.101 -> 216 15:26:29.101 -> 221 15:26:29.101 -> 226 15:26:29.136 -> 231 15:26:29.136 -> 236 15:26:29.136 -> 241 15:26:29.136 -> 246 15:26:29.136 -> 251 15:26:29.136 -> 257 15:26:29.136 -> 263 15:26:29.173 -> 268 15:26:29.173 -> 273 15:26:29.173 -> 278 15:26:29.173 -> 283 15:26:29.173 -> 288 15:26:29.173 -> 293 15:26:29.173 -> 299 15:26:29.210 -> 304 15:26:29.210 -> 309 15:26:29.210 -> 314 15:26:29.210 -> 319 15:26:29.210 -> 324 15:26:29.210 -> 330 15:26:29.210 -> 335 15:26:29.248 -> 340 15:26:29.248 -> 346 15:26:29.248 -> 351 15:26:29.248 -> 356 15:26:29.248 -> 361 15:26:29.248 -> 366 15:26:29.248 -> 371 15:26:29.284 -> 376 15:26:29.284 -> 381 15:26:29.284 -> 387 15:26:29.284 -> 392 15:26:29.284 -> 398 15:26:29.284 -> 403 15:26:29.284 -> 408 15:26:29.320 -> 413 15:26:29.320 -> 418 15:26:29.320 -> 423 15:26:29.320 -> 429 15:26:29.320 -> 434 15:26:29.320 -> 439 15:26:29.320 -> 444 15:26:29.354 -> 449 15:26:29.354 -> 454 15:26:29.354 -> 459 15:26:29.354 -> 465 15:26:29.354 -> 471 15:26:29.354 -> 476 15:26:29.387 -> 481 15:26:29.387 -> 486 15:26:29.387 -> 491 15:26:29.387 -> 496 15:26:29.387 -> 501 15:26:29.387 -> 506 15:26:29.387 -> 512 15:26:29.422 -> 517 15:26:29.422 -> 522 15:26:29.422 -> 528 15:26:29.422 -> 533 15:26:29.422 -> 538 15:26:29.422 -> 543 15:26:29.422 -> 548 15:26:29.456 -> 553 15:26:29.456 -> 559 15:26:29.456 -> 564 15:26:29.456 -> 569 15:26:29.456 -> 574 15:26:29.456 -> 579 15:26:29.492 -> 584 15:26:29.492 -> 589 15:26:29.492 -> 595 15:26:29.492 -> 601 15:26:29.492 -> 606 15:26:29.492 -> 611 15:26:29.492 -> 616 15:26:29.529 -> 621 15:26:29.529 -> 626 15:26:29.529 -> 631 15:26:29.529 -> 636 15:26:29.529 -> 642 15:26:29.529 -> 647 15:26:29.529 -> 652 15:26:29.567 -> 657 15:26:29.567 -> 663 15:26:29.567 -> 668 15:26:29.567 -> 673 15:26:29.567 -> 678 15:26:29.567 -> 684 15:26:29.567 -> 689 15:26:29.602 -> 694 15:26:29.602 -> 699 15:26:29.602 -> 704 15:26:29.602 -> 709 15:26:29.602 -> 714 15:26:29.602 -> 719 15:26:29.602 -> 724 15:26:29.638 -> 731 15:26:29.638 -> 736 15:26:29.638 -> 741 15:26:29.638 -> 746 15:26:29.638 -> 751 15:26:29.638 -> 756 15:26:29.638 -> 761 15:26:29.672 -> 766 15:26:29.672 -> 772 15:26:29.672 -> 777 15:26:29.672 -> 782 15:26:29.672 -> 787 15:26:29.672 -> 792 15:26:29.672 -> 798 15:26:29.706 -> 803 15:26:29.706 -> 808 15:26:29.706 -> 814 15:26:29.706 -> 819 15:26:29.706 -> 824 15:26:29.706 -> 829 15:26:29.742 -> 834 15:26:29.742 -> 839 15:26:29.742 -> 844 15:26:29.742 -> 849 15:26:29.742 -> 855
我正在尝试开发一种算法,使用和不使用巨型阵列来存储 3 轴加速度数据以计算在任一轴上行进的距离。 这是我能从 Arduino 那里得到的最准确的时间吗? 此输出根本不准确。 我用错了吗? 为什么在这些间隔之后输出这个定时? 我在哪里可以找到更多相关信息?
Is this the most accurate timing I can get from and Arduino?
还有micros()
。请注意,准确性不是分辨率。
Am I using it wrong?
没有
Why is this timing output after these intervals?
millis()
的输出只是放在内部缓冲区中。在伪代码中,它通常是这样工作的:
char internal_buffer[SOME_SIZE];
void Serial::println(SOME_TYPE stuff) {
if (internal_buffer_is_full()) {
// actively wait for at least space for stuff to place
internal_buffer_wait_for_space_for_stuff(stuff);
}
internal_buffer += stuff; // just add the data to internal buffer
if (!hardware::is_writing()) {
hardware::start_writing(internal_buffer); // writes __asynchronously__!
}
}
首先,所有内容都经过缓冲。然后异步缓冲区被写入输出,主线程继续它的操作。当缓冲区已满时,主线程会主动等待缓冲区中至少有space,然后再将要写入的数据写入缓冲区。预计您会看到短暂的“刷新”,其中 buffered 输出一点点,然后缓冲继续。
如果要刷新缓冲数据,有Serial.flush()
您的测试主要由 USB 串行通信的性能、粒度和不确定时序决定,而不是 millis() 的精度 - 此外,时序由连接的 PC 提供,这不是真实的-time 系统并拥有自己的缓冲行为只会增加另一个变量。
millis()
和 micros()
都与电路板上的时钟源一样准确,典型的 crystal 振荡器的精度约为 50ppm 或 0.005%,陶瓷谐振器可能为 0.5 %,而典型的 TCXO 约为 2ppm(0.0002%)。
在调用这些函数之间执行的任何代码都需要时间,并且 println()
输出到 USB CDC/ACM 串行并在 PC 上捕获等操作将特别不确定。
如果您真的想确认精度,您应该使用更快、更确定的输出(例如数字 IO)并确定引脚范围。例如:
void setup()
{
pinMode(2, OUTPUT);
}
void loop()
{
digitalWrite( 2, millis() & 1 ) ;
}
这将以 1:1 占空比(1ms 低:1ms 高)以 500Hz 切换引脚