WinAVR avr/delay函数的用法 在avr GCC的函数库中包有个非常有用的精确延时函数, #include
其中包括4个函数 _delay_loop_1( ); _delay_loop_2( ); _delay_us( ); _delay_ms( ); _delay_loop_1(uint8_t __count); 参数 __count 为8bit 长度,1-256,256表示为0。 每个循环花费3个CPU周期。 所以当晶振为1Mhz时,最长延时为768us(microseconds) _delay_loop_2(uint16_t __count); 参数 __count 为 16bit 长度,1-65536,65536被认为是0。 每个循环花费4个CPU周期。 所以当晶振为1Mhz时,最长延时为262.1 ms (milliseconds) 1*4*65536=262144 us _delay_us(double __us); us的精确延时,参数为double,最长为768 us。 原头文件中定义了晶振频率为1Mhz。在makefile中默认为F_CPU=8000000 #ifndef F_CPU /* prevent compiler error by supplying a default */ # warning "F_CPU not defined for
" # define F_CPU 1000000UL #endif 原函数为: static __inline__ void _delay_us(double __us) { uint8_t __ticks; double __tmp = ((F_CPU) / 3e6) * __us; if (__tmp < 1.0) __ticks = 1; else if (__tmp > 255) __ticks = 0; /* i.e. 256 */ else __ticks = (uint8_t)__tmp; _delay_loop_1(__ticks); } _delay_ms(double __ms); ms的精确延时,参数为double,最长为262.14 ms。
WinAVR avr/delay函数的用法
最新推荐文章于 2019-08-19 11:48:29 发布