-
Notifications
You must be signed in to change notification settings - Fork 0
Timers
This page documentation on the use of Timer Functions within SUBLIBinal.
Functions available:
- initialize_Timer
- enable_Timer
- disable_Timer
- update_period_Timer
- update_frequency_Timer
- update_divider_Timer
Definition:
typedef struct TIMER_CONFIG {
Timer_Type which_timer;
uint frequency;
uint pbclk;
void (*callback);
boolean enabled;
}Timer_Config; The Timer_Config structure is made up of 5 different parameters and is used for defining characteristics of a timer.
Parameters:
- which_timer: The which_timer parameter describes which timer the structure is to be associated with.
- frequency: The frequency parameter should specify the desired frequency that a timer should interrupt at
- pbclk: the pbclk parameter contains the frequency of the peripheral bus clock. This is used to accurately determine timer frequency
- callback: The callback parameter is the function callback for the timer interrupt service routine. For more information, please refer to the Callback Information
- enabled: This parameter specifies whether the timer should be enabled upon initialization. This can be either TRUE or FALSE
typedef enum {
Timer_1,
Timer_2,
Timer_3,
Timer_4,
Timer_5
} Timer_Type;The timer type parameter is simply used to determine which timer in the microcontroller the library should configure.
typedef enum {
Div_1,
Div_2,
Div_4,
Div_8,
Div_16,
Div_32,
Div_64,
Div_256
} Clock_Divider;The Clock_Divider enumeration is a specification of clock dividers to use. This parameter is only used if you are trying to manually override the clock divider and period registers. Please note, if you are modifying the divider of Timer_1, only Div_1, Div_8, Div_64, and Div_256 are available for use. If you attempt to use another divider, it will be automatically upgraded to the closest available divider. For example, if you attempt to update to Div_2, the divider will be set to Div_8 instead. This is only true for Timer_1.
Prototype
void initialize_Timer(Timer_Config config);
Description:
This function will initialize a timer. The function will attempt to generate appropriate dividers and periods to acquire the provided frequency. If the function can not achieve the required frequency, it will set parameters to as close as it can to the defined frequency. That is, if a frequency is supplied that is beyond the capabilities of the timer, it will be set for the maximum divider and the maximum period.
Parameters:
- config: config is a Timer_Config structure that has been filled out with all relevant parameters. All parameters should be filled out with the exception of the callback function, which is optional.
Prototype
void disable_Timer(Timer_Config config);
Description:
This function will disable a timer and its associated interrupts. The current TIMER value is not reset.
Parameters:
- config: config is a Timer_Config structure that has been filled out. The only parameter that must strictly be filled out for this function is the which_timer parameter so that the function can properly disable the correct timer.
Prototype
void enable_Timer(Timer_Config config);
Description:
This function will enable a timer and its associated interrupt. The interrupt is only activated if the callback function parameter is not NULL.
Parameters:
- config: config is a Timer_Config structure that has been filled out. The only parameter that must strictly be filled out for this function is the which_timer parameter so that the function can properly disable the correct timer.
Prototype
void update_period_Timer(Timer_Config config, int period);
Description:
This function updates the period of a timer. This function will only touch the period register of a timer and will not change the divider or the enable of the timer. This function will also reset the current value in the timer register to avoid problems associated with timer overflow.
Parameters:
- config: config is a Timer_Config structure that has been filled out. The only parameter that must strictly be filled out for this function is the which_timer parameter so that the function can properly disable the correct timer.
- period: The period parameter should be an integer that you would like the timer period to be set to.
Prototype
void update_frequency_Timer(Timer_Config config, float frequency);
Description:
This function will manually update the frequency of the supplied timer. Both the divider and the period of the timer will be changed. The calculation that update_frequency_Timer completes to determine divider and period is identical to that used by the initialize_Timer function, and behavior will be identical. The TIMER value will also be reset to 0 to avoid issues associated with timer overflow.
Parameters:
- config: config is a Timer_Config structure that has been filled out. The only parameter that must strictly be filled out for this function is the which_timer parameter so that the function can properly disable the correct timer.
- frequency: The frequency parameter is a value that the specified timer should be set to interrupt at.
Prototype:
void update_divider_Timer(Timer_Config config, Clock_Divider div);
Description:
The update_divider_Timer function will simply update the timer divider associated with the configuration parameter. This function will not modify the period register or the current TIMER value.
Parameters:
- config: config is a Timer_Config structure that has been filled out. The only parameter that must strictly be filled out for this function is the which_timer parameter so that the function can properly disable the correct timer.
- div: The div parameter is used to specify what the divider should be. Please note, if you are specifying a new divider for timer_1, it will automatically upgrade to the closest divider available for timer 1. That is, if you attempt to update to Div_2, it will instead update to Div_8.
SUBLIBinal was created by the Palouse Robosub Club.