[Atmel] 用ASF跑SAMD21程序(14)delay

[复制链接]
2485|0
 楼主| ddllxxrr 发表于 2015-1-7 20:23 | 显示全部楼层 |阅读模式
今天这个delay是大家常用的东东。我在这里得飘扬一下Atmle.

Atmle是我见到过的所有编译环境里边延时函数最方便的一个啦。

根本就不用自己写啊。况这里是SAMD21,更不用自己写了。我记得ATmega16只是把delay.h包含进来就行。

那么,SAMD21又怎么样呢。

首先,新建一个工程,然后选择开发板,自然选D21啦,然后打开AFS Wizard,把DELAY模块加进来。

然后选择ASF EXPLORER找快带开始指导。没有,这次我本应奇怪但反到高兴了,为什么呢????

没有消息就是好消息!!!!!

连个快速指导都没有说明很简单么??????

嘿嘿!!!!!!!有个h 文件,打开。
  1. #ifndef DELAY_H_INCLUDED
  2. #define DELAY_H_INCLUDED

  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. /**
  7. * @defgroup group_common_services_delay Busy-Wait Delay Routines
  8. *
  9. * This module provides simple loop-based delay routines for those
  10. * applications requiring a brief wait during execution. Common for
  11. * API ver. 2.
  12. *
  13. * @{
  14. */

  15. #ifdef SYSTICK_MODE
  16. #include "sam0/systick_counter.h"
  17. #endif
  18. #ifdef CYCLE_MODE
  19. #include "sam0/cycle_counter.h"
  20. #endif

  21. void delay_init(void);

  22. /**
  23. * \def delay_s
  24. * \brief Delay in at least specified number of seconds.
  25. * \param delay Delay in seconds
  26. */
  27. #define delay_s(delay)          cpu_delay_s(delay)

  28. /**
  29. * \def delay_ms
  30. * \brief Delay in at least specified number of milliseconds.
  31. * \param delay Delay in milliseconds
  32. */
  33. #define delay_ms(delay)         cpu_delay_ms(delay)

  34. /**
  35. * \def delay_us
  36. * \brief Delay in at least specified number of microseconds.
  37. * \param delay Delay in microseconds
  38. */
  39. #define delay_us(delay)         cpu_delay_us(delay)

  40. #ifdef __cplusplus
  41. }
  42. #endif

  43. /**
  44. * @}
  45. */

  46. #endif /* DELAY_H_INCLUDED */

可见,调用的CORTEX的内核。那谁敢碰啊。
那么我就直接加进初始化那一项
  1. /*
  2. * Include header files for all drivers that have been imported from
  3. * Atmel Software Framework (ASF).
  4. */
  5. #include <asf.h>

  6. int main (void)
  7. {
  8.         system_init();
  9.        
  10.         delay_init();

  11.         // Insert application code here, after the board has been initialized.

  12.         // This skeleton code simply sets the LED to the state of the button.
  13.         while (1) {
  14.                 // Is button pressed?
  15.                 //if (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE) {
  16.                         // Yes, so turn LED on.
  17.                         port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
  18.                         delay_ms(1000);
  19.                 //} else {
  20.                         // No, so turn LED off.
  21.                         port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
  22.                         delay_us(1000000);
  23.                        
  24.                         port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
  25.                         delay_s(1);
  26.                         //} else {
  27.                         // No, so turn LED off.
  28.                         port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
  29.                         delay_s(3);
  30.                        
  31.                 //}
  32.         }
  33. }
从上边的主程序可以看出,我是检验定时准不准才这么做地。经检查很准。



程序是运行一秒LED亮然后一秒LED灭然后又一秒亮,三秒灭。


好今天就到这里,下次是tick就是系统滴答时钟定时。





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

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

2404

主题

7001

帖子

68

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