本帖最后由 ddllxxrr 于 2015-10-2 17:32 编辑
今天同上次区别就是一个中断,包括定义中断函数,及初使化中断函数。
也是每隔2秒翻转一下LED0,这个程序也很好形成,在ASF Wizard 加进RTC模块,在ASF EXPLORER查看API函数的EXAMPLE形成程序。
以下是定义16比特模式向上记数:
- void configure_rtc_count(void)
- { struct rtc_count_config config_rtc_count;
- rtc_count_get_config_defaults(&config_rtc_count);
- config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
- config_rtc_count.mode = RTC_COUNT_MODE_16BIT;
- #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
- config_rtc_count.continuously_update = true;
- #endif
- rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
- rtc_count_enable(&rtc_instance);}
下面定义中断个使能:
- void configure_rtc_callbacks(void)
- { rtc_count_register_callback( &rtc_instance, rtc_overflow_callback, RTC_COUNT_CALLBACK_OVERFLOW);
- rtc_count_enable_callback(&rtc_instance, RTC_COUNT_CALLBACK_OVERFLOW);}
以下是中断函数:
- void rtc_overflow_callback(void){
- /* Do something on RTC overflow here */
- port_pin_toggle_output_level(LED_0_PIN);}
整个程序如下:
- #include <asf.h>
- /* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
- # define CONF_CLOCK_OSC32K_ENABLE true
- # define CONF_CLOCK_OSC32K_STARTUP_TIME SYSTEM_OSC32K_STARTUP_130
- # define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT true
- # define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT true
- # define CONF_CLOCK_OSC32K_ON_DEMAND true
- # define CONF_CLOCK_OSC32K_RUN_IN_STANDBY false
- /* Configure GCLK generator 2 (RTC) */
- # define CONF_CLOCK_GCLK_2_ENABLE true
- # define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY false
- # define CONF_CLOCK_GCLK_2_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC32K
- # define CONF_CLOCK_GCLK_2_PRESCALER 32
- # define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE false
- struct rtc_module rtc_instance;
- void configure_rtc_count(void)
- { struct rtc_count_config config_rtc_count;
- rtc_count_get_config_defaults(&config_rtc_count);
- config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
- config_rtc_count.mode = RTC_COUNT_MODE_16BIT;
- #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
- config_rtc_count.continuously_update = true;
- #endif
- rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
- rtc_count_enable(&rtc_instance);}
-
-
- void rtc_overflow_callback(void){
- /* Do something on RTC overflow here */
- port_pin_toggle_output_level(LED_0_PIN);}
-
- void configure_rtc_callbacks(void)
- { rtc_count_register_callback( &rtc_instance, rtc_overflow_callback, RTC_COUNT_CALLBACK_OVERFLOW);
- rtc_count_enable_callback(&rtc_instance, RTC_COUNT_CALLBACK_OVERFLOW);}
-
-
- int main (void)
- {
- system_init();
- /* Configure and enable RTC */ configure_rtc_count();
- /* Configure and enable callback */ configure_rtc_callbacks();
- /* Set period */ rtc_count_set_period(&rtc_instance, 2000);
- /* Insert application code here, after the board has been initialized. */
- /* This skeleton code simply sets the LED to the state of the button. */
- while (true) { /* Infinite while loop */ }
- }
以下是运行时的截图:
|