atmega8 例程:系統(tǒng)庫函數(shù)的延遲
/***********************************************************
* 函數(shù)庫說明:ATMEGE8 延遲庫函數(shù)
* 版本: v1.0
*
************************************************************
*注意: LED PC5
***********************************************************/
#include
//定義外部晶振
#define F_CPU 6000000UL
//延遲包含頭文件
#include
//函數(shù)聲明
void Delay_s(int ss);
int main(void)
{
//LED等PC5設(shè)置為輸出
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延遲
** 入口參數(shù):ss 需要延時的秒數(shù)
** 出口參數(shù):無
** 使用說明:系統(tǒng)庫函數(shù)延遲因晶振不同有大小限制
***********************************************************/
void Delay_s(int ss)
{
int i = 0;
while(ss--)
{
for(i = 0; i < 25; i++)
{
_delay_ms(40);
}
}
}
//首先庫文件
//且系統(tǒng)延遲函數(shù)因晶振的不同對延遲大小有限制,需要注意:
/**
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.
*/