|
楼主 |
发表于 2008-1-3 18:56:32
|
显示全部楼层
//以下是ICCAVR下的部分代码, 由于系工作成果, 不便贴出全部代码
void AddC2(void)
{
_ADD_C2;
}
void RemoveC2(void)
{
_REMOVE_C2;
}
unsigned char IsCountting = 1; //计数闸门
unsigned char T1OF_counter = 0; //计数溢出中断计数器
//Watchdog initialize
// prescale: 2048K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
//WDTCR = 0x1F; ////特别注意这一条不是ICC生成的,是后来加上的。
WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}
void CounterStart(void)
{
TCNT1 = 0;
TCNT2 = 0x27;
TCCR1B = 0x06; //外部引脚T1, 下降沿驱动
//TCCR1B = 0x07; //外部引脚T1, 上升沿驱动
TCCR2 = 0x07;
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
++T1OF_counter; //中断溢出计数器
}
//TIMER2 initialize - prescale:1024
// WGM: Normal
// desired value: 27Hz
// actual value: 27.002Hz (0.0%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x27; //setup
OCR2 = 0xD9;
//TCCR2 = 0x07; //start
}
#define _GATE_FREQUENCY 54
#define _DEVIDER (1) //计数的闸门时间控制
#pragma interrupt_handler timer2_ovf_isr:iv_TIM2_OVF
void timer2_ovf_isr(void)
{
TCCR1B = 0x00; //timer1 stop
TCCR2 = 0x00; //timer2 stop
IsCountting = 0;
}
void GetFreq(unsigned long *pFreq)
{
IsCountting = 1; //
T1OF_counter = 0;
CounterStart();
while(IsCountting); //等待闸门关闭
*pFreq = TCNT1 + (T1OF_counter * (unsigned int)(-1));
T1OF_counter = 0;
} |
|