avr_timer0.h

 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;
143 } TIMER0_ConfigData;
150 inline void TIMER0_EnableOverflowInterrupt();
151 inline void TIMER0_DisableOverflowInterrupt();
158 inline void TIMER0_EnableCompareMatchAInterrupt();
159 inline void TIMER0_DisableCompareMatchAInterrupt();
166 inline void TIMER0_EnableCompareMatchBInterrupt();
167 inline void TIMER0_DisableCompareMatchBInterrupt();
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 
235 inline void TIMER0_DisableOverflowInterrupt()
236 {
237  TIMSK0 &= ~TIMER0_OVERFLOW_INTERRUPT;
238 }
239 
245 inline void TIMER0_EnableCompareMatchAInterrupt()
246 {
247  TIMSK0 |= TIMER0_COMPARE_MATCH_A_INTERRUPT;
248 }
249 
255 inline void TIMER0_DisableCompareMatchAInterrupt()
256 {
257  TIMSK0 &= ~TIMER0_COMPARE_MATCH_A_INTERRUPT;
258 }
259 
265 inline void TIMER0_EnableCompareMatchBInterrupt()
266 {
267  TIMSK0 |= TIMER0_COMPARE_MATCH_B_INTERRUPT;
268 }
269 
275 inline void TIMER0_DisableCompareMatchBInterrupt()
276 {
277  TIMSK0 &= ~TIMER0_COMPARE_MATCH_B_INTERRUPT;
278 }
279 
285 inline void TIMER0_EnableAllInterrupt()
286 {
287  TIMSK0 |= TIMER0_COMPARE_MATCH_B_INTERRUPT | TIMER0_COMPARE_MATCH_A_INTERRUPT | TIMER0_OVERFLOW_INTERRUPT;
288 }
289 
295 inline void TIMER0_DisableAllInterrupt()
296 {
297  TIMSK0 &= ~(TIMER0_COMPARE_MATCH_B_INTERRUPT | TIMER0_COMPARE_MATCH_A_INTERRUPT | TIMER0_OVERFLOW_INTERRUPT);
298 }
299 
306 void TIMER0_Init(TIMER0_ConfigData Data)
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);
338  TIMER0_EnableOverflowInterrupt();
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);
346  TIMER0_EnableCompareMatchAInterrupt();
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);
353  TIMER0_EnableOverflowInterrupt();
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);
360  TIMER0_EnableOverflowInterrupt();
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 
385 void TIMER0_DeInit()
386 {
387  TCCR0B = 0x00;
388  TCCR0A = 0x00;
389  TCNT0 = 0x00;
390  OCR0A = 0x00;
391  OCR0B = 0x00;
392  TIMER0_DisableAllInterrupt();
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 
422 uint32_t TIMER0_GetSystemTick()
423 {
424  return TIMER0_SystemTickCount;
425 }
426 
432 uint64_t TIMER0_GetSystemTimeUs()
433 {
434  return (uint64_t)(TIMER0_SystemTickCount * TIMER0_SystemTickTimePeriodUs);
435 }
436 
442 uint32_t TIMER0_GetSystemTimeMs()
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 {
477  TIMER0_DisableAllInterrupt();
478  uint32_t LocalTick = TIMER0_SystemTickCount + Tick;
479  if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
480  {
481  TIMER0_EnableCompareMatchAInterrupt();
482  }
483  else
484  {
485  TIMER0_EnableOverflowInterrupt();
486  }
487  while(TIMER0_SystemTickCount != LocalTick);
488 }
489 
497 void TIMER0_DelayUs(uint16_t Time)
498 {
499  TIMER0_DisableAllInterrupt();
500  uint32_t Count = Time / TIMER0_SystemTickTimePeriodUs;
501  uint32_t LocalTime = TIMER0_SystemTickCount + Count;
502  if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
503  {
504  TIMER0_EnableCompareMatchAInterrupt();
505  }
506  else
507  {
508  TIMER0_EnableOverflowInterrupt();
509  }
510  while(TIMER0_SystemTickCount != LocalTime);
511 }
512 
519 void TIMER0_DelayMs(uint16_t Time)
520 {
521  TIMER0_DisableAllInterrupt();
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  {
526  TIMER0_EnableCompareMatchAInterrupt();
527  }
528  else
529  {
530  TIMER0_EnableOverflowInterrupt();
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_ */

Post a Comment

Post a Comment (0)

Previous Post Next Post