[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(13)延时模块精确定时

[复制链接]
1135|2
 楼主| ddllxxrr 发表于 2015-11-17 15:12 | 显示全部楼层 |阅读模式
ASF库模块里有一个,DELAY模块。

该模块定义好了延时函数,这样就很方便我们使用,再也不需要象51那样去计算,直接拿来用就可以了。

  1. **
  2. * [url=home.php?mod=space&uid=82890]@DEF[/url] delay_init
  3. *
  4. * [url=home.php?mod=space&uid=247401]@brief[/url] Initialize the delay driver.
  5. * @param fcpu_hz CPU frequency in Hz
  6. *
  7. * @deprecated
  8. * This function is provided for compatibility with ASF applications that
  9. * may not have been updated to configure the system clock via the common
  10. * clock service; e.g. sysclk_init() and a configuration header file are
  11. * used to configure clocks.
  12. *
  13. * The functions in this module call \ref sysclk_get_cpu_hz() function to
  14. * obtain the system clock frequency.
  15. */
  16. #define delay_init(fcpu_hz)

  17. /**
  18. * @def delay_s
  19. * @brief Delay in seconds.
  20. * @param delay Delay in seconds
  21. */
  22. #define delay_s(delay)      cpu_delay_ms(1000 * delay, F_CPU)

  23. /**
  24. * @def delay_ms
  25. * @brief Delay in milliseconds.
  26. * @param delay Delay in milliseconds
  27. */
  28. #define delay_ms(delay)     cpu_delay_ms(delay, F_CPU)

  29. /**
  30. * @def delay_us
  31. * @brief Delay in microseconds.
  32. * @param delay Delay in microseconds
  33. */
  34. #define delay_us(delay)     cpu_delay_us(delay, F_CPU)

  35. #ifdef __cplusplus
  36. }
  37. #endif

  38. /**
  39. * @}
  40. */

  41. #endif /* _DELAY_H_ */



下午我搞这个程序时,出点小意外。

本来我选择工程是选择开发板的。上来就有board_init();

给我一个错觉就是时钟已经初始化了。

但我错了,本来定1秒程序,跑了25秒,我查了时钟配置,也都对。关键一点一定要加上sysclk_init();

以下是原程,这个程序我就添了一个DELAY模块,顺利通过编译哈:

  1. #include <asf.h>

  2. int main (void)
  3. {
  4.         /* Insert system clock initialization code here (sysclk_init()). */
  5.     uint32_t count;
  6.         sysclk_init();
  7.         board_init();
  8.         //count=sysclk_get_cpu_hz();
  9.          
  10.         delay_init(F_CPU);
  11.         while (1)
  12.         {
  13.                 ioport_set_pin_level(LED0_GPIO, LED0_INACTIVE_LEVEL);
  14.                 delay_ms(1000);
  15.                 ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
  16.                 delay_ms(1000);
  17.         }

  18.         /* Insert application code here, after the board has been initialized. */
  19. }

运行结果是:LED0以1秒一下在改变状态。



ayb_ice 发表于 2015-11-17 15:38 | 显示全部楼层
设置好滴答,通过滴答延时比这个好用
 楼主| ddllxxrr 发表于 2015-11-17 18:07 | 显示全部楼层
ayb_ice 发表于 2015-11-17 15:38
设置好滴答,通过滴答延时比这个好用

您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部