打印
[Atmel]

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

[复制链接]
874|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ddllxxrr|  楼主 | 2015-11-17 15:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ASF库模块里有一个,DELAY模块。

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

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

/**
* @def delay_s
* @brief Delay in seconds.
* @param delay Delay in seconds
*/
#define delay_s(delay)      cpu_delay_ms(1000 * delay, F_CPU)

/**
* @def delay_ms
* @brief Delay in milliseconds.
* @param delay Delay in milliseconds
*/
#define delay_ms(delay)     cpu_delay_ms(delay, F_CPU)

/**
* @def delay_us
* @brief Delay in microseconds.
* @param delay Delay in microseconds
*/
#define delay_us(delay)     cpu_delay_us(delay, F_CPU)

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* _DELAY_H_ */



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

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

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

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

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

#include <asf.h>

int main (void)
{
        /* Insert system clock initialization code here (sysclk_init()). */
    uint32_t count;
        sysclk_init();
        board_init();
        //count=sysclk_get_cpu_hz();
         
        delay_init(F_CPU);
        while (1)
        {
                ioport_set_pin_level(LED0_GPIO, LED0_INACTIVE_LEVEL);
                delay_ms(1000);
                ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
                delay_ms(1000);
        }

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

运行结果是: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

2398

主题

6950

帖子

67

粉丝