STC15W408AS library 0.17.0
Loading...
Searching...
No Matches
Functions | Variables
timer_ms_to_ticks_table.c File Reference

Implementation of timer to milliseconds conversion using lookup tables. More...

#include <timer_ms_to_ticks_table.h>
#include <frequency.h>
#include <interrupt.h>
Include dependency graph for timer_ms_to_ticks_table.c:

Functions

void timer0_1ms_delay_init ()
 Initializes Timer0 for 1 millisecond delay using precalculated ticks.
 
void timer0_1ms_delay ()
 Executes a 1 millisecond delay using Timer0.
 

Variables

uint16_t precalc_1t_ticks [8]
 Precalculated timer ticks for 1ms delay in 1T mode.
 
uint16_t precalc_12t_ticks [8]
 Precalculated timer ticks for 1ms delay in 12T mode.
 

Detailed Description

Implementation of timer to milliseconds conversion using lookup tables.

This file provides the implementation for fast millisecond to tick conversion using precalculated lookup tables. The tables contain precomputed values for 1 millisecond delays at various system clock frequencies and divider settings.

Lookup table organization:

Each array has 8 elements, with the index corresponding to the frequency scale divider (from CLK_DIV register bits [0..2]). The frequency scale ranges from 0 to 7, corresponding to clock dividers of 1, 2, 4, 8, 16, 32, 64, and 128.

The precalculated values are derived from the formula: ticks = (SYSclk / divider) * 0.001

Where SYSclk = MAIN_Fosc / (2^divider_scale)

Example calculations for 11,059,200 Hz crystal:

Note
The values in the tables are rounded approximations. The actual values in comments show the exact mathematical results.
The timer_ms_to_ticks_table.h header provides the public API for accessing these lookup tables via the timer_1ms_to_ticks() function.
See also
timer_ms_to_ticks_table.h - Public API for table-based conversion
timer_to_ms_common.h - Dynamic conversion functions
frequency.h - System clock frequency management
Author
Michael Golovanov

Function Documentation

◆ timer0_1ms_delay()

void timer0_1ms_delay ( )

Executes a 1 millisecond delay using Timer0.

Starts Timer0 and waits for the overflow flag (TF0) to be set, indicating that the configured 1ms time interval has elapsed. The timer is configured by timer0_1ms_delay_init() with the appropriate reload values for the system clock frequency.

Note
This is a blocking function that waits in a polling loop until the timer overflow occurs.
Timer0 must be initialized with timer0_1ms_delay_init() before calling this function.
See also
timer0_1ms_delay_init() - Initializes Timer0 for 1ms delay
TF0 - Timer0 overflow flag
TR0 - Timer0 run control bit

◆ timer0_1ms_delay_init()

void timer0_1ms_delay_init ( )

Initializes Timer0 for 1 millisecond delay using precalculated ticks.

Note
Must be called before using timer0_1ms_delay() to ensure correct timer configuration for the current system clock settings.
Disables Timer0 interrupt during initialization to prevent unwanted interrupt service routine execution.
See also
get_frequency_divider_scale() - Gets current frequency scale index
get_timer0_clock_divider() - Gets Timer0 clock divider setting
timer0_1ms_delay() - Performs the actual 1ms delay
precalc_1t_ticks - Lookup table for 1T mode tick values
precalc_12t_ticks - Lookup table for 12T mode tick values

Variable Documentation

◆ precalc_12t_ticks

uint16_t precalc_12t_ticks[8]
Initial value:
=
{
922,
459,
227,
112,
54,
25,
10,
4,
}

Precalculated timer ticks for 1ms delay in 12T mode.

Array index corresponds to frequency scale divider (CLK_DIV bits [0..2])

These values represent the number of timer ticks required for a 1 millisecond delay when the timer is configured in 12T mode (twelve clock cycles per tick). The array index is determined by the system clock frequency scale setting.

Index mapping:

  • Index 0: divider_scale=0, SYSclk = MAIN_Fosc, divider=12
  • Index 1: divider_scale=1, SYSclk = MAIN_Fosc/2, divider=24
  • Index 2: divider_scale=2, SYSclk = MAIN_Fosc/4, divider=48
  • Index 3: divider_scale=3, SYSclk = MAIN_Fosc/8, divider=96
  • Index 4: divider_scale=4, SYSclk = MAIN_Fosc/16, divider=192
  • Index 5: divider_scale=5, SYSclk = MAIN_Fosc/32, divider=384
  • Index 6: divider_scale=6, SYSclk = MAIN_Fosc/64, divider=768
  • Index 7: divider_scale=7, SYSclk = MAIN_Fosc/128, divider=1536
Note
Values in comments show exact mathematical calculations
See also
get_frequency_divider_scale() - Function to get current index
timer_1ms_to_ticks() - Function using this table

◆ precalc_1t_ticks

uint16_t precalc_1t_ticks[8]
Initial value:
=
{
11070,
5510,
2733,
1343,
650,
300,
127,
43,
}

Precalculated timer ticks for 1ms delay in 1T mode.

Array index corresponds to frequency scale divider (CLK_DIV bits [0..2])

These values represent the number of timer ticks required for a 1 millisecond delay when the timer is configured in 1T mode (one clock cycle per tick). The array index is determined by the system clock frequency scale setting.

Index mapping:

  • Index 0: divider_scale=0, SYSclk = MAIN_Fosc, divider=1
  • Index 1: divider_scale=1, SYSclk = MAIN_Fosc/2, divider=2
  • Index 2: divider_scale=2, SYSclk = MAIN_Fosc/4, divider=4
  • Index 3: divider_scale=3, SYSclk = MAIN_Fosc/8, divider=8
  • Index 4: divider_scale=4, SYSclk = MAIN_Fosc/16, divider=16
  • Index 5: divider_scale=5, SYSclk = MAIN_Fosc/32, divider=32
  • Index 6: divider_scale=6, SYSclk = MAIN_Fosc/64, divider=64
  • Index 7: divider_scale=7, SYSclk = MAIN_Fosc/128, divider=128
Note
Values in comments show exact mathematical calculations
See also
get_frequency_divider_scale() - Function to get current index
timer_1ms_to_ticks() - Function using this table