00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef WIN32
00019 #include <io.h>
00020 #include <sig-avr.h>
00021 #include <interrupt.h>
00022 #include <progmem.h>
00023 #endif
00024
00025 #include "global.h"
00026 #include "timer.h"
00027 #include "pulse.h"
00028
00029
00030 extern short __attribute__ ((progmem)) TimerPrescaleFactor[];
00031
00032 volatile static unsigned char PulseT1AMode;
00033 volatile static unsigned short PulseT1ACount;
00034 volatile static unsigned short PulseT1APeriodTics;
00035 volatile static unsigned char PulseT1BMode;
00036 volatile static unsigned short PulseT1BCount;
00037 volatile static unsigned short PulseT1BPeriodTics;
00038
00039
00040
00041
00042
00043 #define PULSE_MODE_COUNTED 0x01
00044
00045
00046
00047 void pulseInit(void)
00048 {
00049
00050 pulseT1Init();
00051 }
00052
00053 void pulseT1Init(void)
00054 {
00055
00056
00057 timer1PWMOff();
00058
00059
00060
00061 PulseT1AMode = 0;
00062 PulseT1BMode = 0;
00063 PulseT1ACount = 0;
00064 PulseT1BCount = 0;
00065 PulseT1APeriodTics = 0x8000;
00066 PulseT1BPeriodTics = 0x8000;
00067
00068
00069
00070 timerAttach(TIMER1OUTCOMPAREA_INT,pulseT1AService);
00071 timerAttach(TIMER1OUTCOMPAREB_INT,pulseT1BService);
00072 }
00073
00074 void pulseT1Off(void)
00075 {
00076
00077
00078
00079 PulseT1ACount = 0;
00080 PulseT1BCount = 0;
00081
00082 cbi(TCCR1A,COM1A1);
00083 cbi(TCCR1A,COM1A0);
00084
00085 cbi(TCCR1A,COM1B1);
00086 cbi(TCCR1A,COM1B0);
00087
00088 timerDetach(TIMER1OUTCOMPAREA_INT);
00089 timerDetach(TIMER1OUTCOMPAREB_INT);
00090 }
00091
00092 void pulseT1ASetFreq(u16 freqHz)
00093 {
00094
00095
00096
00097
00098
00099 u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+(inp(TCCR1B) & TIMER_PRESCALE_MASK)));
00100
00101
00102 PulseT1APeriodTics = ((u32)F_CPU/((u32)prescaleDiv*2*freqHz));
00103 }
00104
00105 void pulseT1BSetFreq(u16 freqHz)
00106 {
00107
00108
00109
00110
00111
00112 u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+(inp(TCCR1B) & TIMER_PRESCALE_MASK)));
00113
00114
00115 PulseT1BPeriodTics = ((u32)F_CPU/((u32)prescaleDiv*2*freqHz));
00116 }
00117
00118 void pulseT1ARun(u16 nPulses)
00119 {
00120
00121
00122
00123 u16 OCValue;
00124
00125 OCValue = inp(OCR1AL);
00126 OCValue += inp(OCR1AH)<<8;
00127
00128 OCValue += PulseT1APeriodTics;
00129
00130 outp((OCValue>>8), OCR1AH);
00131 outp((OCValue & 0x00FF),OCR1AL);
00132
00133
00134
00135 if(nPulses)
00136 {
00137
00138 PulseT1AMode |= PULSE_MODE_COUNTED;
00139 PulseT1ACount = nPulses<<1;
00140 }
00141 else
00142 {
00143
00144 PulseT1AMode &= ~PULSE_MODE_COUNTED;
00145 PulseT1ACount = 1<<1;
00146 }
00147
00148 cbi(TCCR1A,COM1A1);
00149 sbi(TCCR1A,COM1A0);
00150
00151 sbi(TIMSK, OCIE1A);
00152 }
00153
00154 void pulseT1BRun(u16 nPulses)
00155 {
00156
00157
00158
00159 u16 OCValue;
00160
00161 OCValue = inp(OCR1BL);
00162 OCValue += inp(OCR1BH)<<8;
00163
00164 OCValue += PulseT1BPeriodTics;
00165
00166 outp((OCValue>>8), OCR1BH);
00167 outp((OCValue & 0x00FF),OCR1BL);
00168
00169
00170
00171 if(nPulses)
00172 {
00173
00174 PulseT1BMode |= PULSE_MODE_COUNTED;
00175 PulseT1BCount = nPulses<<1;
00176 }
00177 else
00178 {
00179
00180 PulseT1BMode &= ~PULSE_MODE_COUNTED;
00181 PulseT1BCount = 1<<1;
00182 }
00183
00184
00185 cbi(TCCR1A,COM1B1);
00186 sbi(TCCR1A,COM1B0);
00187
00188
00189 sbi(TIMSK, OCIE1B);
00190 }
00191
00192 u16 pulseT1ARemaining(void)
00193 {
00194
00195
00196 return (PulseT1ACount+1)>>1;
00197 }
00198
00199 u16 pulseT1BRemaining(void)
00200 {
00201
00202
00203 return (PulseT1BCount+1)>>1;
00204 }
00205
00206 void pulseT1AService(void)
00207 {
00208 u16 OCValue;
00209
00210
00211
00212 if(PulseT1ACount)
00213 {
00214
00215 OCValue = inp(OCR1AL);
00216 OCValue += inp(OCR1AH)<<8;
00217
00218 OCValue += PulseT1APeriodTics;
00219
00220 outp((OCValue>>8), OCR1AH);
00221 outp((OCValue & 0x00FF),OCR1AL);
00222
00223 if(PulseT1AMode & PULSE_MODE_COUNTED)
00224 PulseT1ACount--;
00225 }
00226 else
00227 {
00228
00229
00230 cbi(TCCR1A,COM1A1);
00231 cbi(TCCR1A,COM1A0);
00232
00233 cbi(TIMSK, OCIE1A);
00234 }
00235 }
00236
00237 void pulseT1BService(void)
00238 {
00239 u16 OCValue;
00240
00241
00242
00243 if(PulseT1BCount)
00244 {
00245
00246 OCValue = inp(OCR1BL);
00247 OCValue += inp(OCR1BH)<<8;
00248
00249 OCValue += PulseT1BPeriodTics;
00250
00251 outp((OCValue>>8), OCR1BH);
00252 outp((OCValue & 0x00FF),OCR1BL);
00253
00254 if(PulseT1BMode & PULSE_MODE_COUNTED)
00255 PulseT1BCount--;
00256 }
00257 else
00258 {
00259
00260
00261 cbi(TCCR1A,COM1B1);
00262 cbi(TCCR1A,COM1B0);
00263
00264 cbi(TIMSK, OCIE1B);
00265 }
00266 }