今天这个delay是大家常用的东东。我在这里得飘扬一下Atmle.
Atmle是我见到过的所有编译环境里边延时函数最方便的一个啦。
根本就不用自己写啊。况这里是SAMD21,更不用自己写了。我记得ATmega16只是把delay.h包含进来就行。
那么,SAMD21又怎么样呢。
首先,新建一个工程,然后选择开发板,自然选D21啦,然后打开AFS Wizard,把DELAY模块加进来。
然后选择ASF EXPLORER找快带开始指导。没有,这次我本应奇怪但反到高兴了,为什么呢????
没有消息就是好消息!!!!!
连个快速指导都没有说明很简单么??????
嘿嘿!!!!!!!有个h 文件,打开。
#ifndef DELAY_H_INCLUDED
#define DELAY_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup group_common_services_delay Busy-Wait Delay Routines
*
* This module provides simple loop-based delay routines for those
* applications requiring a brief wait during execution. Common for
* API ver. 2.
*
* @{
*/
#ifdef SYSTICK_MODE
#include "sam0/systick_counter.h"
#endif
#ifdef CYCLE_MODE
#include "sam0/cycle_counter.h"
#endif
void delay_init(void);
/**
* \def delay_s
* \brief Delay in at least specified number of seconds.
* \param delay Delay in seconds
*/
#define delay_s(delay) cpu_delay_s(delay)
/**
* \def delay_ms
* \brief Delay in at least specified number of milliseconds.
* \param delay Delay in milliseconds
*/
#define delay_ms(delay) cpu_delay_ms(delay)
/**
* \def delay_us
* \brief Delay in at least specified number of microseconds.
* \param delay Delay in microseconds
*/
#define delay_us(delay) cpu_delay_us(delay)
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* DELAY_H_INCLUDED */
可见,调用的CORTEX的内核。那谁敢碰啊。
那么我就直接加进初始化那一项
/*
* Include header files for all drivers that have been imported from
* Atmel Software Framework (ASF).
*/
#include <asf.h>
int main (void)
{
system_init();
delay_init();
// Insert application code here, after the board has been initialized.
// This skeleton code simply sets the LED to the state of the button.
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);
delay_ms(1000);
//} else {
// No, so turn LED off.
port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
delay_us(1000000);
port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
delay_s(1);
//} else {
// No, so turn LED off.
port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
delay_s(3);
//}
}
}
从上边的主程序可以看出,我是检验定时准不准才这么做地。经检查很准。
程序是运行一秒LED亮然后一秒LED灭然后又一秒亮,三秒灭。
好今天就到这里,下次是tick就是系统滴答时钟定时。
|