GlobLib
HAL and API libraries for MCUs and hardware.
TIMERS

This module contains low level functions for timer interaction. More...

Files

file  attiny13a_timer.h
 Header file for attiny13a timers.
 

Macros

#define IC_PIN   PIN4
 Pin used for input capture.
 

Enumerations

Functions

mcu_error TIMER_setupCount (timer_main timerNumber, uint16_t frequency, void(*handler)(void))
 Initialize given timer for standard count with interrupt trigging the function handler on timeout. More...
 
mcu_error TIMER_setupIC (timer_main timerNumber, timer_channel timerChannel)
 Initialize given timer for input capture. More...
 
mcu_error TIMER_setupPWM (timer_main timerNumber, timer_channel timerChannel, uint16_t frequency, uint8_t duty)
 Initialize given timer for PWM mode. More...
 
mcu_error TIMER_setupPulse (timer_main timerNumber, timer_channel timerChannel, uint16_t frequency, uint16_t pulse)
 Initialize given timer for Pulse mode. More...
 
mcu_error TIMER_setPeriod (timer_main timerNumber, uint32_t period)
 Set the period for the timer. (in microseconds) More...
 
mcu_error TIMER_setFrequency (timer_main timerNumber, uint16_t frequency)
 Set the frequency for the timer. (in hertz) More...
 
mcu_error TIMER_setDuty (timer_main timerNumber, timer_channel channel, uint8_t duty)
 Set the duty cycle for the waveform (PWM only, 0 - 100%). More...
 
mcu_error TIMER_setPulse (timer_main timerNumber, timer_channel channel, uint16_t pulse)
 Set the pulse width for the output. (in milliseconds) More...
 
uint8_t TIMER_getCount (timer_main timerNumber)
 Get the current value of a running timer. More...
 
mcu_error TIMER_setCount (timer_main timerNumber, uint8_t count)
 Set the current value of a running timer. More...
 
uint16_t TIMER_getIC (timer_main timerNumber, timer_channel channel)
 Get the last input capture time from a channel of a timer. More...
 
mcu_error TIMER_enableISR (timer_main timerNumber, void(*handler)(void))
 Set the ISR target for timeout. More...
 
mcu_error TIMER_disableISR (timer_main timerNumber)
 Disable the timeout IRQ. More...
 
mcu_error TIMER_pause (timer_main timerNumber)
 Pause an already running timer. More...
 
mcu_error TIMER_resume (timer_main timerNumber)
 Resume a paused timer. More...
 

Detailed Description

This module contains low level functions for timer interaction.

The ATTINY13A has a single 8 bit timer with two output channels with output channels mapped to:

The low width of the timer means there are frequency and resolution restrictions. See the given function descriptions.

Note
Some features of this module are disabled by default to save space in flash ROM. The follow should be done to enable these features:

Many of the functions in the module have unused parameters. These parameters are kept in place for compatability with other MCUs.

Warning
Timer input capture and external interrupts (GPIO_ISREnable()) cannot both be used in the same application. Input caputre relies on external interrupt generation for triggers.
Author
Stuart Ianna
Version
0.1
Date
October 2018
Bug:
None
Todo:
Compilers
  • avr-gcc V4.9.2
Working example
#include <stm32f103cb_core.h>
#include <coms128.h>
void up(void);
volatile uint8_t flag;
int main(void){
//Clock setup
CLOCK_setSpeed(CLOCK_EX_8_OUT_72);
//Serial setup
USART_setup(USART_2);
USART_setBaud(USART_2,USART_BAUD_115200);
COMS(USART_2);
//Setup timer 3 to call function "up" at 10Hz
TIMER_setupCount(TIMER_3,20,&up); //Period = 50000 -> Resolution = 1us
//Setup timer 1 for pulse width output at 1000Hz
TIMER_setupPulse(TIMER_1,CHANNEL_1,1000,400); //400us pulse
TIMER_setupPulse(TIMER_1,CHANNEL_2,1000,50); //50us pulse
//Setup timer 2 for PWM at 10kHz
TIMER_setupPWM(TIMER_2,CHANNEL_1,10000,50); //50% duty cycle
//Setup timer four for input capture
TIMER_setupIC(TIMER_4,CHANNEL_3);
TIMER_setupIC(TIMER_4,CHANNEL_4);
//Change duty cycle and pulse output
TIMER_setDuty(TIMER_1,CHANNEL_1,80);
TIMER_setPulse(TIMER_1,CHANNEL_2,100);
TIMER_setDuty(TIMER_1,CHANNEL_2,10);
while (1){
if(flag){
flag = 0;
//Time how long it takes to compete print function with TIMER_3
TIMER_setCount(TIMER_3,0);
//Print input capture values
printt(TIMER_getIC(TIMER_4,CHANNEL_1));
printt(TIMER_getIC(TIMER_4,CHANNEL_2));
printt(TIMER_getIC(TIMER_4,CHANNEL_3));
printt(TIMER_getIC(TIMER_4,CHANNEL_4));
printl(TIMER_getCount(TIMER_3)); //Printing the time difference
}
}
return 0;
}
//This is called by TIMER_3 on timeout
void up(void){
flag = 1;
}

