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
Go to the documentation of this file.
1 
41 #ifndef AVR_TIMER0_H_
42 #define AVR_TIMER0_H_
43 
44 #include <avr/io.h>
45 #include <avr/interrupt.h>
46 #include "avr_macros.h"
47 #include "avr_utils.h"
48 
49 #pragma message ( "avr_timer0.h included" )
50 
55 #define TIMER0_OC0A_NO_OUTPUT (0<<COM0A1) | (0<<COM0A0)
56 #define TIMER0_OC0A_TOGGLE_ON_MATCH (1<<COM0A0)
57 #define TIMER0_OC0A_CLEAR_ON_MATCH (1<<COM0A1)
58 #define TIMER0_OC0A_SET_ON_MATCH (1<<COM0A1) | (1<<COM0A0)
59 #define TIMER0_OC0A_NON_INVERTING (1<<COM0A1)
60 #define TIMER0_OC0A_INVERTING (1<<COM0A1) | (1<<COM0A0)
61 #define TIMER0_OC0A_MASK (1<<COM0A1) | (1<<COM0A0)
68 #define TIMER0_OC0B_NO_OUTPUT (0<<COM0B1) | (0<<COM0B0)
69 #define TIMER0_OC0B_TOGGLE_ON_MATCH (1<<COM0B0)
70 #define TIMER0_OC0B_CLEAR_ON_MATCH (1<<COM0B1)
71 #define TIMER0_OC0B_SET_ON_MATCH (1<<COM0B1) | (1<<COM0B0)
72 #define TIMER0_OC0B_NON_INVERTING (1<<COM0B1)
73 #define TIMER0_OC0B_INVERTING (1<<COM0B1) | (1<<COM0B0)
74 #define TIMER0_OC0B_MASK (1<<COM0B1) | (1<<COM0B0)
81 #define TIMER0_MODE_NORMAL (0<<WGM01) | (0<<WGM00)
82 #define TIMER0_MODE_PHASE_CORRECT_PWM (1<<WGM00)
83 #define TIMER0_MODE_CTC (1<<WGM01)
84 #define TIMER0_MODE_FAST_PWM (1<<WGM01) | (1<<WGM00)
85 #define TIMER0_MODE_MASK (1<<WGM01) | (1<<WGM00)
92 #define TIMER0_PWM_TOP_0xFF (0<<WGM02)
93 #define TIMER0_PWM_TOP_OCRA (1<<WGM02)
94 #define TIMER0_PWM_TOP_MASK (1<<WGM02)
101 #define TIMER0_PRESCALER_0 (0<<CS02) | (0<<CS01) | (0<<CS00)
102 #define TIMER0_PRESCALER_1 (1<<CS00)
103 #define TIMER0_PRESCALER_8 (1<<CS01)
104 #define TIMER0_PRESCALER_64 (1<<CS01) | (1<<CS00)
105 #define TIMER0_PRESCALER_256 (1<<CS02)
106 #define TIMER0_PRESCALER_1024 (1<<CS02) | (1<<CS00)
107 #define TIMER0_PRESCALER_MASK (1<<CS02) | (1<<CS01) | (1<<CS00)
114 #define TIMER0_COMPARE_MATCH_A_INTERRUPT (1<<OCIE0A)
115 #define TIMER0_COMPARE_MATCH_B_INTERRUPT (1<<OCIE0B)
116 #define TIMER0_OVERFLOW_INTERRUPT (1<<TOIE0)
123 static volatile uint32_t TIMER0_SystemTickCount = 0;
124 static volatile uint32_t TIMER0_SystemTickFrequency = 0;
125 static volatile uint8_t TIMER0_SystemTickTimePeriodUs = 0;
126 static volatile uint8_t TIMER0_SystemTickPerMs = 0;
133 typedef struct TIMER0_ConfigData
134 {
135  uint8_t TimerMode;
136  uint8_t Prescaler;
137  uint8_t OutputCompareModePinA;
138  uint8_t OutputCompareModePinB;
139  uint8_t OutputCompareRegisterA;
140  uint8_t OutputCompareRegisterB;
141  uint8_t TimerCounterRegister;
142  uint8_t SystemTickState;
150 inline void TIMER0_EnableOverflowInterrupt();
151 inline void TIMER0_DisableOverflowInterrupt();
174 inline void TIMER0_EnableAllInterrupt();
175 inline void TIMER0_DisableAllInterrupt();
182 void TIMER0_Init(TIMER0_ConfigData Data);
183 void TIMER0_DeInit();
190 inline void TIMER0_SetOCR0A(uint8_t Value);
191 inline void TIMER0_SetOCR0B(uint8_t Value);
198 uint32_t TIMER0_GetSystemTick();
199 uint64_t TIMER0_GetSystemTimeUs();
200 uint32_t TIMER0_GetSystemTimeMs();
207 uint32_t TIMER0_TickToUs(uint32_t Tick);
208 uint16_t TIMER0_UsToTick(uint16_t Time);
215 void TIMER0_DelayTick(uint16_t Tick);
216 void TIMER0_DelayUs(uint16_t Time);
217 void TIMER0_DelayMs(uint16_t Time);
225 inline void TIMER0_EnableOverflowInterrupt() /* Timer-0 Sample 1 */
226 {
227  TIMSK0 |= TIMER0_OVERFLOW_INTERRUPT;
228 }
229 
236 {
237  TIMSK0 &= ~TIMER0_OVERFLOW_INTERRUPT;
238 }
239 
246 {
248 }
249 
256 {
258 }
259 
266 {
268 }
269 
276 {
278 }
279 
286 {
288 }
289 
296 {
298 }
299 
307 {
308  TCCR0A = Data.OutputCompareModePinA | Data.OutputCompareModePinB | Data.TimerMode;
309  TCCR0B = TIMER0_PWM_TOP_0xFF | Data.Prescaler;
310  TCNT0 = Data.TimerCounterRegister;
311  OCR0A = Data.OutputCompareRegisterA;
312  OCR0B = Data.OutputCompareRegisterB;
313 
314  uint32_t TIMER0_InputClockFrequency = 0;
315 
316  /* Computes the Input Clock Frequency for Timer-0 */
317  if(Data.Prescaler == TIMER0_PRESCALER_1)
318  {
319  TIMER0_InputClockFrequency = F_CPU / 1;
320  }
321  else if(Data.Prescaler == TIMER0_PRESCALER_8)
322  {
323  TIMER0_InputClockFrequency = F_CPU / 8;
324  }
325  else
326  {
327  TIMER0_InputClockFrequency = F_CPU / (2 ^ Data.Prescaler);
328  }
329 
330  /* Computes the System Tick Frequency for Timer-0 */
331  if (Data.SystemTickState == ENABLE)
332  {
333  if(Data.TimerMode == (TIMER0_MODE_NORMAL))
334  {
335  TIMER0_SystemTickFrequency = 2 * TIMER0_InputClockFrequency / 512; /* Computes the Tick Count Frequency for Timer-0 in NORMAL MODE */
336  TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
337  TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
339  }
340  else if(Data.TimerMode == (TIMER0_MODE_CTC))
341  {
342  TIMER0_SystemTickFrequency = F_100kHz; /* Sets the Tick Count Frequency for Timer-0 in CTC MODE */
343  OCR0A = (uint8_t)(TIMER0_InputClockFrequency / TIMER0_SystemTickFrequency) - 1; /* OCR0A Count Calculation Formula for Timer-0 in CTC Mode*/
344  TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
345  TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
347  }
348  else if(Data.TimerMode == (TIMER0_MODE_FAST_PWM))
349  {
350  TIMER0_SystemTickFrequency = TIMER0_InputClockFrequency / 256; /* Computes the Tick Count Frequency for Timer-0 in FAST PWM MODE */
351  TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
352  TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
354  }
355  else if(Data.TimerMode == (TIMER0_MODE_PHASE_CORRECT_PWM))
356  {
357  TIMER0_SystemTickFrequency = TIMER0_InputClockFrequency / 510; /* Computes the Tick Count Frequency for Timer-0 in PHASE CORRECT PWM MODE */
358  TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
359  TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
361  }
362  }
363 
364  if (Data.OutputCompareModePinA != (TIMER0_OC0A_NO_OUTPUT))
365  {
366  DDRD |= (1<<5);
367 
368  }
369 
370  if (Data.OutputCompareModePinB != (TIMER0_OC0B_NO_OUTPUT))
371  {
372 
373  DDRD |= (1<<6);
374  }
375 
376  /* Enables Global Interrupts */
377  sei();
378 }
379 
386 {
387  TCCR0B = 0x00;
388  TCCR0A = 0x00;
389  TCNT0 = 0x00;
390  OCR0A = 0x00;
391  OCR0B = 0x00;
393 }
394 
401 inline void TIMER0_SetOCR0A(uint8_t Value)
402 {
403  OCR0A = Value;
404 }
405 
412 inline void TIMER0_SetOCR0B(uint8_t Value)
413 {
414  OCR0B = Value;
415 }
416 
423 {
424  return TIMER0_SystemTickCount;
425 }
426 
433 {
434  return (uint64_t)(TIMER0_SystemTickCount * TIMER0_SystemTickTimePeriodUs);
435 }
436 
443 {
444  return (uint32_t)((TIMER0_SystemTickCount * F_1kHz) / TIMER0_SystemTickFrequency);
445 }
446 
453 uint32_t TIMER0_TickToUs(uint32_t Tick)
454 {
455  return (uint32_t)(Tick * TIMER0_SystemTickTimePeriodUs);
456 }
457 
464 uint16_t TIMER0_UsToTick(uint16_t Time)
465 {
466  return (uint16_t)(Time / TIMER0_SystemTickTimePeriodUs);
467 }
468 
475 void TIMER0_DelayTick(uint16_t Tick)
476 {
478  uint32_t LocalTick = TIMER0_SystemTickCount + Tick;
479  if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
480  {
482  }
483  else
484  {
486  }
487  while(TIMER0_SystemTickCount != LocalTick);
488 }
489 
497 void TIMER0_DelayUs(uint16_t Time)
498 {
500  uint32_t Count = Time / TIMER0_SystemTickTimePeriodUs;
501  uint32_t LocalTime = TIMER0_SystemTickCount + Count;
502  if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
503  {
505  }
506  else
507  {
509  }
510  while(TIMER0_SystemTickCount != LocalTime);
511 }
512 
519 void TIMER0_DelayMs(uint16_t Time)
520 {
522  uint32_t Count = (uint32_t)Time * (uint32_t)TIMER0_SystemTickPerMs;
523  uint32_t LocalTime = TIMER0_SystemTickCount + Count;
524  if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
525  {
527  }
528  else
529  {
531  }
532  while(TIMER0_SystemTickCount != LocalTime);
533 }
534 
540 ISR(TIMER0_OVF_vect) // 1.375us
541 {
542  TIMER0_SystemTickCount++;
543 }
544 
550 ISR(TIMER0_COMPA_vect) // 1.5 + 1.41us + 1.41 Takes Total 4.33uS
551 {
552  TIMER0_SystemTickCount++;
553 }
554 
560 ISR(TIMER0_COMPB_vect)
561 {
562 
563 }
564 
565 #endif /* AVR_TIMER0_H_ */
F_1kHz
#define F_1kHz
Definition: avr_macros.h:46
TIMER0_OC0A_NO_OUTPUT
#define TIMER0_OC0A_NO_OUTPUT
Definition: avr_timer0.h:55
TIMER0_SetOCR0B
void TIMER0_SetOCR0B(uint8_t Value)
Public Function to set OCR0B value.
Definition: avr_timer0.h:412
TIMER0_OVERFLOW_INTERRUPT
#define TIMER0_OVERFLOW_INTERRUPT
Definition: avr_timer0.h:116
TIMER0_DisableCompareMatchBInterrupt
void TIMER0_DisableCompareMatchBInterrupt()
Public Function to Disable Timer-0 Compare Match B Interrupt.
Definition: avr_timer0.h:275
TIMER0_SetOCR0A
void TIMER0_SetOCR0A(uint8_t Value)
Public Function to Set OCR0A value.
Definition: avr_timer0.h:401
TIMER0_UsToTick
uint16_t TIMER0_UsToTick(uint16_t Time)
Public Function to convert Time in Microsecond to Tick Count.
Definition: avr_timer0.h:464
TIMER0_GetSystemTick
uint32_t TIMER0_GetSystemTick()
Public Function to Read current System Tick Count.
Definition: avr_timer0.h:422
avr_utils.h
Library for Utility Functions.
TIMER0_Init
void TIMER0_Init(TIMER0_ConfigData Data)
Public Function to Configure and Initialize Timer-0.
Definition: avr_timer0.h:306
TIMER0_PRESCALER_1
#define TIMER0_PRESCALER_1
Definition: avr_timer0.h:102
TIMER0_MODE_FAST_PWM
#define TIMER0_MODE_FAST_PWM
Definition: avr_timer0.h:84
TIMER0_DelayMs
void TIMER0_DelayMs(uint16_t Time)
Public Function to Generate Blocking Delay in Millisecond.
Definition: avr_timer0.h:519
TIMER0_GetSystemTimeMs
uint32_t TIMER0_GetSystemTimeMs()
Public Function to Read current System Time in Millisecond.
Definition: avr_timer0.h:442
TIMER0_EnableCompareMatchAInterrupt
void TIMER0_EnableCompareMatchAInterrupt()
Public Function to Enable Timer-0 Compare Match A Interrupt.
Definition: avr_timer0.h:245
TIMER0_EnableOverflowInterrupt
void TIMER0_EnableOverflowInterrupt()
Public Function to Enable Timer-0 Overflow Interrupt.
Definition: avr_timer0.h:225
TIMER0_COMPARE_MATCH_B_INTERRUPT
#define TIMER0_COMPARE_MATCH_B_INTERRUPT
Definition: avr_timer0.h:115
TIMER0_MODE_PHASE_CORRECT_PWM
#define TIMER0_MODE_PHASE_CORRECT_PWM
Definition: avr_timer0.h:82
avr_macros.h
Library for all common Macro Definition.
TIMER0_DisableCompareMatchAInterrupt
void TIMER0_DisableCompareMatchAInterrupt()
Public Function to Disable Timer-0 Compare Match A Interrupt.
Definition: avr_timer0.h:255
TIMER0_TickToUs
uint32_t TIMER0_TickToUs(uint32_t Tick)
Public Function to Convert Tick Count to Time in Microsecond.
Definition: avr_timer0.h:453
F_CPU
#define F_CPU
Definition: avr_macros.h:39
TIMER0_GetSystemTimeUs
uint64_t TIMER0_GetSystemTimeUs()
Public Function to Read current System Time in Microsecond.
Definition: avr_timer0.h:432
TIMER0_COMPARE_MATCH_A_INTERRUPT
#define TIMER0_COMPARE_MATCH_A_INTERRUPT
Definition: avr_timer0.h:114
TIMER0_DeInit
void TIMER0_DeInit()
Public Function to De-Initialize Timer-0.
Definition: avr_timer0.h:385
TIMER0_DisableAllInterrupt
void TIMER0_DisableAllInterrupt()
Public Function to Disable Timer-0 All Interrupt.
Definition: avr_timer0.h:295
TIMER0_ConfigData
Definition: avr_timer0.h:134
TIMER0_EnableCompareMatchBInterrupt
void TIMER0_EnableCompareMatchBInterrupt()
Public Function to Enable Timer-0 Compare Match B Interrupt.
Definition: avr_timer0.h:265
TIMER0_MODE_CTC
#define TIMER0_MODE_CTC
Definition: avr_timer0.h:83
TIMER0_DisableOverflowInterrupt
void TIMER0_DisableOverflowInterrupt()
Public Function to Disable Timer-0 Overflow Interrupt.
Definition: avr_timer0.h:235
TIMER0_EnableAllInterrupt
void TIMER0_EnableAllInterrupt()
Public Function to Enable Timer-0 All Interrupt.
Definition: avr_timer0.h:285
TIMER0_DelayTick
void TIMER0_DelayTick(uint16_t Tick)
Public Function to Generate Blocking Delay in Tick Count.
Definition: avr_timer0.h:475
TIMER0_PWM_TOP_0xFF
#define TIMER0_PWM_TOP_0xFF
Definition: avr_timer0.h:92
F_100kHz
#define F_100kHz
Definition: avr_macros.h:52
TIMER0_PRESCALER_8
#define TIMER0_PRESCALER_8
Definition: avr_timer0.h:103
TIMER0_MODE_NORMAL
#define TIMER0_MODE_NORMAL
Definition: avr_timer0.h:81
TIMER0_OC0B_NO_OUTPUT
#define TIMER0_OC0B_NO_OUTPUT
Definition: avr_timer0.h:68
ISR
ISR(TIMER0_OVF_vect)
Interrupt Service Routine for Timer-0 Overflow.
Definition: avr_timer0.h:540
TIMER0_DelayUs
void TIMER0_DelayUs(uint16_t Time)
Public Function to Generate Blocking Delay in Microsecond.
Definition: avr_timer0.h:497
F_1Mhz
#define F_1Mhz
Definition: avr_macros.h:56
TIMER0_MODE_MASK
#define TIMER0_MODE_MASK
Definition: avr_timer0.h:85