Embedded C Firmware Library : Arduino / Atmega328p  1
Register Level Embedded C Hardware Abstraction Library for AVR ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI
avr_timer0.h File Reference

Library for 8-Bit Timer-0. More...

#include <avr/io.h>
#include <avr/interrupt.h>
#include "avr_macros.h"
#include "avr_utils.h"

Go to the source code of this file.

Data Structures

struct  TIMER0_ConfigData
 

Macros

TIMER0_OC0A_MODE

Options for Timer-0 Output Compare Pin A (OC0A) Mode

#define TIMER0_OC0A_NO_OUTPUT   (0<<COM0A1) | (0<<COM0A0)
 
#define TIMER0_OC0A_TOGGLE_ON_MATCH   (1<<COM0A0)
 
#define TIMER0_OC0A_CLEAR_ON_MATCH   (1<<COM0A1)
 
#define TIMER0_OC0A_SET_ON_MATCH   (1<<COM0A1) | (1<<COM0A0)
 
#define TIMER0_OC0A_NON_INVERTING   (1<<COM0A1)
 
#define TIMER0_OC0A_INVERTING   (1<<COM0A1) | (1<<COM0A0)
 
#define TIMER0_OC0A_MASK   (1<<COM0A1) | (1<<COM0A0)
 
TIMER0_OC0B_MODE

Options for Timer-0 Output Compare Pin B (OC0B) Mode

#define TIMER0_OC0B_NO_OUTPUT   (0<<COM0B1) | (0<<COM0B0)
 
#define TIMER0_OC0B_TOGGLE_ON_MATCH   (1<<COM0B0)
 
#define TIMER0_OC0B_CLEAR_ON_MATCH   (1<<COM0B1)
 
#define TIMER0_OC0B_SET_ON_MATCH   (1<<COM0B1) | (1<<COM0B0)
 
#define TIMER0_OC0B_NON_INVERTING   (1<<COM0B1)
 
#define TIMER0_OC0B_INVERTING   (1<<COM0B1) | (1<<COM0B0)
 
#define TIMER0_OC0B_MASK   (1<<COM0B1) | (1<<COM0B0)
 
TIMER0_MODE

Options for Timer-0 Operation / Waveform Generation Mode

#define TIMER0_MODE_NORMAL   (0<<WGM01) | (0<<WGM00)
 
#define TIMER0_MODE_PHASE_CORRECT_PWM   (1<<WGM00)
 
#define TIMER0_MODE_CTC   (1<<WGM01)
 
#define TIMER0_MODE_FAST_PWM   (1<<WGM01) | (1<<WGM00)
 
#define TIMER0_MODE_MASK   (1<<WGM01) | (1<<WGM00)
 
TIMER0_PWM_TOP_COUNT

Options for the TOP Count Value in Timer-0 PWM Generation Mode

#define TIMER0_PWM_TOP_0xFF   (0<<WGM02)
 
#define TIMER0_PWM_TOP_OCRA   (1<<WGM02)
 
#define TIMER0_PWM_TOP_MASK   (1<<WGM02)
 
TIMER0_PRESCALER

Options for Timer-0 Input Clock Prescaler

#define TIMER0_PRESCALER_0   (0<<CS02) | (0<<CS01) | (0<<CS00)
 
#define TIMER0_PRESCALER_1   (1<<CS00)
 
#define TIMER0_PRESCALER_8   (1<<CS01)
 
#define TIMER0_PRESCALER_64   (1<<CS01) | (1<<CS00)
 
#define TIMER0_PRESCALER_256   (1<<CS02)
 
#define TIMER0_PRESCALER_1024   (1<<CS02) | (1<<CS00)
 
#define TIMER0_PRESCALER_MASK   (1<<CS02) | (1<<CS01) | (1<<CS00)
 
TIMER0_INTERRUPT

Options for the Interrupts for Timer-0

#define TIMER0_COMPARE_MATCH_A_INTERRUPT   (1<<OCIE0A)
 
#define TIMER0_COMPARE_MATCH_B_INTERRUPT   (1<<OCIE0B)
 
#define TIMER0_OVERFLOW_INTERRUPT   (1<<TOIE0)
 

Typedefs

TIMER0_CONFIGURATION_DATA

Structure used to Configure Timer-0

typedef struct TIMER0_ConfigData TIMER0_ConfigData
 

Functions

 ISR (TIMER0_OVF_vect)
 Interrupt Service Routine for Timer-0 Overflow. More...
 
 ISR (TIMER0_COMPA_vect)
 Interrupt Service Routine for Timer-0 Compare Match A. More...
 
 ISR (TIMER0_COMPB_vect)
 Interrupt Service Routine for Timer-0 Compare Match B. More...
 