Enumeration Type Documentation

Timers channels available for each timer.

Enumerator
CHANNEL_1 

Channel 1.

CHANNEL_2 

Channel 2.

Definition at line 76 of file attiny13a_timer.h.

enum timer_main

Main timers available on the MCU.

Enumerator
TIMER_0 

Timer 0.

Definition at line 68 of file attiny13a_timer.h.

Function Documentation

mcu_error TIMER_disableISR ( timer_main  timerNumber)

Disable the timeout IRQ.

This disables any previous use of timeout interrupts.

Parameters
timerNumberThe timer number to modify (not used)
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error.
mcu_error TIMER_enableISR ( timer_main  timerNumber,
void(*)(void)  handler 
)

Set the ISR target for timeout.

This feature can be used for Count, Pulse and PWM modes. Function is called on timeout event recursively until TIMER_disableISR() is used.

Parameters
timerNumberThe timer number to modify (not used)
handlerThe function to call on timeout
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error.
uint8_t TIMER_getCount ( timer_main  timerNumber)

Get the current value of a running timer.

The resolution of the value returned depends of the frequency of timer as setup.

Parameters
timerNumberThe timer number to modify (not used)
Returns
The current value of the timer.

Definition at line 297 of file attiny13a_timer.c.

uint16_t TIMER_getIC ( timer_main  timerNumber,
timer_channel  channel 
)

Get the last input capture time from a channel of a timer.

The timer must be setup with TIMER_setupIC() for this function to work.

Parameters
timerNumberThe timer number to modify (not used)
channelThe timer channel to modify
Note
This function returns 0 if called twice when no reading has occured.
Returns
The last input capture time in microseconds
mcu_error TIMER_pause ( timer_main  timerNumber)

Pause an already running timer.

The timer must have been already setup to use this.

Parameters
timerNumberThe timer number to modify (not used)
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 113 of file attiny13a_timer.c.

mcu_error TIMER_resume ( timer_main  timerNumber)

Resume a paused timer.

Due to the internal register configuration of the ATTINY13A the timer needs to be restarted with one of the setup functions. E.g TIMER_setupCount()

Warning
This should not be used to restart a paused timer for the ATTINY13a.
Parameters
timerNumberThe timer number to modify (not used)
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error.

Definition at line 122 of file attiny13a_timer.c.

mcu_error TIMER_setCount ( timer_main  timerNumber,
uint8_t  count 
)

Set the current value of a running timer.

Parameters
timerNumberThe timer number to modify (not used)
countThe timer value
Returns
The current value of the timer.

Definition at line 303 of file attiny13a_timer.c.

mcu_error TIMER_setDuty ( timer_main  timerNumber,
timer_channel  channel,
uint8_t  duty 
)

Set the duty cycle for the waveform (PWM only, 0 - 100%).

Parameters
timerNumberThe timer number to modify (not used)
channelThe timer chanel to modify
dutyThe desired duty cycle.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 312 of file attiny13a_timer.c.

mcu_error TIMER_setFrequency ( timer_main  timerNumber,
uint16_t  frequency 
)

Set the frequency for the timer. (in hertz)

The minimum and maximum frequency value that can be represented are dependant on the frequency of the main clock as specified:

F_CPU Minimum Maximum
9.6MHz 40Hz 10kHz
4.8MHz 20Hz 5kHz
1.2MHz 5Hz 2.5kHz
0.6MHz 3Hz 1.25kHz
Parameters
timerNumberThe timer number to modify (not used)
frequencyThe desired timeout frequency.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PERIOD if the period is too long
mcu_error TIMER_setPeriod ( timer_main  timerNumber,
uint32_t  period 
)

Set the period for the timer. (in microseconds)

