00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef __TINYX61_MACROS_H__
00042 #define __TINYX61_MACROS_H__
00043
00044
00045
00048 #include "compiler.h"
00049 #ifdef __GNUC__
00050 #include <avr/io.h>
00051 #include <avr/power.h>
00052 #elif __ICCAVR__
00053 #include <ioavr.h>
00054 #else
00055 #error Current COMPILER not supported
00056 #endif
00058
00059
00060
00061 #if defined(__ICCAVR__)
00063 #define SAVE_INTERRUPT() __save_interrupt();
00064
00066 #define RESTORE_INTERRUPT(state) __restore_interrupt(state);
00067
00069 #define DISABLE_INTERRUPT() __disable_interrupt();
00070
00071 #elif defined(__GNUC__)
00073 #define SAVE_INTERRUPT() SREG
00074
00076 #define RESTORE_INTERRUPT(state) (SREG = (state))
00077
00079 #define DISABLE_INTERRUPT() cli()
00080
00081 #else
00083 #define SAVE_INTERRUPT() SREG
00084
00086 #define RESTORE_INTERRUPT(state) (SREG = (state))
00087
00089 #define DISABLE_INTERRUPT() (SREG &= (~GLOBAL_INTERRUPT_BIT_MASK))
00090 #endif
00091
00092
00099 #define TC0_WRITE_TCNT0(value) \
00100 { \
00101 TCNT0H = (U8)((value) >> 8); \
00102 TCNT0L = (U8)(value); \
00103 }
00104
00105
00112 #define TC0_READ_TCNT0(destinationVariable) \
00113 { \
00114 U8 tempL; \
00115 tempL = TCNT0L; \
00116 (destinationVariable) = ((TCNT0H << 8) | tempL); \
00117 }
00118
00119
00126 #define TC0_WRITE_TCNT0_INT_SAFE(value) \
00127 { \
00128 U8 iFlagTemp; \
00129 iFlagTemp = SAVE_INTERRUPT(); \
00130 DISABLE_INTERRUPT(); \
00131 TCNT0H = (U8)((value) >> 8); \
00132 TCNT0L = (U8)(value); \
00133 RESTORE_INTERRUPT(iFlagTemp); \
00134 }
00135
00136
00143 #define TC0_READ_TCNT0_INT_SAFE(destinationVariable) \
00144 { \
00145 U8 iFlagTemp; \
00146 U8 tempL; \
00147 iFlagTemp = SAVE_INTERRUPT(); \
00148 DISABLE_INTERRUPT(); \
00149 tempL = TCNT0L; \
00150 (destinationVariable) = ((TCNT0H << 8) | tempL); \
00151 RESTORE_INTERRUPT(iFlagTemp); \
00152 }
00153
00154
00161 #define TC0_WRITE_16_BIT_OCR0AB(value) \
00162 { \
00163 OCR0B = (U8)((value) >> 8); \
00164 OCR0A = (U8)(value);\
00165 }
00166
00167
00174 #define TC0_READ_16_BIT_OCR0AB(destinationVariable) \
00175 { \
00176 U8 tempL = OCR0A; \
00177 (destinationVariable) = ((U16)OCR0B << 8) | tempL; \
00178 }
00179
00180
00187 #define TC0_READ_16_BIT_OCR0AB_INT_SAFE(destinationVariable) \
00188 { \
00189 U8 iFlagTemp; \
00190 iFlagTemp = SAVE_INTERRUPT(); \
00191 DISABLE_INTERRUPT(); \
00192 U8 tempL = OCR0A; \
00193 (destinationVariable) = ((U16)OCR0B << 8) | tempL; \
00194 RESTORE_INTERRUPT(iFlagTemp); \
00195 }
00196
00197
00204 #define TC0_WRITE_16_BIT_OCR0AB_INT_SAFE(value) \
00205 { \
00206 U8 iFlagTemp; \
00207 iFlagTemp = SAVE_INTERRUPT(); \
00208 DISABLE_INTERRUPT(); \
00209 OCR0B = (U8)((value) >> 8); \
00210 OCR0A = (U8)(value);\
00211 RESTORE_INTERRUPT(iFlagTemp); \
00212 }
00213
00214
00222 #define TC1_WRITE_10_BIT_REGISTER(destinationRegister, value) \
00223 { \
00224 TC1H = ((value) >> 8); \
00225 (destinationRegister) = (U8)(value); \
00226 }
00227
00228
00236 #define TC1_READ_10_BIT_REGISTER(sourceRegister, destinationVariable) \
00237 { \
00238 U8 tempL; \
00239 tempL = (sourceRegister); \
00240 (destinationVariable) = ( ((U16)TC1H << 8) | tempL); \
00241 }
00242
00243
00252 #define TC1_WRITE_10_BIT_REGISTER_INT_SAFE(destinationRegister, value) \
00253 { \
00254 U8 iFlagTemp; \
00255 iFlagTemp = SAVE_INTERRUPT(); \
00256 DISABLE_INTERRUPT(); \
00257 TC1H = ((value) >> 8); \
00258 (destinationRegister) = (U8)(value); \
00259 RESTORE_INTERRUPT(iFlagTemp); \
00260 }
00261
00262
00271 #define TC1_READ_10_BIT_REGISTER_INT_SAFE(sourceRegister, destinationVariable) \
00272 { \
00273 U8 iFlagTemp; \
00274 U8 tempL; \
00275 iFlagTemp = SAVE_INTERRUPT(); \
00276 tempL = (sourceRegister); \
00277 (destinationVariable) = ( ((U16)TC1H << 8) | tempL); \
00278 RESTORE_INTERRUPT(iFlagTemp); \
00279 }
00280
00281
00290 #define TC1_SET_ALL_COMPARE_VALUES(compareValue) \
00291 { \
00292 U16 tempValue = compareValue; \
00293 TC1H = ((U8)((tempValue) >> 8)); \
00294 OCR1A = ((U8)tempValue); \
00295 OCR1B = ((U8)tempValue); \
00296 OCR1D = ((U8)tempValue); \
00297 }
00298
00305 #ifdef __GNUC__
00306 #define Clear_prescaler() (clock_prescale_set(0))
00307 #else
00308 #define Clear_prescaler() (Set_cpu_prescaler(0))
00309 #endif
00310
00317 #ifdef __GNUC__
00318 #define Set_cpu_prescaler(x) (clock_prescale_set(x))
00319 #else
00320 extern void Set_cpu_prescaler(U8 x);
00321 #endif
00322
00323
00324
00326 #define GLOBAL_INTERRUPT_BIT_MASK 0x80
00327
00329 #define CLEAR_ALL_TIMER1_INT_FLAGS (TIFR = TIFR | 0xE4)
00330
00332 #define DISABLE_ALL_TIMER1_INTS (TIMSK &= 0x1B)
00333
00335 #define SET_TIMER1_INT_ZC_DETECTION (TIMSK = (1 << TOIE1))
00336
00338 #define CLEAR_ALL_TIMER0_INT_FLAGS (TIFR = TIFR | 0x1B)
00339
00341 #define DISABLE_ALL_TIMER0_INTS (TIMSK &= 0xE4)
00342
00344 #define SET_TIMER0_COMPA_INT (TIMSK = (1 << OCIE0A))
00345
00347 #define ADC_RESOLUTION 1024
00348
00350 #define ADC_PRESCALER_8 ((0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
00351
00353 #define ADC_PRESCALER_16 ((1 << ADPS2) | (0 << ADPS1) | (0 << ADPS0))
00354
00356 #define ADC_TS_FREERUNNING ((0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
00357
00358 #endif