TIMER0_OVERFLOW_INTERRUPT_CONTROL

Public Functions to Control Timer-0 Overflow Interrupts

void TIMER0_EnableOverflowInterrupt ()
 Public Function to Enable Timer-0 Overflow Interrupt. More...
 
void TIMER0_DisableOverflowInterrupt ()
 Public Function to Disable Timer-0 Overflow Interrupt. More...
 
TIMER0_COMPARE_MATCH_A_INTERRUPT_CONTROL

Public Functions to Control Timer-0 Compare Match A Interrupts

void TIMER0_EnableCompareMatchAInterrupt ()
 Public Function to Enable Timer-0 Compare Match A Interrupt. More...
 
void TIMER0_DisableCompareMatchAInterrupt ()
 Public Function to Disable Timer-0 Compare Match A Interrupt. More...
 
TIMER0_COMPARE_MATCH_B_INTERRUPT_CONTROL

Public Functions to Control Timer-0 Compare Match B Interrupts

void TIMER0_EnableCompareMatchBInterrupt ()
 Public Function to Enable Timer-0 Compare Match B Interrupt. More...
 
void TIMER0_DisableCompareMatchBInterrupt ()
 Public Function to Disable Timer-0 Compare Match B Interrupt. More...
 
TIMER0_INTERRUPT_CONTROL

Public Functions to Control Timer-0 Compare Match B Interrupts

void TIMER0_EnableAllInterrupt ()
 Public Function to Enable Timer-0 All Interrupt. More...
 
void TIMER0_DisableAllInterrupt ()
 Public Function to Disable Timer-0 All Interrupt. More...
 
TIMER0_CONFIGURATION_AND_CONTROL

Public Functions to Control and Configure Timer-0

void TIMER0_Init (TIMER0_ConfigData Data)
 Public Function to Configure and Initialize Timer-0. More...
 
void TIMER0_DeInit ()
 Public Function to De-Initialize Timer-0. More...
 
TIMER0_OCR0x_WRITE

Public Functions to Set OCR0x Registers of Timer-0

void TIMER0_SetOCR0A (uint8_t Value)
 Public Function to Set OCR0A value. More...
 
void TIMER0_SetOCR0B (uint8_t Value)
 Public Function to set OCR0B value. More...
 
TIMER0_SYSTEM_TICK_AND_TIME_READ

Public Functions to Read System Tick and Time updated by Timer-0

uint32_t TIMER0_GetSystemTick ()
 Public Function to Read current System Tick Count. More...
 
uint64_t TIMER0_GetSystemTimeUs ()
 Public Function to Read current System Time in Microsecond. More...
 
uint32_t TIMER0_GetSystemTimeMs ()
 Public Function to Read current System Time in Millisecond. More...
 
TIMER0_TICK_AND_TIME_CONVERSION

Public Functions to Convert between Timer-0 System Tick and Microsecond

uint32_t TIMER0_TickToUs (uint32_t Tick)
 Public Function to Convert Tick Count to Time in Microsecond. More...
 
uint16_t TIMER0_UsToTick (uint16_t Time)
 Public Function to convert Time in Microsecond to Tick Count. More...
 
TIMER0_GENERATE_DELAY

Public Functions to Generate Delays using Timer-0 System Tick

void TIMER0_DelayTick (uint16_t Tick)
 Public Function to Generate Blocking Delay in Tick Count. More...
 
void TIMER0_DelayUs (uint16_t Time)
 Public Function to Generate Blocking Delay in Microsecond. More...
 
void TIMER0_DelayMs (uint16_t Time)
 Public Function to Generate Blocking Delay in Millisecond. More...
 

Detailed Description

Library for 8-Bit Timer-0.

Created: 01-05-2020 08:20:10 PM
Author : Arnab Kumar Das
Website: www.ArnabKumarDas.com
Microcontroller Supported: ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI

                +-\/-+
          PC6  1|    |28  PC5 (A5/ADC5)
RXD  (D0) PD0  2|    |27  PC4 (A4/ADC4)
TXD  (D1) PD1  3|    |26  PC3 (A3/ADC3)
     (D2) PD2  4|    |25  PC2 (A2/ADC2)
PWM  (D3) PD3  5|    |24  PC1 (A1/ADC1)
XCK  (D4) PD4  6|    |23  PC0 (A0/ADC0)
          VCC  7|    |22  GND
          GND  8|    |21  AREF
          PB6  9|    |20  AVCC
          PB7 10|    |19  PB5 (D13)
