[Atmel] 用ASF跑SAMD21程序(1)8位定时器点LED

[复制链接]
 楼主| ddllxxrr 发表于 2014-12-16 16:14 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-12-16 18:26 编辑

ASF大家已经不莫生了,它是Atmel的编程库,但我发现很少用起来地,从今天开始,我就一步步从SAMD21这块板子跑起,
用8位定时器点LED先.

首先,选择新建ASF工程


然后点选开发板,注意了,不选开发板有些管脚还得重定义,比如LED0
选择手里的开发板

工程名字可心默认,也可自己改名,然后选择我建的工程名:

这时ASF向导出现了.

选则TC回调函数方式:

然后点选APPLY,这时用ASF exploer 可以选则快速开始那一项,则按照网页的提示把代码搞好.

现注意改一下时钟设置,这个是关键也是同STM32不同的地方




以下是主程序:
  1. #include <asf.h>

  2. void configure_tc(void);
  3. void configure_tc_callbacks(void);
  4. void tc_callback_to_toggle_led(
  5.                 struct tc_module *const module_inst);


  6. //! [module_inst]
  7. struct tc_module tc_instance;
  8. //! [module_inst]

  9. //! [callback_funcs]
  10. void tc_callback_to_toggle_led(
  11.                 struct tc_module *const module_inst)
  12. {
  13.         port_pin_toggle_output_level(LED0_PIN);
  14. }
  15. //! [callback_funcs]

  16. //! [setup]
  17. void configure_tc(void)
  18. {
  19.         //! [setup_config]
  20.         struct tc_config config_tc;
  21.         //! [setup_config]
  22.         //! [setup_config_defaults]
  23.         tc_get_config_defaults(&config_tc);
  24.         //! [setup_config_defaults]

  25.         //! [setup_change_config]
  26.         config_tc.counter_size = TC_COUNTER_SIZE_8BIT;
  27.         config_tc.clock_source = GCLK_GENERATOR_1;
  28.         config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV1024;
  29.         config_tc.counter_8_bit.period = 100;
  30.         config_tc.counter_8_bit.compare_capture_channel[0] = 50;
  31.         config_tc.counter_8_bit.compare_capture_channel[1] = 54;
  32.         //! [setup_change_config]

  33.         //! [setup_set_config]
  34.         tc_init(&tc_instance, TC3, &config_tc);
  35.         //! [setup_set_config]

  36.         //! [setup_enable]
  37.         tc_enable(&tc_instance);
  38.         //! [setup_enable]
  39. }

  40. void configure_tc_callbacks(void)
  41. {
  42.         //! [setup_register_callback]
  43.         tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
  44.                         TC_CALLBACK_OVERFLOW);
  45.         //tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
  46.         //                TC_CALLBACK_CC_CHANNEL0);
  47.         //tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
  48.         //                TC_CALLBACK_CC_CHANNEL1);
  49.         //! [setup_register_callback]

  50.         //! [setup_enable_callback]
  51.         tc_enable_callback(&tc_instance, TC_CALLBACK_OVERFLOW);
  52.         //tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
  53.         //tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL1);
  54.         //! [setup_enable_callback]
  55. }
  56. //! [setup]

  57. int main(void)
  58. {
  59.         system_init();

  60. //! [setup_init]
  61.         configure_tc();
  62.         configure_tc_callbacks();
  63. //! [setup_init]

  64. //! [main]
  65.         //! [enable_global_interrupts]
  66.         system_interrupt_enable_global();
  67.         //! [enable_global_interrupts]

  68.         //! [main_loop]
  69.         while (true) {
  70.         }
  71.         //! [main_loop]
  72. //! [main]
  73. }

