ButterflyMP3

timer0.c

Go to the documentation of this file.
00001 
00020 //mtA
00021 //#include <inavr.h>
00022 //#include "iom169.h"
00023 #include <avr/io.h>
00024 #include <inttypes.h>
00025 //#include <avr/signal.h>//This header file is obsolete.  Use <avr/interrupt.h>.
00026 #include <avr/interrupt.h>
00027 //mtE
00028 #include "main.h"
00029 #include "types.h"
00030 #include "timer0.h"
00031 
00032 TIMER_CALLBACK_FUNC CallbackFunc[TIMER0_NUM_CALLBACKS];
00033 
00034 // Value definition:
00035 // 0      The timer has expired
00036 // 1-254  The timer is counting down
00037 // 255    Free timer
00038 
00039 // mt char CountDownTimers[TIMER0_NUM_COUNTDOWNTIMERS];
00040 uint8_t CountDownTimers[TIMER0_NUM_COUNTDOWNTIMERS];
00041 
00042 /*****************************************************************************
00043 *
00044 *   Function name : Timer0_Init
00045 *
00046 *   Purpose :       Initialize Timer/Counter 0
00047 *
00048 *****************************************************************************/
00049 void Timer0_Init(void)
00050 {
00051     //mt char i;
00052         uint8_t i;
00053 
00054     // Initialize array of callback functions
00055     for (i=0; i<TIMER0_NUM_CALLBACKS; i++)
00056         CallbackFunc[i] = NULL;
00057 
00058     // Initialize countdown timers
00059     for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
00060         CountDownTimers[i] = 255;
00061 
00062 
00063         // Initialize Timer0.
00064         // Used to give the correct time-delays in the song
00065 
00066     // Enable timer0 compare interrupt
00067         TIMSK0 = (1<<OCIE0A);
00068 
00069     // Sets the compare value
00070         OCR0A = 31;
00071 
00072         // Set Clear on Timer Compare (CTC) mode, CLK/256 prescaler
00073         TCCR0A = (1<<WGM01)|(0<<WGM00)|(4<<CS00);
00074     // (8000000/256) / 31  =1008.06451612903Hz or 0.992ms 
00075     
00076         // Set Clear on Timer Compare (CTC) mode, CLK/1024 prescaler
00077         // TCCR0A = (1<<WGM01)|(0<<WGM00)|(5<<CS00);
00078         // (8000000/1024) / 31  = 252.01612903226Hz or 3.968ms 
00079     
00080 }
00081 
00082 
00083 
00084 /*****************************************************************************
00085 *
00086 *   Function name : TIMER0_COMP_interrupt
00087 *
00088 *   Purpose :       Check if any functions are to be called
00089 *
00090 *****************************************************************************/
00091 // mtA
00092 // #pragma vector = TIMER0_COMP_vect
00093 // __interrupt void TIMER0_COMP_interrupt(void)
00094 SIGNAL(SIG_OUTPUT_COMPARE0)
00095 // mtE
00096 {
00097     // mt char i;
00098         uint8_t i;
00099     
00100     for (i=0; i<TIMER0_NUM_CALLBACKS; i++)
00101         if (CallbackFunc[i] != NULL)
00102             CallbackFunc[i]();
00103     
00104     // Count down timers
00105     for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
00106         if (CountDownTimers[i] != 255 && CountDownTimers[i] != 0)
00107             CountDownTimers[i]--;
00108 
00109 }
00110 
00111 
00112 /*****************************************************************************
00113 *
00114 *   Function name : Timer0_RegisterCallbackFunction
00115 *
00116 *   @param          pFunc pointer to function to add
00117 *
00118 *   Purpose :       Set up functions to be called from the 
00119 *                   TIMER0_COMP_interrupt
00120 *
00121 *****************************************************************************/
00122 BOOL Timer0_RegisterCallbackFunction(TIMER_CALLBACK_FUNC pFunc)
00123 {
00124     // mt char i;
00125         uint8_t i;
00126     
00127     for (i=0; i<TIMER0_NUM_CALLBACKS; i++)
00128     {
00129         if (CallbackFunc[i] == pFunc)
00130             return TRUE;
00131     }
00132     
00133     for (i=0; i<TIMER0_NUM_CALLBACKS; i++)
00134     {
00135         if (CallbackFunc[i] == NULL)
00136         {
00137             CallbackFunc[i] = pFunc;
00138             return TRUE;
00139         }   
00140     }
00141     
00142     return FALSE;
00143 }
00144 
00145 
00146 /*****************************************************************************
00147 *
00148 *   Function name : Timer0_RemoveCallbackFunction
00149 *
00150 *   @param           pFunc pointer to function to remove
00151 *
00152 *   Purpose :       Remove functions from the list which is called int the
00153 *                   TIMER0_COMP_interrupt
00154 *
00155 *****************************************************************************/
00156 BOOL Timer0_RemoveCallbackFunction(TIMER_CALLBACK_FUNC pFunc)
00157 {
00158     // mt char i;
00159         uint8_t i;
00160     
00161     for (i=0; i<TIMER0_NUM_CALLBACKS; i++)
00162     {
00163         if (CallbackFunc[i] == pFunc)
00164         {
00165             CallbackFunc[i] = NULL;
00166             return TRUE;
00167         }
00168     }
00169         
00170     return FALSE;
00171 }
00172 /*
00173 
00174 char Timer0_AllocateCountdownTimer()
00175 {
00176     // mt char i;
00177         uint8_t i;
00178         
00179     for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
00180         if (CountDownTimers[i] == 255)
00181         {
00182             CountDownTimers[i] = 0;
00183             return i+1;
00184         }
00185 
00186     return 0;
00187 }
00188 
00189 void Timer0_SetCountdownTimer(char timer, char value)
00190 {
00191     cli(); // mt __disable_interrupt();
00192     CountDownTimers[timer-1] = value;
00193     sei(); // mt __enable_interrupt();
00194 }
00195 
00196 char Timer0_GetCountdownTimer(char timer)
00197 {
00198     char t;
00199     cli(); // mt __disable_interrupt();
00200     t = CountDownTimers[timer-1];
00201     sei(); // mt __enable_interrupt();
00202     
00203     return t;
00204 }
00205 
00206 void Timer0_ReleaseCountdownTimer(char timer)
00207 {
00208     cli(); // mt __disable_interrupt();
00209     CountDownTimers[timer-1] = 255;
00210     sei(); // mt __enable_interrupt();
00211 }
00212 */
00213 
 All Files Functions Variables Typedefs Enumerations Enumerator Defines