OC0B (D5) PD5 11|    |18  PB4 (D12)
OC0A (D6) PD6 12|    |17  PB3 (D11) PWM
     (D7) PD7 13|    |16  PB2 (D10) PWM
     (D8) PB0 14|    |15  PB1 (D9)  PWM
                +----+
Note
This library supports Timer-0 based delay and PWM generation
This library supports System Tick Count in NORMAL, CTC, FAST PWM and PHASE CORRECT PWM MODE
The System Tick Count is used by the delay functions
System Tick Count Frequency depends on the Prescaler and Timer Mode
Recommended Timer-0 mode for System Tick Count in CTC mode.
System Tick Frequency should not be less than 5kHz and more than 100kHz Frequency
Minimum safe recommended delay is 10 microsecond

Macro Definition Documentation

◆ TIMER0_OC0A_NO_OUTPUT

#define TIMER0_OC0A_NO_OUTPUT   (0<<COM0A1) | (0<<COM0A0)

Sets the Compare Output Mode of Pin OC0A to : Normal Port Operation, Pin Disconnected

◆ TIMER0_OC0A_TOGGLE_ON_MATCH

#define TIMER0_OC0A_TOGGLE_ON_MATCH   (1<<COM0A0)

Sets the Compare Output Mode of Pin OC0A to : Toggle on Compare Match

◆ TIMER0_OC0A_CLEAR_ON_MATCH

#define TIMER0_OC0A_CLEAR_ON_MATCH   (1<<COM0A1)

Sets the Compare Output Mode of Pin OC0A to : Clear on Compare Match

◆ TIMER0_OC0A_SET_ON_MATCH

#define TIMER0_OC0A_SET_ON_MATCH   (1<<COM0A1) | (1<<COM0A0)

Sets the Compare Output Mode of Pin OC0A to : Set on Compare Match

◆ TIMER0_OC0A_NON_INVERTING

#define TIMER0_OC0A_NON_INVERTING   (1<<COM0A1)

Sets the Compare Output Mode of Pin OC0A to : Non-Inverting PWM Output

◆ TIMER0_OC0A_INVERTING

#define TIMER0_OC0A_INVERTING   (1<<COM0A1) | (1<<COM0A0)

Sets the Compare Output Mode of Pin OC0A to : Inverting PWM Output

◆ TIMER0_OC0A_MASK

#define TIMER0_OC0A_MASK   (1<<COM0A1) | (1<<COM0A0)

Mask for Timer-0 OC0A Compare Output Mode

◆ TIMER0_OC0B_NO_OUTPUT

#define TIMER0_OC0B_NO_OUTPUT   (0<<COM0B1) | (0<<COM0B0)

Sets the Compare Output Mode of Pin OC0B to : Normal Port Operation, Pin Disconnected

◆ TIMER0_OC0B_TOGGLE_ON_MATCH

#define TIMER0_OC0B_TOGGLE_ON_MATCH   (1<<COM0B0)

Sets the Compare Output Mode of Pin OC0B to : Toggle on Compare Match

◆ TIMER0_OC0B_CLEAR_ON_MATCH

#define TIMER0_OC0B_CLEAR_ON_MATCH   (1<<COM0B1)

Sets the Compare Output Mode of Pin OC0B to : Clear on Compare Match

◆ TIMER0_OC0B_SET_ON_MATCH

#define TIMER0_OC0B_SET_ON_MATCH   (1<<COM0B1) | (1<<COM0B0)

Sets the Compare Output Mode of Pin OC0B to : Set on Compare Match

◆ TIMER0_OC0B_NON_INVERTING

#define TIMER0_OC0B_NON_INVERTING   (1<<COM0B1)

Sets the Compare Output Mode of Pin OC0B to : Non-Inverting PWM Output

◆ TIMER0_OC0B_INVERTING

#define TIMER0_OC0B_INVERTING   (1<<COM0B1) | (1<<COM0B0)

Sets the Compare Output Mode of Pin OC0B to : Inverting PWM Output

◆ TIMER0_OC0B_MASK

#define TIMER0_OC0B_MASK   (1<<COM0B1) | (1<<COM0B0)

Mask for Timer-0 OC0B Compare Output Mode

◆ TIMER0_MODE_NORMAL

#define TIMER0_MODE_NORMAL   (0<<WGM01) | (0<<WGM00)

Sets the Timer/Counter Mode of Operation to : Normal TOP : 0xFF, Update of OCRx at : Immediate, TOV Flag Set on : MAX