时钟配置:

  1. /**
  2. * \file
  3. *
  4. * \brief SAM D21 Clock configuration
  5. *
  6. * Copyright (C) 2013-2014 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. *    this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. *    this list of conditions and the following disclaimer in the documentation
  20. *    and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. *    from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. *    Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. #include <clock.h>

  44. #ifndef CONF_CLOCKS_H_INCLUDED
  45. #  define CONF_CLOCKS_H_INCLUDED

  46. /* System clock bus configuration */
  47. #  define CONF_CLOCK_CPU_CLOCK_FAILURE_DETECT     false
  48. #  define CONF_CLOCK_FLASH_WAIT_STATES            0
  49. #  define CONF_CLOCK_CPU_DIVIDER                  SYSTEM_MAIN_CLOCK_DIV_1
  50. #  define CONF_CLOCK_APBA_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1
  51. #  define CONF_CLOCK_APBB_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1

  52. /* SYSTEM_CLOCK_SOURCE_OSC8M configuration - Internal 8MHz oscillator */
  53. #  define CONF_CLOCK_OSC8M_PRESCALER              SYSTEM_OSC8M_DIV_1
  54. #  define CONF_CLOCK_OSC8M_ON_DEMAND              true
  55. #  define CONF_CLOCK_OSC8M_RUN_IN_STANDBY         false

  56. /* SYSTEM_CLOCK_SOURCE_XOSC configuration - External clock/oscillator */
  57. #  define CONF_CLOCK_XOSC_ENABLE                  false
  58. #  define CONF_CLOCK_XOSC_EXTERNAL_CRYSTAL        SYSTEM_CLOCK_EXTERNAL_CRYSTAL
  59. #  define CONF_CLOCK_XOSC_EXTERNAL_FREQUENCY      12000000UL
  60. #  define CONF_CLOCK_XOSC_STARTUP_TIME            SYSTEM_XOSC_STARTUP_32768
  61. #  define CONF_CLOCK_XOSC_AUTO_GAIN_CONTROL       true
  62. #  define CONF_CLOCK_XOSC_ON_DEMAND               true
  63. #  define CONF_CLOCK_XOSC_RUN_IN_STANDBY          false

  64. /* SYSTEM_CLOCK_SOURCE_XOSC32K configuration - External 32KHz crystal/clock oscillator */
  65. #  define CONF_CLOCK_XOSC32K_ENABLE               false
  66. #  define CONF_CLOCK_XOSC32K_EXTERNAL_CRYSTAL     SYSTEM_CLOCK_EXTERNAL_CRYSTAL
  67. #  define CONF_CLOCK_XOSC32K_STARTUP_TIME         SYSTEM_XOSC32K_STARTUP_65536
  68. #  define CONF_CLOCK_XOSC32K_AUTO_AMPLITUDE_CONTROL  false
  69. #  define CONF_CLOCK_XOSC32K_ENABLE_1KHZ_OUPUT    false
  70. #  define CONF_CLOCK_XOSC32K_ENABLE_32KHZ_OUTPUT  true
  71. #  define CONF_CLOCK_XOSC32K_ON_DEMAND            true
  72. #  define CONF_CLOCK_XOSC32K_RUN_IN_STANDBY       false

  73. /* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
  74. #  define CONF_CLOCK_OSC32K_ENABLE                false
  75. #  define CONF_CLOCK_OSC32K_STARTUP_TIME          SYSTEM_OSC32K_STARTUP_130
  76. #  define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT    true
  77. #  define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT   true
  78. #  define CONF_CLOCK_OSC32K_ON_DEMAND             true
  79. #  define CONF_CLOCK_OSC32K_RUN_IN_STANDBY        false

  80. /* SYSTEM_CLOCK_SOURCE_DFLL configuration - Digital Frequency Locked Loop */
  81. #  define CONF_CLOCK_DFLL_ENABLE                  false
  82. #  define CONF_CLOCK_DFLL_LOOP_MODE               SYSTEM_CLOCK_DFLL_LOOP_MODE_OPEN
  83. #  define CONF_CLOCK_DFLL_ON_DEMAND               false

  84. /* DFLL open loop mode configuration */
  85. #  define CONF_CLOCK_DFLL_COARSE_VALUE            (0x1f / 4)
  86. #  define CONF_CLOCK_DFLL_FINE_VALUE              (0xff / 4)

  87. /* DFLL closed loop mode configuration */
  88. #  define CONF_CLOCK_DFLL_SOURCE_GCLK_GENERATOR   GCLK_GENERATOR_1
  89. #  define CONF_CLOCK_DFLL_MULTIPLY_FACTOR         6
  90. #  define CONF_CLOCK_DFLL_QUICK_LOCK              true
  91. #  define CONF_CLOCK_DFLL_TRACK_AFTER_FINE_LOCK   true
  92. #  define CONF_CLOCK_DFLL_KEEP_LOCK_ON_WAKEUP     true
  93. #  define CONF_CLOCK_DFLL_ENABLE_CHILL_CYCLE      true
  94. #  define CONF_CLOCK_DFLL_MAX_COARSE_STEP_SIZE    (0x1f / 4)
  95. #  define CONF_CLOCK_DFLL_MAX_FINE_STEP_SIZE      (0xff / 4)

  96. /* SYSTEM_CLOCK_SOURCE_DPLL configuration - Digital Phase-Locked Loop */
  97. #  define CONF_CLOCK_DPLL_ENABLE                  false
  98. #  define CONF_CLOCK_DPLL_ON_DEMAND               true
  99. #  define CONF_CLOCK_DPLL_RUN_IN_STANDBY          false
  100. #  define CONF_CLOCK_DPLL_LOCK_BYPASS             false
  101. #  define CONF_CLOCK_DPLL_WAKE_UP_FAST            false
  102. #  define CONF_CLOCK_DPLL_LOW_POWER_ENABLE        false

  103. #  define CONF_CLOCK_DPLL_LOCK_TIME               SYSTEM_CLOCK_SOURCE_DPLL_LOCK_TIME_NO_TIMEOUT
  104. #  define CONF_CLOCK_DPLL_REFERENCE_CLOCK         SYSTEM_CLOCK_SOURCE_DPLL_REFERENCE_CLOCK_REF0
  105. #  define CONF_CLOCK_DPLL_FILTER                  SYSTEM_CLOCK_SOURCE_DPLL_FILTER_DEFAULT

  106. #  define CONF_CLOCK_DPLL_REFERENCE_FREQUENCY     32768
  107. #  define CONF_CLOCK_DPLL_REFEREMCE_DIVIDER       1
  108. #  define CONF_CLOCK_DPLL_OUTPUT_FREQUENCY        48000000

  109. /* Set this to true to configure the GCLK when running clocks_init. If set to
  110. * false, none of the GCLK generators will be configured in clocks_init(). */
  111. #  define CONF_CLOCK_CONFIGURE_GCLK               true

  112. /* Configure GCLK generator 0 (Main Clock) */
  113. #  define CONF_CLOCK_GCLK_0_ENABLE                true
  114. #  define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY        false
  115. #  define CONF_CLOCK_GCLK_0_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  116. #  define CONF_CLOCK_GCLK_0_PRESCALER             1
  117. #  define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE         false

  118. /* Configure GCLK generator 1 */
  119. #  define CONF_CLOCK_GCLK_1_ENABLE                true
  120. #  define CONF_CLOCK_GCLK_1_RUN_IN_STANDBY        false
  121. #  define CONF_CLOCK_GCLK_1_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  122. #  define CONF_CLOCK_GCLK_1_PRESCALER             1
  123. #  define CONF_CLOCK_GCLK_1_OUTPUT_ENABLE         false

  124. /* Configure GCLK generator 2 (RTC) */
  125. #  define CONF_CLOCK_GCLK_2_ENABLE                false
  126. #  define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY        false
  127. #  define CONF_CLOCK_GCLK_2_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC32K
  128. #  define CONF_CLOCK_GCLK_2_PRESCALER             32
  129. #  define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE         false

  130. /* Configure GCLK generator 3 */
  131. #  define CONF_CLOCK_GCLK_3_ENABLE                false
  132. #  define CONF_CLOCK_GCLK_3_RUN_IN_STANDBY        false
  133. #  define CONF_CLOCK_GCLK_3_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  134. #  define CONF_CLOCK_GCLK_3_PRESCALER             1
  135. #  define CONF_CLOCK_GCLK_3_OUTPUT_ENABLE         false

  136. /* Configure GCLK generator 4 */
  137. #  define CONF_CLOCK_GCLK_4_ENABLE                false
  138. #  define CONF_CLOCK_GCLK_4_RUN_IN_STANDBY        false
  139. #  define CONF_CLOCK_GCLK_4_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  140. #  define CONF_CLOCK_GCLK_4_PRESCALER             1
  141. #  define CONF_CLOCK_GCLK_4_OUTPUT_ENABLE         false

  142. /* Configure GCLK generator 5 */
  143. #  define CONF_CLOCK_GCLK_5_ENABLE                false
  144. #  define CONF_CLOCK_GCLK_5_RUN_IN_STANDBY        false
  145. #  define CONF_CLOCK_GCLK_5_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  146. #  define CONF_CLOCK_GCLK_5_PRESCALER             1
  147. #  define CONF_CLOCK_GCLK_5_OUTPUT_ENABLE         false

  148. /* Configure GCLK generator 6 */
  149. #  define CONF_CLOCK_GCLK_6_ENABLE                false
  150. #  define CONF_CLOCK_GCLK_6_RUN_IN_STANDBY        false
  151. #  define CONF_CLOCK_GCLK_6_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  152. #  define CONF_CLOCK_GCLK_6_PRESCALER             1
  153. #  define CONF_CLOCK_GCLK_6_OUTPUT_ENABLE         false

  154. /* Configure GCLK generator 7 */
  155. #  define CONF_CLOCK_GCLK_7_ENABLE                false
  156. #  define CONF_CLOCK_GCLK_7_RUN_IN_STANDBY        false
  157. #  define CONF_CLOCK_GCLK_7_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  158. #  define CONF_CLOCK_GCLK_7_PRESCALER             1
  159. #  define CONF_CLOCK_GCLK_7_OUTPUT_ENABLE         false

  160. #endif /* CONF_CLOCKS_H_INCLUDED */



本帖子中包含更多资源

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

×
86269838 发表于 2017-3-16 15:03 | 显示全部楼层
斑斑又有问题了。我在main(void)加了按键点亮LED的中断,如下:

while (1)
        {
                /* Is button pressed? */
                if (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE) {
                        /* Yes, so turn LED on. */
                        port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
                        } else {
                        /* No, so turn LED off. */
                        port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
                }

你的code里面while循环里面是空的。这时我的板子用按键可以亮灯,但是利用counter中断产生的周期闪烁的亮度非常小,几乎不可见,但还是有亮,我改counter的period也有变化。这时亮度很低是为什么?
86269838 发表于 2017-3-16 15:07 | 显示全部楼层
本帖最后由 86269838 于 2017-3-16 15:09 编辑

我看波形是有个很小的pulse过来,频率和我设的period一致。在没有这个按键中断的时候应该是能保持一段时间。嗷,我懂了,是进入下一个while循环通过判断按键的状态把LED的active状态给覆盖了。
zwsam 发表于 2022-2-21 11:26 | 显示全部楼层
不错的资料!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2403

主题

6995

帖子

68

粉丝
快速回复 返回顶部 返回列表