45 #include <avr/interrupt.h> 49 #pragma message ( "avr_timer0.h included" ) 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 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();
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()
227 TIMSK0 |= TIMER0_OVERFLOW_INTERRUPT;
235 inline void TIMER0_DisableOverflowInterrupt()
237 TIMSK0 &= ~TIMER0_OVERFLOW_INTERRUPT;
245 inline void TIMER0_EnableCompareMatchAInterrupt()
247 TIMSK0 |= TIMER0_COMPARE_MATCH_A_INTERRUPT;
255 inline void TIMER0_DisableCompareMatchAInterrupt()
257 TIMSK0 &= ~TIMER0_COMPARE_MATCH_A_INTERRUPT;
265 inline void TIMER0_EnableCompareMatchBInterrupt()
267 TIMSK0 |= TIMER0_COMPARE_MATCH_B_INTERRUPT;
275 inline void TIMER0_DisableCompareMatchBInterrupt()
277 TIMSK0 &= ~TIMER0_COMPARE_MATCH_B_INTERRUPT;
285 inline void TIMER0_EnableAllInterrupt()
287 TIMSK0 |= TIMER0_COMPARE_MATCH_B_INTERRUPT | TIMER0_COMPARE_MATCH_A_INTERRUPT | TIMER0_OVERFLOW_INTERRUPT;
295 inline void TIMER0_DisableAllInterrupt()
297 TIMSK0 &= ~(TIMER0_COMPARE_MATCH_B_INTERRUPT | TIMER0_COMPARE_MATCH_A_INTERRUPT | TIMER0_OVERFLOW_INTERRUPT);
306 void TIMER0_Init(TIMER0_ConfigData Data)
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;
314 uint32_t TIMER0_InputClockFrequency = 0;
317 if(Data.Prescaler == TIMER0_PRESCALER_1)
319 TIMER0_InputClockFrequency =
F_CPU / 1;
321 else if(Data.Prescaler == TIMER0_PRESCALER_8)
323 TIMER0_InputClockFrequency = F_CPU / 8;
327 TIMER0_InputClockFrequency = F_CPU / (2 ^ Data.Prescaler);
331 if (Data.SystemTickState == ENABLE)
333 if(Data.TimerMode == (TIMER0_MODE_NORMAL))
335 TIMER0_SystemTickFrequency = 2 * TIMER0_InputClockFrequency / 512;
336 TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
337 TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
338 TIMER0_EnableOverflowInterrupt();
340 else if(Data.TimerMode == (TIMER0_MODE_CTC))
342 TIMER0_SystemTickFrequency = F_100kHz;
343 OCR0A = (uint8_t)(TIMER0_InputClockFrequency / TIMER0_SystemTickFrequency) - 1;
344 TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
345 TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
346 TIMER0_EnableCompareMatchAInterrupt();
348 else if(Data.TimerMode == (TIMER0_MODE_FAST_PWM))
350 TIMER0_SystemTickFrequency = TIMER0_InputClockFrequency / 256;
351 TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
352 TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
353 TIMER0_EnableOverflowInterrupt();
355 else if(Data.TimerMode == (TIMER0_MODE_PHASE_CORRECT_PWM))
357 TIMER0_SystemTickFrequency = TIMER0_InputClockFrequency / 510;
358 TIMER0_SystemTickTimePeriodUs = (uint8_t)(F_1Mhz / TIMER0_SystemTickFrequency);
359 TIMER0_SystemTickPerMs = (uint8_t)(TIMER0_SystemTickFrequency / F_1kHz);
360 TIMER0_EnableOverflowInterrupt();
364 if (Data.OutputCompareModePinA != (TIMER0_OC0A_NO_OUTPUT))
370 if (Data.OutputCompareModePinB != (TIMER0_OC0B_NO_OUTPUT))
392 TIMER0_DisableAllInterrupt();
401 inline void TIMER0_SetOCR0A(uint8_t Value)
412 inline void TIMER0_SetOCR0B(uint8_t Value)
422 uint32_t TIMER0_GetSystemTick()
424 return TIMER0_SystemTickCount;
432 uint64_t TIMER0_GetSystemTimeUs()
434 return (uint64_t)(TIMER0_SystemTickCount * TIMER0_SystemTickTimePeriodUs);
442 uint32_t TIMER0_GetSystemTimeMs()
444 return (uint32_t)((TIMER0_SystemTickCount * F_1kHz) / TIMER0_SystemTickFrequency);
453 uint32_t TIMER0_TickToUs(uint32_t Tick)
455 return (uint32_t)(Tick * TIMER0_SystemTickTimePeriodUs);
464 uint16_t TIMER0_UsToTick(uint16_t Time)
466 return (uint16_t)(Time / TIMER0_SystemTickTimePeriodUs);
475 void TIMER0_DelayTick(uint16_t Tick)
477 TIMER0_DisableAllInterrupt();
478 uint32_t LocalTick = TIMER0_SystemTickCount + Tick;
479 if((TCCR0A & (TIMER0_MODE_MASK)) == TIMER0_MODE_CTC)
481 TIMER0_EnableCompareMatchAInterrupt();
485 TIMER0_EnableOverflowInterrupt();
487 while(TIMER0_SystemTickCount != LocalTick);
497 void TIMER0_DelayUs(uint16_t Time)
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)
504 TIMER0_EnableCompareMatchAInterrupt();
508 TIMER0_EnableOverflowInterrupt();
510 while(TIMER0_SystemTickCount != LocalTime);
519 void TIMER0_DelayMs(uint16_t Time)
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)
526 TIMER0_EnableCompareMatchAInterrupt();
530 TIMER0_EnableOverflowInterrupt();
532 while(TIMER0_SystemTickCount != LocalTime);
542 TIMER0_SystemTickCount++;
552 TIMER0_SystemTickCount++;
إرسال تعليق