◆ TIMER0_MODE_PHASE_CORRECT_PWM

#define TIMER0_MODE_PHASE_CORRECT_PWM   (1<<WGM00)

Sets the Timer/Counter Mode of Operation to : Phase Correct PWM, TOP : TIMER0_PWM_TOP_COUNT, Update of OCRx at : TOP, TOV Flag Set on : BOTTOM

◆ TIMER0_MODE_CTC

#define TIMER0_MODE_CTC   (1<<WGM01)

Sets the Timer/Counter Mode of Operation to : Clear Timer on Compare Match (CTC) Mode, TOP : OCRA, Update of OCRx at : Immediate, TOV Flag Set on : MAX

◆ TIMER0_MODE_FAST_PWM

#define TIMER0_MODE_FAST_PWM   (1<<WGM01) | (1<<WGM00)

Sets the Timer/Counter Mode of Operation to : Fast PWM Mode, TOP : TIMER0_PWM_TOP_COUNT, Update of OCRx at : BOTTOM, TOV Flag Set on : MAX/TOP

◆ TIMER0_MODE_MASK

#define TIMER0_MODE_MASK   (1<<WGM01) | (1<<WGM00)

Mask for Timer/Counter Mode of Operation

◆ TIMER0_PWM_TOP_0xFF

#define TIMER0_PWM_TOP_0xFF   (0<<WGM02)

Sets the TOP to : 0xFF in any PWM Mode

◆ TIMER0_PWM_TOP_OCRA

#define TIMER0_PWM_TOP_OCRA   (1<<WGM02)

Sets the TOP to : OCRA in any PWM Mode

◆ TIMER0_PWM_TOP_MASK

#define TIMER0_PWM_TOP_MASK   (1<<WGM02)

Mask for TOP value in any PWM Mode

◆ TIMER0_PRESCALER_0

#define TIMER0_PRESCALER_0   (0<<CS02) | (0<<CS01) | (0<<CS00)

No Clock Source : Timer/Counter Stopped

◆ TIMER0_PRESCALER_1

#define TIMER0_PRESCALER_1   (1<<CS00)

Sets the Timer-0 Clock Source to : F_CPU/1 : No Prescaling

◆ TIMER0_PRESCALER_8

#define TIMER0_PRESCALER_8   (1<<CS01)

Sets the Timer-0 Clock Source to : F_CPU/8 : System Clock Divided by 8

◆ TIMER0_PRESCALER_64

#define TIMER0_PRESCALER_64   (1<<CS01) | (1<<CS00)

Sets the Timer-0 Clock Source to : F_CPU/64 : System Clock Divided by 64

◆ TIMER0_PRESCALER_256

#define TIMER0_PRESCALER_256   (1<<CS02)

Sets the Timer-0 Clock Source to : F_CPU/256 : System Clock Divided by 256

◆ TIMER0_PRESCALER_1024

#define TIMER0_PRESCALER_1024   (1<<CS02) | (1<<CS00)

Sets the Timer-0 Clock Source to : F_CPU/1024 : System Clock Divided by 1024

◆ TIMER0_PRESCALER_MASK

#define TIMER0_PRESCALER_MASK   (1<<CS02) | (1<<CS01) | (1<<CS00)

Mask for Timer-0 Prescaler

◆ TIMER0_COMPARE_MATCH_A_INTERRUPT

#define TIMER0_COMPARE_MATCH_A_INTERRUPT   (1<<OCIE0A)

Timer-0 Interrupt Control Bit : Compare Match A

◆ TIMER0_COMPARE_MATCH_B_INTERRUPT

#define TIMER0_COMPARE_MATCH_B_INTERRUPT   (1<<OCIE0B)

Timer-0 Interrupt Control Bit : Compare Match B

◆ TIMER0_OVERFLOW_INTERRUPT

#define TIMER0_OVERFLOW_INTERRUPT   (1<<TOIE0)

Timer-0 Interrupt Control Bit : Timer Overflow

Function Documentation

◆ TIMER0_EnableOverflowInterrupt()

void TIMER0_EnableOverflowInterrupt ( )
inline

Public Function to Enable Timer-0 Overflow Interrupt.

Returns
void

◆ TIMER0_DisableOverflowInterrupt()

void TIMER0_DisableOverflowInterrupt ( )
inline

Public Function to Disable Timer-0 Overflow Interrupt.

Returns
void

◆ TIMER0_EnableCompareMatchAInterrupt()

