当前位置:首页 > 单片机 > 单片机
[导读]/************************************************************ 函数库说明:ATMEGE8 延迟库函数* 版本: v1.0 * **

/***********************************************************

* 函数库说明:ATMEGE8 延迟库函数

* 版本: v1.0

*

************************************************************

*注意: LED PC5

***********************************************************/

#include

//定义外部晶振

#define F_CPU 6000000UL

//延迟包含头文件

#include

//函数声明

void Delay_s(int ss);

int main(void)

{

//LED等PC5设置为输出

DDRC |= (1 << DDC5);

//起始PC5输出高电平,LED不亮

PORTC |= (1 << PC5);

while(1)

{

//取反

PORTC ^= (1 << PC5);

//延迟1s

Delay_s(1);

}

return 0;

}

/***********************************************************

** 名称:void Delay_s(int ss)

** 功能:精确1s延迟

** 入口参数:ss 需要延时的秒数

** 出口参数:无

** 使用说明:系统库函数延迟因晶振不同有大小限制

***********************************************************/

void Delay_s(int ss)

{

int i = 0;

while(ss--)

{

for(i = 0; i < 25; i++)

{

_delay_ms(40);

}

}

}


//首先库文件里面不再定义F_CPU(即晶振频率),所以在main.c中自行定义。

//且系统延迟函数因晶振的不同对延迟大小有限制,需要注意:

/**

ingroup util_delay

Perform a delay of c __us microseconds, using _delay_loop_1().

The macro F_CPU is supposed to be defined to a

constant defining the CPU clock frequency (in Hertz).

The maximal possible delay is 768 us / F_CPU in MHz.

*/


/**

ingroup util_delay

Perform a delay of c __ms milliseconds, using _delay_loop_2().

The macro F_CPU is supposed to be defined to a

constant defining the CPU clock frequency (in Hertz).

The maximal possible delay is 262.14 ms / F_CPU in MHz.

*/


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