⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timers.c

📁 WWVB receiver using AVR.
💻 C
字号:
/* $Id: timers.c,v 1.5 2005/10/30 19:44:38 simimeie Exp $ * Functions that work with the avrs timer(s). * Mainly a function that allows you to get sort of a timestamp. */ #define BV _BV#include <avr/io.h>#include <avr/signal.h>#include "timers.h" static volatile uint16_t ticks;static volatile uint16_t seconds;static volatile uint8_t isvalid;/* This interrupt handler gets called when counter 0 overflows */SIGNAL(SIG_OVERFLOW0) {	ticks++;	if (ticks >= TICKSPERSECOND) {		ticks = 0;		seconds++;	}	isvalid++;}#ifdef HIGHRESTIMER/* This interrupt handler gets called when counter 1 overflows. * This should never happen, but we need to signal THAT it happened. * So we stop the timer and set it to 0. */SIGNAL(SIG_OVERFLOW1) {	TCCR1B = 0;	TCNT1H = 0; /* High has to be WRITTEN first */	TCNT1L = 0;}void starthighrestimer(void) {	TCNT1H = 0; /* High has to be WRITTEN first */	TCNT1L = 0;	TCCR1B = 0x05; /* CLK/1024 */}uint16_t readhighrestimer(void) {	TCCR1B = 0; /* Stop it */	/* Split into two lines to force execution sequence */	uint8_t tmp = TCNT1L; /* Low has to be READ first */	return ((((uint16_t)(TCNT1H)) << 8) | tmp);}#endifvoid timers_init(void) {	TIMSK |= BV(TOIE0);#ifdef HIGHRESTIMER	TIMSK |= BV(TOIE1);#endif	TCCR0 = 0x03; /* CLK/64 */}void timers_zero(void) {	if (ticks > (TICKSPERSECOND / 2)) {		seconds++;	}	ticks = 0;}void gettickdata(struct tickdata * td) {	uint8_t lastiv;	do {		lastiv = isvalid;		td->seconds = seconds;		td->ticks = ticks;	} while (lastiv != isvalid); /* In that case we got interrupted */	return;}uint16_t getseconds(void) {	uint8_t lastiv;	uint16_t res;	do {		lastiv = isvalid;		res = seconds;	} while (lastiv != isvalid); /* In that case we got interrupted */	return res;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -