126 lines
3.5 KiB
C
126 lines
3.5 KiB
C
/***************************************************************************
|
|
* *
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
***************************************************************************/
|
|
|
|
/***************************************************************************
|
|
* INCLUDES
|
|
***************************************************************************/
|
|
|
|
#include "config.h"
|
|
#include "sync_timer.h"
|
|
#include "usart.h"
|
|
|
|
|
|
/***************************************************************************
|
|
* DEFINITIONS
|
|
***************************************************************************/
|
|
|
|
/* overflow counter */
|
|
static volatile UINT8 ui8_sync_timer_overflow = 0;
|
|
|
|
/* flag that is set 100 per second */
|
|
static volatile BOOL b_100 = false;
|
|
|
|
/* TIMER0 compare interrupt handler */
|
|
ISR(TIMER0_COMP_vect)
|
|
{
|
|
++ui8_sync_timer_overflow;
|
|
|
|
if (ui8_sync_timer_overflow == SYNC_TIMER_COUNT)
|
|
{
|
|
ui8_sync_timer_overflow = 0;
|
|
b_100 = true;
|
|
|
|
#ifdef USART_USE_TIMER
|
|
/* used by usartRead for timeout */
|
|
++usart_timeout;
|
|
#endif /* USART USE_TIMER */
|
|
}
|
|
}
|
|
|
|
|
|
/***************************************************************************
|
|
* FUNCTIONS
|
|
***************************************************************************/
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
inline BOOL sync()
|
|
{
|
|
if (b_100)
|
|
{
|
|
b_100 = false;
|
|
|
|
return (true);
|
|
}
|
|
|
|
return (false);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
void initSyncTimer()
|
|
{
|
|
/* set counter0 value */
|
|
OCR0 = SYNC_TIMER_PRESCALER_OCR;
|
|
|
|
/* enable counter0 interrupt */
|
|
TIMSK = BV(OCIE0);
|
|
|
|
/* enable counter0 compare */
|
|
TCCR0 = BV(WGM01);
|
|
|
|
#ifdef SYNC_TIMER_PRESCALER_EDGE
|
|
/* prescaler <= fcpu/1 */
|
|
#if SYNC_TIMER_PRESCALER == 1
|
|
TCCR0 |= BV(CS00);
|
|
/* prescaler <= fcpu/8 */
|
|
#elif SYNC_TIMER_PRESCALER == 8
|
|
TCCR0 |= BV(CS01);
|
|
/* prescaler <= fcpu/64 */
|
|
#elif SYNC_TIMER_PRESCALER == 64
|
|
TCCR0 |= BV(CS00) | BV(CS01);
|
|
/* prescaler <= fcpu/256 */
|
|
#elif SYNC_TIMER_PRESCALER == 256
|
|
TCCR0 |= BV(CS02);
|
|
/* prescaler <= fcpu/1024 */
|
|
#elif SYNC_TIMER_PRESCALER == 1024
|
|
TCCR0 |= BV(CS00) | BV(CS02);
|
|
#else
|
|
#error "SYNC_TIMER: SYNC_TIMER_PRESCALER not set"
|
|
#endif /* SYNC_TIMER_PRESCALER */
|
|
#else
|
|
/* prescaler <= fcpu/1 */
|
|
#if SYNC_TIMER_PRESCALER == 1
|
|
TCCR0 |= BV(CS00);
|
|
/* prescaler <= fcpu/8 */
|
|
#elif SYNC_TIMER_PRESCALER == 8
|
|
TCCR0 |= BV(CS01);
|
|
/* prescaler <= fcpu/32 */
|
|
#elif SYNC_TIMER_PRESCALER == 32
|
|
TCCR0 |= BV(CS00) | BV(CS01);
|
|
/* prescaler <= fcpu/64 */
|
|
#elif SYNC_TIMER_PRESCALER == 64
|
|
TCCR0 |= BV(CS02);
|
|
/* prescaler <= fcpu/128 */
|
|
#elif SYNC_TIMER_PRESCALER == 128
|
|
TCCR0 |= BV(CS00) | BV(CS02);
|
|
/* prescaler <= fcpu/8 */
|
|
#elif SYNC_TIMER_PRESCALER == 256
|
|
TCCR0 |= BV(CS01) | BV(CS02);
|
|
/* prescaler <= fcpu/8 */
|
|
#elif SYNC_TIMER_PRESCALER == 1024
|
|
TCCR0 |= BV(CS00) | BV(CS01) | BV(CS02);
|
|
/* prescaler not set */
|
|
#else
|
|
#error "SYNC_TIMER: SYNC_TIMER_PRESCALER not set"
|
|
#endif /* SYNC_TIMER_PRESCALER */
|
|
#endif /* SYNC_TIMER_PRESCALER_EDGE */
|
|
}
|
|
|
|
|
|
/* END */
|