void TIMER0_EnableCompareMatchAInterrupt ( )
inline

Public Function to Enable Timer-0 Compare Match A Interrupt.

Returns
void

◆ TIMER0_DisableCompareMatchAInterrupt()

void TIMER0_DisableCompareMatchAInterrupt ( )
inline

Public Function to Disable Timer-0 Compare Match A Interrupt.

Returns
void

◆ TIMER0_EnableCompareMatchBInterrupt()

void TIMER0_EnableCompareMatchBInterrupt ( )
inline

Public Function to Enable Timer-0 Compare Match B Interrupt.

Returns
void

◆ TIMER0_DisableCompareMatchBInterrupt()

void TIMER0_DisableCompareMatchBInterrupt ( )
inline

Public Function to Disable Timer-0 Compare Match B Interrupt.

Returns
void

◆ TIMER0_EnableAllInterrupt()

void TIMER0_EnableAllInterrupt ( )
inline

Public Function to Enable Timer-0 All Interrupt.

Returns
void

◆ TIMER0_DisableAllInterrupt()

void TIMER0_DisableAllInterrupt ( )
inline

Public Function to Disable Timer-0 All Interrupt.

Returns
void

◆ TIMER0_Init()

void TIMER0_Init ( TIMER0_ConfigData  Data)

Public Function to Configure and Initialize Timer-0.

Parameters
[in]Data: Structure Variable containing Timer-0 Configuration Data
Returns
void

◆ TIMER0_DeInit()

void TIMER0_DeInit ( )

Public Function to De-Initialize Timer-0.

Returns
void

◆ TIMER0_SetOCR0A()

void TIMER0_SetOCR0A ( uint8_t  Value)
inline

Public Function to Set OCR0A value.

Parameters
[in]Value: Variable to Store OCR0A Register value
Returns
void

◆ TIMER0_SetOCR0B()

void TIMER0_SetOCR0B ( uint8_t  Value)
inline

Public Function to set OCR0B value.

Parameters
[in]Value: Variable to Store OCR0B Register value
Returns
void

◆ TIMER0_GetSystemTick()

uint32_t TIMER0_GetSystemTick ( )

Public Function to Read current System Tick Count.

Returns
Timer-0 System Tick

◆ TIMER0_GetSystemTimeUs()

uint64_t TIMER0_GetSystemTimeUs ( )

Public Function to Read current System Time in Microsecond.

Returns
Timer-0 System Time in Microsecond

◆ TIMER0_GetSystemTimeMs()

uint32_t TIMER0_GetSystemTimeMs ( )

Public Function to Read current System Time in Millisecond.

Returns
Timer-0 System Time in Millisecond

◆ TIMER0_TickToUs()

uint32_t TIMER0_TickToUs ( uint32_t  Tick)

Public Function to Convert Tick Count to Time in Microsecond.

Parameters
[in]Tick: Variable to Store Tick Count
Returns
Time in Microsecond

◆ TIMER0_UsToTick()

uint16_t TIMER0_UsToTick ( uint16_t  Time)

Public Function to convert Time in Microsecond to Tick Count.

Parameters
[in]Time: Variable to Store Time in Microsecond
Returns
Tick Count

◆ TIMER0_DelayTick()

void TIMER0_DelayTick ( uint16_t  Tick)

Public Function to Generate Blocking Delay in Tick Count.

Parameters
[in]Tick: Variable to Store Tick Count
Returns
void

◆ TIMER0_DelayUs()

void TIMER0_DelayUs ( uint16_t  Time)

Public Function to Generate Blocking Delay in Microsecond.

Parameters
[in]Time: Variable to Store Time in Microsecond
Returns
void
Note
Minimum safe Delay is 50 Microsecond with increments of 10 Microsecond

◆ TIMER0_DelayMs()

void TIMER0_DelayMs ( uint16_t  Time)

Public Function to Generate Blocking Delay in Millisecond.

Parameters
[in]Time: Variable to Store Time in Millisecond
Returns
void

◆ ISR() [1/3]

ISR ( TIMER0_OVF_vect  )

Interrupt Service Routine for Timer-0 Overflow.

Parameters
[in]TIMER0_OVF_vect

◆ ISR() [2/3]

ISR ( TIMER0_COMPA_vect  )

Interrupt Service Routine for Timer-0 Compare Match A.

Parameters
[in]TIMER0_COMPA_vect

◆ ISR() [3/3]

ISR ( TIMER0_COMPB_vect  )

Interrupt Service Routine for Timer-0 Compare Match B.

Parameters
[in]TIMER0_COMPB_vect