11/*
2- * test timer4: show the value for millis() over time and use the
3- * delay() function
2+ * test timer4: show the value for millis() over time and compare
3+ * different impementations for the delay() function
44 */
55
66#include "Arduino.h"
7- #include "HardwareSerial.h"
8- #include "Print.h"
7+ #include "Serial.h"
98
109
1110uint32_t prev = 0 ;
1211
13-
1412void mydelay (unsigned long ms )
13+ #if 1
14+ {
15+ // 88 bytes
16+ uint16_t start ;
17+
18+ start = (uint16_t ) micros ();
19+
20+ while (ms > 0 ) {
21+ while ( (ms > 0 ) && (((uint16_t )micros () - start ) >= 1000 )) {
22+ ms -- ;
23+ start += 1000 ;
24+ }
25+
26+ }
27+ }
28+ #endif
29+ #if 0
30+ {
31+ // 92 bytes
32+ uint16_t start , now ;
33+
34+ start = (uint16_t ) micros ();
35+
36+ while (ms > 0 ) {
37+
38+ now = (uint16_t ) micros ();
39+ while ( (ms > 0 ) && ((now - start ) >= 1000 )) {
40+ ms -- ;
41+ start += 1000 ;
42+ }
43+
44+ }
45+ }
46+ #endif
47+ #if 0
48+ {
49+ // 100 bytes
50+ uint16_t start , passed ;
51+
52+ start = (uint16_t ) micros ();
53+
54+ while (ms > 0 ) {
55+
56+ passed = (uint16_t ) micros () - start ;
57+ while ( (ms > 0 ) && (passed >= 1000 )) {
58+ ms -- ;
59+ passed -= 1000 ;
60+ start += 1000 ;
61+ }
62+
63+ }
64+ }
65+ #endif
66+ #if 0
67+ {
68+ // 159 bytes
69+ uint32_t start , passed ;
70+
71+ start = micros ();
72+ while (ms > 0 ) {
73+ passed = micros () - start ;
74+ while ( (ms > 0 ) && (passed >= 1000 )) {
75+ ms -- ;
76+ passed -= 1000 ;
77+ start += 1000 ;
78+ }
79+ }
80+ }
81+ #endif
82+ #if 0
1583{
84+ // 131 bytes
1685 uint32_t start = micros ();
1786
1887 while (ms > 0 ) {
19- // yield();
20- while ( ms > 0 && (micros () - start ) >= 1000 ) {
88+ while ( (ms > 0 ) && ((micros () - start ) >= 1000 )) {
2189 ms -- ;
2290 start += 1000 ;
2391 }
2492 }
2593}
94+ #endif
95+ #if 0
96+ {
97+ // 131 bytes
98+ uint32_t start = micros ();
2699
100+ while (ms > 0 ) {
101+ while ( ms > 0 && (micros () - start ) >= 1000 ) {
102+ ms -- ;
103+ start += 1000 ;
104+ }
105+ }
106+ }
107+ #endif
27108
28109void setup (void )
29110{
30- HardwareSerial_begin (115200 );
111+ Serial_begin (115200 );
112+
113+ Serial_print_s ("testing delay functions.\n"
114+ "Code size of current impementation: " );
115+ Serial_println_u ((uint16_t )setup - (uint16_t )mydelay );
31116}
32117
33118
@@ -36,14 +121,14 @@ void loop (void)
36121 uint32_t now ;
37122
38123 now = millis ();
39- Print_print_s ("millis()=" );
40- Print_print_u (now );
41- Print_print_s ("\tdelta=" );
42- Print_print_u (now - prev );
43- Print_print_s ("\tmicros()=" );
44- Print_print_u (micros ());
45- Print_print_s ("\tTIM4_CNTR=" );
46- Print_println_u (TIM4 -> CNTR );
124+ Serial_print_s ("millis()=" );
125+ Serial_print_u (now );
126+ Serial_print_s ("\tdelta=" );
127+ Serial_print_u (now - prev );
128+ Serial_print_s ("\tmicros()=" );
129+ Serial_print_u (micros ());
130+ Serial_print_s ("\tTIM4_CNTR=" );
131+ Serial_println_u (TIM4 -> CNTR );
47132
48133 prev = now ;
49134 mydelay (1000 );
0 commit comments