STC15W408AS library 0.9.0
Macros
Timer Aux common

Macros

#define timer_uint16_ticks_to_freq100(ticks, timer_clock_divider, frequency_divider_scale)   ((100 * get_master_clock_frequency() / (timer_clock_divider * (1 + (uint32_t)ticks))) >> frequency_divider_scale)
 Convert ticks to timer frequency multiplied by 100. More...
 
#define timer_frequency_to_ticks_unsafe(frequency, timer_clock_divider, frequency_divider_scale)   (((get_master_clock_frequency() >> 1) / (frequency * timer_clock_divider)) >> frequency_divider_scale)
 Convert timer frequency to ticks (unsafe operation) More...
 
#define timer_ticks_to_ms_usafe(ticks, timer_divider, frequency_divider_scale)   (((1UL + (uint32_t)ticks) / ((get_master_clock_frequency_high_part() / timer_divider) >> frequency_divider_scale)))
 Convert ticks count to ms. More...
 
#define timer_ms_to_ticks_usafe(ms, timer_clock_divider)   (ms * ((get_master_clock_frequency_high_part() + 26) >> get_frequency_divider_scale()) / timer_clock_divider)
 Convert milliseconds to timer ticks. More...
 

Detailed Description

Functions and data structures for converting timer ticks to frequency and times for timers.

Author
Michael Golovanov

Macro Definition Documentation

◆ timer_frequency_to_ticks_unsafe

#define timer_frequency_to_ticks_unsafe (   frequency,
  timer_clock_divider,
  frequency_divider_scale 
)    (((get_master_clock_frequency() >> 1) / (frequency * timer_clock_divider)) >> frequency_divider_scale)

Convert timer frequency to ticks (unsafe operation)

Frequency to ticks is a reverse operation of ticks to frequency,

Not all frequency ranges can be correctly coverted to ticks. This method doesnt carefully analyze frequency to ticks covertion correctness.

For 1T mode and frequency divider scale 0 minimal frequency is near 100Hz (11059200 / 2 / 100) = 55296 ticks and maximum frequency is near 5MHz 1.1 ticks

For 12T mode and frequency divider scale 0 minimal frequency is near 8Hz (11059200 / 2 / 12 / 8) = 57600 ticks and maximum frequency is near 400kHz

Parameters
frequencyuint32_t frequency
timer_clock_divideruint8_t or timer_clock_divider_t enum T12 or T1 timer clock divider
frequency_divider_scaleuint8_t mcu clock frequency diviver scale in range of [0..7]
Returns
uint16_t ticks count for input parameters

◆ timer_ms_to_ticks_usafe

#define timer_ms_to_ticks_usafe (   ms,
  timer_clock_divider 
)    (ms * ((get_master_clock_frequency_high_part() + 26) >> get_frequency_divider_scale()) / timer_clock_divider)

Convert milliseconds to timer ticks.

1 ms = ((get_master_clock_frequency_high_part() + 26) >> get_frequency_divider_scale()) / timer_clock_divider

This routine doesnt check overflows. Its unsafe.

Parameters
msuint16_t ms to convert
timer_clock_dividertimer clock divider
Returns
uint16_t ticks count for milliseconds

◆ timer_ticks_to_ms_usafe

#define timer_ticks_to_ms_usafe (   ticks,
  timer_divider,
  frequency_divider_scale 
)    (((1UL + (uint32_t)ticks) / ((get_master_clock_frequency_high_part() / timer_divider) >> frequency_divider_scale)))

Convert ticks count to ms.

This method doesnt analyze corner cases and overflow of result. Be careful.

Parameters
ticksuint16_t ticks count
timer_dividertimer_clock_divider_t 1T or 12T timer clock divider
frequency_divider_scaleuint8_t MCU frequency divider scale
Returns
milliseconds time corresponing to timer ticks

◆ timer_uint16_ticks_to_freq100

#define timer_uint16_ticks_to_freq100 (   ticks,
  timer_clock_divider,
  frequency_divider_scale 
)    ((100 * get_master_clock_frequency() / (timer_clock_divider * (1 + (uint32_t)ticks))) >> frequency_divider_scale)

Convert ticks to timer frequency multiplied by 100.

The output frequency = (SYSclk/timer_clock_divider)/((65536-ticks) * 2). Multiplication by 100 is used for get frequency value without using float numbers.

uint16 ticks used in mode 0, 1, 3. Low byte of ticks is used in mode 2.

Parameters
ticksuint16_t timer ticks count to covert
timer_clock_divideruint8_t timer clock divider T12 or T1
frequency_divider_scaleuint8_t mcu frequency divider scale 0..7
Returns
uint32_t frequency multiplied by 100 corresponding to ticks count