lib/RadioHead/RH_ASK.cpp
// Common function for setting timer ticks @ prescaler values for speed
// Returns prescaler index into {0, 1, 8, 64, 256, 1024} array
// and sets nticks to compare-match value if lower than max_ticks
// returns 0 & nticks = 0 on fault
Common function for setting timer ticks @ prescaler values for speed
Returns prescaler index into {0, 1, 8, 64, 256, 1024} array
and sets nticks to compare-match value if lower than max_ticks
returns 0 & nticks = 0 on fault
Common function for setting timer ticks @ prescaler values for speed Returns prescaler index into { 0 1 8 64 256 1024 } array and sets nticks to compare - match value if lower than max_ticks returns 0 & nticks = 0 on fault
uint8_t RH_ASK::timerCalc(uint16_t speed, uint16_t max_ticks, uint16_t *nticks)
{
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) || (RH_PLATFORM == RH_PLATFORM_GENERIC_AVR8) || (RH_PLATFORM == RH_PLATFORM_ATTINY)
    uint8_t prescaler;     
    unsigned long ulticks; 
    if (speed == 0)
    {
        *nticks = 0;
        return 0;
    }
    unsigned long inv_bit_time = ((unsigned long)speed) * 8;
    for (prescaler = 1; prescaler < NUM_PRESCALERS; prescaler += 1)
    {
	uint16_t prescalerValue;
	memcpy_P(&prescalerValue, &prescalers[prescaler], sizeof(uint16_t));
        unsigned long inv_clock_time = F_CPU / ((unsigned long)prescalerValue);
        ulticks = inv_clock_time / inv_bit_time;
        if ((ulticks > 1) && (ulticks < max_ticks))
            break; 
    }
    if ((prescaler == 6) || (ulticks < 2) || (ulticks > max_ticks))
    {
        *nticks = 0;
        return 0;
    }
    *nticks = ulticks;
    return prescaler;
#else
    return 0; 
#endif
}
uint8_t RH_ASK :: timerCalc ( uint16_t speed , uint16_t max_ticks , uint16_t * nticks ) { #if ( RH_PLATFORM == RH_PLATFORM_ARDUINO ) || ( RH_PLATFORM == RH_PLATFORM_GENERIC_AVR8 ) || ( RH_PLATFORM == RH_PLATFORM_ATTINY ) uint8_t prescaler ; unsigned long ulticks ; if ( speed == 0 ) { * nticks = 0 ; return 0 ; } unsigned long inv_bit_time = ( ( unsigned long ) speed ) * 8 ; for ( prescaler = 1 ; prescaler < NUM_PRESCALERS ; prescaler += 1 ) { uint16_t prescalerValue ; memcpy_P ( & prescalerValue , & prescalers [ prescaler ] , sizeof ( uint16_t ) ) ; unsigned long inv_clock_time = F_CPU / ( ( unsigned long ) prescalerValue ) ; ulticks = inv_clock_time / inv_bit_time ; if ( ( ulticks > 1 ) && ( ulticks < max_ticks ) ) break ; } if ( ( prescaler == 6 ) || ( ulticks < 2 ) || ( ulticks > max_ticks ) ) { * nticks = 0 ; return 0 ; } * nticks = ulticks ; return prescaler ; #else return 0 ; #endif }
Common function for setting timer ticks @ prescaler values for speed
Returns prescaler index into {0, 1, 8, 64, 256, 1024} array
and sets nticks to compare-match value if lower than max_ticks
returns 0 & nticks = 0 on fault
Common function for setting timer ticks @ prescaler values for speed Returns prescaler index into { 0 1 8 64 256 1024 } array and sets nticks to compare - match value if lower than max_ticks returns 0 & nticks = 0 on fault
// Clock divider (prescaler) values - 0/3333: error flag // index into array & return bit value // calculate by ntick overflow // Div-by-zero protection // signal fault // test increasing prescaler (divisor), decreasing ulticks until no overflow // 1/Fraction of second needed to xmit one bit // Integer arithmetic courtesy Jim Remington // 1/Amount of time per CPU clock tick (in seconds) // number of prescaled ticks needed to handle bit time @ speed // Test if ulticks fits in nticks bitwidth (with 1-tick safety margin) // found prescaler // Won't fit, check with next prescaler value // Check for error // signal fault // not implemented or needed on other platforms