The division factor for the delay_us(x) macro is simply expanded. (Isn’t it simpler to throw away the FOSC value and use F_CPU instead?)
Code: Select all
// Sinan -Removed: #define FOSC 16000000UL // internal clock of 16 mhz
#if F_CPU == 20000000L // for the 20 MHz clock on rare Arduino boards
#define FOSC 20000000UL
#elif F_CPU == 16000000L // for the 16 MHz clock on most Arduino boards
#define FOSC 16000000UL
#elif F_CPU == 8000000L // for the 8 MHz internal clock
#define FOSC 8000000UL
#else
#error Unsupported clock speed
#endif
Code: Select all
#ifdef FRSKY_SPORT
// This section chooses the correct timer values for the chosen baudrate depending on the clock speed.
// 57600 = Desired baudrate pour le Sport Interface = 17 micro sec per bit.
#if F_CPU == 20000000L // 20MHz clock
// Sinan: Not tested
#define TICKS2COUNT 348 // Ticks between two bits.
#define TICKS2WAITONE 348 // Wait one bit period.
#define TICKS2WAITONE_HALF 520 // Wait one and a half bit period.
#elif F_CPU == 16000000L // 16MHz clock
#define TICKS2COUNT 278 // Ticks between two bits.
#define TICKS2WAITONE 278 // Wait one bit period.
#define TICKS2WAITONE_HALF 416 // Wait one and a half bit period.
#elif F_CPU == 8000000L // 8MHz clock
// Assumes a 8MHz clock
#define TICKS2COUNT 139 // Ticks between two bits.
#define TICKS2WAITONE 139 // Wait one bit period.
#define TICKS2WAITONE_HALF 208 // Wait one and a half bit period.
#else
#error Unsupported clock speed
#endif
#else // FRSKY HUB
// This section chooses the correct timer values for the chosen baudrate depending on the clock speed.
// 9600 = Desired baudrate for FrSky hub
#if F_CPU == 20000000L // 20MHz clock
// Sinan: Not tested
#define TICKS2COUNT (348*6) // Ticks between two bits.
#define TICKS2WAITONE (348*6) // Wait one bit period.
#define TICKS2WAITONE_HALF (520*6) // Wait one and a half bit period.
#elif F_CPU == 16000000L // 16MHz clock
#define TICKS2COUNT (278*6) // Ticks between two bits.
#define TICKS2WAITONE (278*6) // Wait one bit period.
#define TICKS2WAITONE_HALF (416*6) // Wait one and a half bit period.
#elif F_CPU == 8000000L // 8MHz clock
#define TICKS2COUNT (139*6) // Ticks between two bits.
#define TICKS2WAITONE (139*6) // Wait one bit period.
#define TICKS2WAITONE_HALF (208*6) // Wait one and a half bit period.
#else
#error Unsupported clock speed
#endif
#endif
Code: Select all
void init()
{
// Timer1
TIMSK1 &= ~( 1<< OCIE1A ) ;
TCCR1A = 0x00 ; //Init.
TCCR1B = 0xC1 ; // I/p noise cancel, rising edge, Clock/1
// TODO: this code should use F_CPU to determine the prescale factor.
#if defined(ADCSRA)
// set a2d prescale factor to 128
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
// XXX: this will not work properly for other clock speeds, and
// this code should use F_CPU to determine the prescale factor.
sbi(ADCSRA, ADPS2);
sbi(ADCSRA, ADPS1);
sbi(ADCSRA, ADPS0);
// enable a2d conversions
sbi(ADCSRA, ADEN);
#endif
Cheers, Sinan