The minimum and maximum periods that can be represented are dependant on the frequency of the main clock as specified:

F_CPU Minimum Maximum
9.6MHz 100us 25ms
4.8MHz 200us 50ms
1.2MHz 800us 200ms
0.6MHz 1.6ms 400ms
Parameters
timerNumberThe timer number to modify (not used)
periodThe desired timeout period.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PERIOD if the period is too long
mcu_error TIMER_setPulse ( timer_main  timerNumber,
timer_channel  channel,
uint16_t  pulse 
)

Set the pulse width for the output. (in milliseconds)

The resolution and period of the pulse widths are limited by the clock frequency as follows:

F_CPU Period Resolution
9.6MHz 6.8ms 26.6us
4.8MHz 13.6ms 53.3us
1.2MHz 13.5ms 53.3us
0.6MHz 27.9ms 106us
Parameters
timerNumberThe timer number to modify (not used)
channelThe timer channel to modify
pulseThe desired pulse width.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PULSE if the pulse is too long

Definition at line 326 of file attiny13a_timer.c.

mcu_error TIMER_setupCount ( timer_main  timerNumber,
uint16_t  frequency,
void(*)(void)  handler 
)

Initialize given timer for standard count with interrupt trigging the function handler on timeout.

The minimum and maximum frequency value that can be represented are dependant on the frequency of the main clock as specified:

F_CPU Minimum Maximum
9.6MHz 40Hz 10kHz
4.8MHz 20Hz 5kHz
1.2MHz 5Hz 2.5kHz
0.6MHz 3Hz 1.25kHz
Parameters
timerNumberThe timer number to setup (not used)
handlerThe function to call on timeout event.
frequencyThe frequency of the timer count expiration.
Warning
TIMER_ISR needs to be uncommented in the makefile for this to work
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.
mcu_error TIMER_setupIC ( timer_main  timerNumber,
timer_channel  timerChannel 
)

Initialize given timer for input capture.

The input capture channel is fixed to PORTB, PIN4. This can be changed by adding the compiler flag -DIC_PIN=xxx, where xxx is the pin number defined by gpio_pin.

Warning
Input capture and external pin interrupts cannot be used at the same time, as input capture uses external interrupts.
Note
TIMER_IC needs to be uncommented in the makefile for this to work

The signal period and pulse resolution is dependant on the clock frequency of the device, as set in the Makefile.

F_CPU Period Resolution
9.6MHz 6.8ms 26.6us
4.8MHz 13.6ms 53.3us
1.2MHz 13.5ms 53.3us
0.6MHz 27.9ms 106us
Parameters
timerNumberThe timer number to setup (not used)
timerChannelThe channel of that timer to use. (not used)
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.
mcu_error TIMER_setupPulse ( timer_main  timerNumber,
timer_channel  timerChannel,
uint16_t  frequency,
uint16_t  pulse 
)

Initialize given timer for Pulse mode.

The given timer number timer_main is now locked to this pulse or PWM modes.

All channels of this timer can only be used for pulse of PWM. Each channel must also share the same frequency. However each channel can be turned on and off

The signal period and pulse resolution is dependant on the clock frequency of the device, as set in the Makefile.

F_CPU Period Resolution
9.6MHz 6.8ms 26.6us
4.8MHz 13.6ms 53.3us
1.2MHz 13.5ms 53.3us
0.6MHz 27.9ms 106us
Parameters
timerNumberThe timer number to setup (not used)
timerChannelThe channel of that timer to use.
frequencyThe frequency of the total signal. (not used)
pulseThe initial pulse width of the signal.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 251 of file attiny13a_timer.c.

mcu_error TIMER_setupPWM ( timer_main  timerNumber,
timer_channel  timerChannel,
uint16_t  frequency,
uint8_t  duty 
)

Initialize given timer for PWM mode.

The given timer number timer_main is now locked to this or pulse mode.

All channels of this timer can only be used for PWM of Pulse width output. Each channel must also share the same frequency. However each channel can be turned on and off

The frequency of the PWM waveform is determined by the clock frequency as follows:

F_CPU Frequency
9.6MHz 37.5kHz
4.8MHz 18.75kHz
1.2MHz 4.69kHz
0.6MHz 2.3kHz
Parameters
timerNumberThe timer number to setup (not used)
timerChannelThe channel of that timer to use.
frequencyThe frequency of the signal. (not used)
dutyThe initial duty cycle.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 220 of file attiny13a_timer.c.