timer8_drv.h

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *
00019  * 1. Redistributions of source code must retain the above copyright notice,
00020  * this list of conditions and the following disclaimer.
00021  *
00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
00023  * this list of conditions and the following disclaimer in the documentation
00024  * and/or other materials provided with the distribution.
00025  *
00026  * 3. The name of ATMEL may not be used to endorse or promote products derived
00027  * from this software without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 #ifndef _TIMER8_DRV_H_
00042 #define _TIMER8_DRV_H_
00043 
00044 //_____ I N C L U D E S ________________________________________________________
00045 
00046 #include "config.h"
00047 
00048 //_____ G E N E R A L    D E F I N I T I O N S _________________________________
00049 
00050 #ifndef FOSC
00051 #  error  You must define FOSC in "config.h" file
00052 #endif
00053     // ----------
00054 #ifndef TIMER8_0             // 8-bit TIMER 0 Defintion
00055 #define TIMER8_0        0x00
00056 #endif
00057 #ifndef TIMER8_2             // 8-bit TIMER 2 Defintion
00058 #define TIMER8_2        0x02
00059 #endif
00060 #ifndef BOTH_TIMER8          // Both the 8-bit TIMERs Defintion
00061 #define BOTH_TIMER8     0xFF
00062 #endif
00063 
00064 //_____ M A C R O S ____________________________________________________________
00065 
00066     // ---------- Two ways to have a look on the things
00067 #define Timer8_set_pwm_a(value)    ( Timer8_set_compare_a(value) )
00068 #define Timer8_set_pwm(value)      ( Timer8_set_pwm_a(value) )
00069 #define Timer8_get_pwm_a()         ( Timer8_get_compare_a() )
00070 #define Timer8_get_pwm()           ( Timer8_get_pwm_a() )
00071     // ---------- If no clock, the timer is off !
00072 #define Timer8_off()                 Timer8_set_clock(TIMER8_NO_CLOCK)
00073 
00074     
00075 #ifndef USE_TIMER8
00076 #       error You must define USE_TIMER8 to TIMER8_0 or TIMER8_2 or BOTH_TIMER8 in "config.h" file
00077 
00078 #   elif (USE_TIMER8 == TIMER8_0)        
00079 
00080 
00081 
00082 #       define Timer8_select(timer8_num)        // Empty !
00083     // ---------- Macros
00084 #       define Timer8_clear()                  { TCCR0A=0; TCNT0=0; OCR0A=0; }
00085         // ----------
00086 #       define Timer8_set_counter(value)       ( TCNT0 = value )
00087 #       define Timer8_get_counter()            ( TCNT0 )
00088         // ----------
00089 #       define Timer8_set_compare_a(value)     ( OCR0A = value )
00090 #       define Timer8_set_compare(value)       ( Timer8_set_compare_a(value) )          // No index specified
00091 #       define Timer8_get_compare_a()          ( OCR0A )
00092 #       define Timer8_get_compare()            ( Timer8_get_compare_a() )               // No index specified
00093         // ----------
00094 #       define Timer8_set_mode_output_a(conf)  ( TCCR0A = (TCCR0A & (~TIMER8_COMP_MODE_MASK_A)) | (conf << COM0A0) )
00095 #       define Timer8_set_mode_output(conf)    ( Timer8_set_mode_output_a(conf) )       // No index specified
00096 #       define Timer8_get_mode_output_a()      ((TCCR0A & TIMER8_COMP_MODE_MASK_A) >> COM0A0 )
00097 #       define Timer8_get_mode_output()        ( Timer8_get_mode_output_a() )           // No index specified
00098         // ----------
00099 #       define Timer8_set_waveform_mode(conf)  ( TCCR0A = (TCCR0A & (~TIMER8_WGM_RA_MASK)) |  \
00100                                                ( (((conf & 0x02) >> 1) << WGM01) | ((conf & 0x1) << WGM00) ) )
00101 #       define Timer8_get_waveform_mode()      ( ((TCCR0A & (1<<WGM00)) >> WGM00) |  \
00102                                                ( ((TCCR0A & (1<<WGM01)) >> WGM01) << 0x1) )
00103         // ----------
00104 #       define Timer8_set_clock(value)         ( TCCR0B = (TCCR0B & (~TIMER8_CLK_MASK)) | (value << CS00) )
00105 #       define Timer8_get_clock()              ((TCCR0B & TIMER8_CLK_MASK) >> CS00 )
00106         // ----------
00107 #       define Timer8_overflow_it_enable()    ( TIMSK0 |=  (1<<TOIE0)  )
00108 #       define Timer8_overflow_it_disable()   ( TIMSK0 &= ~(1<<TOIE0)  )
00109 #       define Timer8_compare_a_it_enable()   ( TIMSK0 |=  (1<<OCIE0A) )
00110 #       define Timer8_compare_a_it_disable()  ( TIMSK0 &= ~(1<<OCIE0A) )
00111 #       define Timer8_compare_it_enable()     ( Timer8_compare_a_it_enable()  )
00112 #       define Timer8_compare_it_disable()    ( Timer8_compare_a_it_disable() )
00113 
00114 #       define Timer8_get_overflow_it_mask()  ((TIMSK0  &  (1<<TOIE0) ) >> TOIE0  )
00115 #       define Timer8_get_compare_a_it_mask() ((TIMSK0  &  (1<<OCIE0A)) >> OCIE0A )
00116 #       define Timer8_get_compare_it_mask()   ( Timer8_get_compare_a_it_mask() )
00117         // ----------
00118 #       define Timer8_clear_overflow_it()     ( TIFR0 |=  (1<<TOV0)  )
00119 #       define Timer8_clear_compare_a_it()    ( TIFR0 |=  (1<<OCF0A) )
00120 #       define Timer8_clear_compare_it()      ( Timer8_clear_compare_a_it() )
00121 #       define Timer8_get_overflow_it()       ((TIFR0  &  (1<<TOV0) ) >> TOV0  )
00122 #       define Timer8_get_compare_a_it()      ((TIFR0  &  (1<<OCF0A)) >> OCF0A )
00123 #       define Timer8_get_compare_it()        ( Timer8_get_compare_a_it() )
00124 
00125 #   elif USE_TIMER8 == TIMER8_2          
00126 
00127 
00128 
00129 #       define Timer8_select(timer8_num)        // Empty !
00130     // ---------- Macros
00131 #       define Timer8_clear()                  { TCCR2A=0; TCNT2=0; OCR2A=0; }
00132         // ----------
00133 #       define Timer8_set_counter(value)       ( TCNT2 = value )
00134 #       define Timer8_get_counter()            ( TCNT2 )
00135         // ----------
00136 #       define Timer8_set_compare_a(value)     ( OCR2A = value )
00137 #       define Timer8_set_compare(value)       ( Timer8_set_compare_a(value) )          // No index specified
00138 #       define Timer8_get_compare_a()          ( OCR2A )
00139 #       define Timer8_get_compare()            ( Timer8_get_compare_a() )               // No index specified
00140         // ----------
00141 #       define Timer8_set_mode_output_a(conf)  ( TCCR2A = (TCCR2A & (~TIMER8_COMP_MODE_MASK_A)) | (conf << COM2A0) )
00142 #       define Timer8_set_mode_output(conf)    ( Timer8_set_mode_output_a(conf) )       // No index specified
00143 #       define Timer8_get_mode_output_a()      ((TCCR2A & TIMER8_COMP_MODE_MASK_A) >> COM2A0 )
00144 #       define Timer8_get_mode_output()        ( Timer8_get_mode_output_a() )           // No index specified
00145         // ----------
00146 #       define Timer8_set_waveform_mode(conf)  ( TCCR2A = (TCCR2A & (~TIMER8_WGM_RA_MASK)) |  \
00147                                                ( (((conf & 0x02) >> 1) << WGM21) | ((conf & 0x1) << WGM20) ) )
00148 #       define Timer8_get_waveform_mode()      ( ((TCCR2A & (1<<WGM20)) >> WGM20)          |  \
00149                                                ( ((TCCR2A & (1<<WGM21)) >> WGM21) << 0x1) )
00150         // ----------
00151 #       define Timer8_set_clock(value)         ( TCCR2B = (TCCR2B & (~TIMER8_CLK_MASK)) | (value << CS20) )
00152 #       define Timer8_get_clock()              ((TCCR2B & TIMER8_CLK_MASK) >> CS20 )
00153         // ----------
00154 #       define Timer8_overflow_it_enable()    ( TIMSK2 |=  (1<<TOIE2)  )
00155 #       define Timer8_overflow_it_disable()   ( TIMSK2 &= ~(1<<TOIE2)  )
00156 #       define Timer8_compare_a_it_enable()   ( TIMSK2 |=  (1<<OCIE2A) )
00157 #       define Timer8_compare_a_it_disable()  ( TIMSK2 &= ~(1<<OCIE2A) )
00158 #       define Timer8_compare_it_enable()     ( Timer8_compare_a_it_enable()  )
00159 #       define Timer8_compare_it_disable()    ( Timer8_compare_a_it_disable() )
00160 
00161 #       define Timer8_get_overflow_it_mask()  ((TIMSK2  &  (1<<TOIE2) ) >> TOIE2  )
00162 #       define Timer8_get_compare_a_it_mask() ((TIMSK2  &  (1<<OCIE2A)) >> OCIE2A )
00163 #       define Timer8_get_compare_it_mask()   ( Timer8_get_compare_a_it_mask() )
00164         // ----------
00165 #       define Timer8_clear_overflow_it()     ( TIFR2 |=  (1<<TOV2)  )
00166 #       define Timer8_clear_compare_a_it()    ( TIFR2 |=  (1<<OCF2A) )
00167 #       define Timer8_clear_compare_it()      ( Timer8_clear_compare_a_it() )
00168 #       define Timer8_get_overflow_it()       ((TIFR2  &  (1<<TOV2) ) >> TOV2  )
00169 #       define Timer8_get_compare_a_it()      ((TIFR2  &  (1<<OCF2A)) >> OCF2A )
00170 #       define Timer8_get_compare_it()        ( Timer8_get_compare_a_it() )
00171 
00172 #   elif USE_TIMER8 == BOTH_TIMER8   
00173 
00174 
00175 
00176         extern U8 timer8_selected;         // $$$-- EXTERNAL DECLARATION --$$$
00177 #       define Timer8_select(timer8_num)      (timer8_selected=timer8_num)
00178     // ---------- Macros
00179 #       define Timer8_clear()                 ((timer8_selected==TIMER8_0)? \
00180                                                   (TCCR0A=0, TCNT0=0, OCR0A=0 ) \
00181                                                   : \
00182                                                   (TCCR2A=0, TCNT2=0, OCR2A=0 ) )
00183         // ----------
00184 #       define Timer8_set_counter(value)      ((timer8_selected==TIMER8_0)? (TCNT0 = value) : (TCNT2 = value) )
00185 #       define Timer8_get_counter()           ((timer8_selected==TIMER8_0)? (TCNT0) : (TCNT2) )
00186         // ----------
00187 #       define Timer8_set_compare_a(value)    ((timer8_selected==TIMER8_0)? (OCR0A = value) : (OCR2A = value) )
00188 #       define Timer8_set_compare(value)      ( Timer8_set_compare_a(value) )
00189 #       define Timer8_get_compare_a()         ((timer8_selected==TIMER8_0)? (OCR0A) : (OCR2A) )
00190 #       define Timer8_get_compare()           ( Timer8_get_compare_a() )
00191         // ----------
00192 #       define Timer8_set_mode_output_a(conf) ((timer8_selected==TIMER8_0)? \
00193                                                   (TCCR0A = (TCCR0A & (~TIMER8_COMP_MODE_MASK_A)) | (conf << COM0A0)) \
00194                                                   : \
00195                                                   (TCCR2A = (TCCR2A & (~TIMER8_COMP_MODE_MASK_A)) | (conf << COM2A0)) )
00196 #       define Timer8_set_mode_output(conf)   ( Timer8_set_mode_output_a(conf) )
00197 #       define Timer8_get_mode_output_a()     ((timer8_selected==TIMER8_0)? \
00198                                                   ((TCCR0A & TIMER8_COMP_MODE_MASK_A) >> COM0A0) \
00199                                                   : \
00200                                                   ((TCCR2A & TIMER8_COMP_MODE_MASK_A) >> COM2A0) )
00201 #       define Timer8_get_mode_output()       ( Timer8_get_mode_output_a() )
00202         // ----------
00203 #       define Timer8_set_waveform_mode(conf) ((timer8_selected==TIMER8_0)? \
00204                                                   (TCCR0A = (TCCR0A & (~WGM8_RA_MASK)) |  \
00205                                                   ((((conf & 0x02) >> 1) << WGM01) | ((conf & 0x1) << WGM00) ) )  \
00206                                                   : \
00207                                                   (TCCR2A = (TCCR2A & (~WGM8_RA_MASK)) |  \
00208                                                   ((((conf & 0x02) >> 1) << WGM21) | ((conf & 0x1) << WGM20) ) )  )
00209 #       define Timer8_get_waveform_mode()     ((timer8_selected==TIMER8_0)? \
00210                                                   (((TCCR0A & (1<<WGM00)) >> WGM00) | (((TCCR0A & (1<<WGM01)) >> WGM01) << 0x1)) \
00211                                                   : \
00212                                                   (((TCCR2A & (1<<WGM20)) >> WGM20) | (((TCCR2A & (1<<WGM21)) >> WGM21) << 0x1)) )
00213         // ----------
00214 #       define Timer8_set_clock(value)        ((timer8_selected==TIMER8_0)? \
00215                                                   (TCCR0B = (TCCR0B & (~TIMER8_CLK_MASK)) | (value << CS00)) \
00216                                                   : \
00217                                                   (TCCR2B = (TCCR2B & (~TIMER8_CLK_MASK)) | (value << CS20)) )
00218 
00219 #       define Timer8_get_clock()             ((timer8_selected==TIMER8_0)? \
00220                                                   (((TCCR0B & TIMER8_CLK_MASK) >> CS00)) : (((TCCR2B & TIMER8_CLK_MASK) >> CS20)) )
00221         // ----------
00222 #       define Timer8_overflow_it_enable()    ((timer8_selected==TIMER8_0)? \
00223                                                   (TIMSK0 |=  (1<<TOIE0)) : (TIMSK2 |=  (1<<TOIE2)) )
00224 #       define Timer8_overflow_it_disable()   ((timer8_selected==TIMER8_0)? \
00225                                                   (TIMSK0 &= ~(1<<TOIE0)) : (TIMSK2 &= ~(1<<TOIE2)) )
00226 #       define Timer8_compare_a_it_enable()   ((timer8_selected==TIMER8_0)? \
00227                                                   (TIMSK0 |= (1<<OCIE0A)) : (TIMSK2 |= (1<<OCIE2A)) )
00228 #       define Timer8_compare_a_it_disable()  ((timer8_selected==TIMER8_0)? \
00229                                                   (TIMSK0 &= ~(1<<OCIE0A)) : (TIMSK2 &= ~(1<<OCIE2A)) )
00230 #       define Timer8_compare_it_enable()     ( Timer8_compare_a_it_enable()  )
00231 #       define Timer8_compare_it_disable()    ( Timer8_compare_a_it_disable() )
00232 
00233 #       define Timer8_get_overflow_it_mask()  ((timer8_selected==TIMER8_0)? \
00234                                                   ((TIMSK0 & (1<<TOIE0)) >> TOIE0) : ((TIMSK2 & (1<<TOIE2)) >> TOIE2) )
00235 #       define Timer8_get_compare_a_it_mask() ((timer8_selected==TIMER8_0)? \
00236                                                   ((TIMSK0 & (1<<OCIE0A)) >> OCIE0A) : ((TIMSK2 & (1<<OCIE2A)) >> OCIE2A) )
00237 #       define Timer8_get_compare_it_mask()   ( Timer8_get_compare_a_it_mask() )
00238         // ----------
00239 #       define Timer8_clear_overflow_it()     ((timer8_selected==TIMER8_0)? \
00240                                                   (TIFR0 |= (1<<TOV0)) : (TIFR2 |= (1<<TOV2)) )
00241 #       define Timer8_clear_compare_a_it()    ((timer8_selected==TIMER8_0)? \
00242                                                   (TIFR0 |= (1<<OCF0A)) : (TIFR2 |= (1<<OCF2A)) )
00243 #       define Timer8_clear_compare_it()      ( Timer8_clear_compare_a_it() )
00244 #       define Timer8_get_overflow_it()       ((timer8_selected==TIMER8_0)? \
00245                                                   ((TIFR0 & (1<<TOV0)) >> TOV0) : ((TIFR2 & (1<<TOV2)) >> TOV2) )
00246 #       define Timer8_get_compare_a_it()      ((timer8_selected==TIMER8_0)? \
00247                                                   ((TIFR0 & (1<<OCF0A)) >> OCF0A) : ((TIFR2 & (1<<OCF2A)) >> OCF2A) )
00248 #       define Timer8_get_compare_it()        ( Timer8_get_compare_a_it() )
00249 
00250     #else
00251         #error USE_TIMER8 definition is not referenced in "timer8_drv.h" file
00252 #endif
00253 
00254 
00258         // ---------- CLK SOURCE for TIMER 2 
00259 #       define Timer8_2_system_clk()          ( ASSR &= ~((1<<EXCLK)|(1<<AS2)) )
00260 #       define Timer8_2_external_osc()        ( ASSR  =  (ASSR & ~(1<<EXCLK)) | (1<<AS2) )
00261 #       define Timer8_2_external_clk()        ( ASSR |=  ((1<<EXCLK)|(1<<AS2)) )
00262         // ---------- UPDATE in ASYNCHRONOUS OPERATION 
00263 #       define Timer8_2_update_busy()         ( ASSR & ( (1<<TCN2UB) | (1<<OCR2UB) | (1<<TCR2UB) ) )
00264 
00265 
00266 //_____ T I M E R   D E F I N I T I O N S ______________________________________
00267 
00268     // ---------- Pre-definitions for "conf" field for Timer8_set(get)_mode_output_x(conf) macros
00269 #define TIMER8_COMP_MODE_NORMAL    (0)
00270 #define TIMER8_COMP_MODE_TOGGLE    (1)
00271 #define TIMER8_COMP_MODE_CLEAR_OC  (2)
00272 #define TIMER8_COMP_MODE_SET_OC    (3)
00273 #define TIMER8_COMP_MODE_MASK_A    (3<<COM0A0)
00274     // ---------- Pre-definitions for "conf" field for Timer8_set_waveform_mode(conf) macro
00275 #define TIMER8_WGM_NORMAL          (0)
00276 #define TIMER8_WGM_PWM_PC8         (1)
00277 #define TIMER8_WGM_CTC_OCR         (2)
00278 #define TIMER8_WGM_FAST_PWM8       (3)
00279 #define TIMER8_WGM_RA_MASK         ( (1<<WGM00) | (1<<WGM01) )
00280     // ---------- Pre-definitions for "value" field for Timer8_set_clock(value) macro
00281 #define TIMER8_NO_CLOCK                   (0)
00282 #define TIMER8_CLKIO_BY_1                 (1)
00283 #define TIMER8_CLK_MASK                   (7<<CS00)
00284         // ---------- and especally for TIMER 0
00285 #define TIMER8_0_NO_CLOCK                 (0)
00286 #define TIMER8_0_CLKIO_BY_1               (1)
00287 #define TIMER8_0_CLKIO_BY_8               (2)
00288 #define TIMER8_0_CLKIO_BY_64              (3)
00289 #define TIMER8_0_CLKIO_BY_256             (4)
00290 #define TIMER8_0_CLKIO_BY_1024            (5)
00291 #define TIMER8_0_EXT_CLOCK_FALLING_EDGE   (6)
00292 #define TIMER8_0_EXT_CLOCK_RISING_EDGE    (7)
00293         // ---------- and especally for TIMER 2
00294 #define TIMER8_2_NO_CLOCK                 (0)
00295 #define TIMER8_2_CLKIO_BY_1               (1)
00296 #define TIMER8_2_CLKIO_BY_8               (2)
00297 #define TIMER8_2_CLKIO_BY_32              (3)
00298 #define TIMER8_2_CLKIO_BY_64              (4)
00299 #define TIMER8_2_CLKIO_BY_128             (5)
00300 #define TIMER8_2_CLKIO_BY_256             (6)
00301 #define TIMER8_2_CLKIO_BY_1024            (7)
00302 
00303 
00304 //_____ D E C L A R A T I O N S ________________________________________________
00305 
00306 //------------------------------------------------------------------------------
00307 //  @fn timer8_get_counter
00317 extern  U8 timer8_get_counter(void);
00318 
00319 //______________________________________________________________________________
00320 
00321 #endif  // _TIMER8_DRV_H_
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 
00345 
00346 
00347 
00348 
00349 
00350 
00351 

Generated on Fri Oct 31 15:31:41 2008 for ATMEL by  doxygen 1.5.3