ASF库模块里有一个,DELAY模块。
该模块定义好了延时函数,这样就很方便我们使用,再也不需要象51那样去计算,直接拿来用就可以了。
- **
- * [url=home.php?mod=space&uid=82890]@DEF[/url] delay_init
- *
- * [url=home.php?mod=space&uid=247401]@brief[/url] Initialize the delay driver.
- * @param fcpu_hz CPU frequency in Hz
- *
- * @deprecated
- * This function is provided for compatibility with ASF applications that
- * may not have been updated to configure the system clock via the common
- * clock service; e.g. sysclk_init() and a configuration header file are
- * used to configure clocks.
- *
- * The functions in this module call \ref sysclk_get_cpu_hz() function to
- * obtain the system clock frequency.
- */
- #define delay_init(fcpu_hz)
- /**
- * @def delay_s
- * @brief Delay in seconds.
- * @param delay Delay in seconds
- */
- #define delay_s(delay) cpu_delay_ms(1000 * delay, F_CPU)
- /**
- * @def delay_ms
- * @brief Delay in milliseconds.
- * @param delay Delay in milliseconds
- */
- #define delay_ms(delay) cpu_delay_ms(delay, F_CPU)
- /**
- * @def delay_us
- * @brief Delay in microseconds.
- * @param delay Delay in microseconds
- */
- #define delay_us(delay) cpu_delay_us(delay, F_CPU)
- #ifdef __cplusplus
- }
- #endif
- /**
- * @}
- */
- #endif /* _DELAY_H_ */
下午我搞这个程序时,出点小意外。
本来我选择工程是选择开发板的。上来就有board_init();
给我一个错觉就是时钟已经初始化了。
但我错了,本来定1秒程序,跑了25秒,我查了时钟配置,也都对。关键一点一定要加上sysclk_init();
以下是原程,这个程序我就添了一个DELAY模块,顺利通过编译哈:
- #include <asf.h>
- int main (void)
- {
- /* Insert system clock initialization code here (sysclk_init()). */
- uint32_t count;
- sysclk_init();
- board_init();
- //count=sysclk_get_cpu_hz();
-
- delay_init(F_CPU);
- while (1)
- {
- ioport_set_pin_level(LED0_GPIO, LED0_INACTIVE_LEVEL);
- delay_ms(1000);
- ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
- delay_ms(1000);
- }
- /* Insert application code here, after the board has been initialized. */
- }
运行结果是:LED0以1秒一下在改变状态。
|