標題: T/C1 CTC模式精確定時 [打印本頁]

作者: xuwei    時間: 2015-6-13 23:28
標題: T/C1 CTC模式精確定時
//延時一毫秒
void DelayMs(uint16_t)
{
   uint16_t i;
   for(i=0;i<t;i++)
      _delay_loop_2(CPU_FRQ * 4);//CPU_FRQ為系統(tǒng)時鐘頻率以MHz為單位
}

/********************************************
Timer1 CTC 模式精確定時示例
文件名:main.c
編譯:WinAVR-20070525

硬件環(huán)境:CA-M8X
時鐘:外部4MHz
打開的開關(guān)如下
S6(1,2) - 外部4MHz晶振
S1(7) - PB0接LED

芯藝設(shè)計室 2004-2007 版權(quán)所有
轉(zhuǎn)載請保留本注釋在內(nèi)的全部內(nèi)容
Email: changfutong@sina.com

本程序按0.5秒改變一次接PB0口的LED
狀態(tài)來說明T/C1 CTC模式精確定時
********************************************/
#include <avr/io.h>
#include <avr/interrupt.h>

#define FLASH_R_LED PORTB^=_BV(PB0)

//系統(tǒng)時鐘:4000000Hz 分頻:64
//CTC TOP值:0x7A12
//誤差:0.000000個系統(tǒng)周期

//Timer1初始化
void Timer1Init(void)
{
TCCR1A=0;
TCCR1B=_BV(WGM12)|_BV(CS10)|_BV(CS11);
OCR1A=0x7A12;
TIMSK|=_BV(OCIE1A);
}

//Timer1 CTC中斷,此中斷發(fā)生周期是:500毫秒
ISR(TIMER1_COMPA_vect)
{
FLASH_R_LED;
}

int main(void)
{
DDRB|=_BV(PB0);
Timer1Init();
sei();
while(1);
}






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1