Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "LPC214x.h"
00012 #include "type.h"
00013 #include "irq.h"
00014 #include "target.h"
00015 #include "adc.h"
00016
00017 volatile unsigned int ADC0Value[ADC_NUM], ADC1Value[ADC_NUM];
00018 volatile unsigned int ADC0IntDone = 0, ADC1IntDone = 0;
00019 unsigned int adcChannelValues[8];
00020
00021 #if ADC_INTERRUPT_FLAG
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 void ADC0Handler (void) __irq
00032 {
00033 unsigned int regVal;
00034
00035 IENABLE;
00036
00037 regVal = AD0STAT;
00038 if ( regVal & 0x0000FF00 )
00039 {
00040 regVal = (regVal & 0x0000FF00) >> 0x08;
00041
00042
00043 switch ( regVal )
00044 {
00045 case 0x01:
00046 regVal = AD0DR0;
00047 break;
00048 case 0x02:
00049 regVal = AD0DR1;
00050 break;
00051 case 0x04:
00052 regVal = AD0DR2;
00053 break;
00054 case 0x08:
00055 regVal = AD0DR3;
00056 break;
00057 case 0x10:
00058 regVal = AD0DR4;
00059 break;
00060 case 0x20:
00061 regVal = AD0DR5;
00062 break;
00063 case 0x40:
00064 regVal = AD0DR6;
00065 break;
00066 case 0x80:
00067 regVal = AD0DR7;
00068 break;
00069 default:
00070 break;
00071 }
00072 AD0CR &= 0xF8FFFFFF;
00073 ADC0IntDone = 1;
00074 return;
00075 }
00076
00077 if ( regVal & ADC_ADINT )
00078 {
00079 switch ( regVal & 0xFF )
00080 {
00081 case 0x01:
00082 ADC0Value[0] = ( AD0DR0 >> 6 ) & 0x3FF;
00083 break;
00084 case 0x02:
00085 ADC0Value[1] = ( AD0DR1 >> 6 ) & 0x3FF;
00086 break;
00087 case 0x04:
00088 ADC0Value[2] = ( AD0DR2 >> 6 ) & 0x3FF;
00089 break;
00090 case 0x08:
00091 ADC0Value[3] = ( AD0DR3 >> 6 ) & 0x3FF;
00092 break;
00093 case 0x10:
00094 ADC0Value[4] = ( AD0DR4 >> 6 ) & 0x3FF;
00095 break;
00096 case 0x20:
00097 ADC0Value[5] = ( AD0DR5 >> 6 ) & 0x3FF;
00098 break;
00099 case 0x40:
00100 ADC0Value[6] = ( AD0DR6 >> 6 ) & 0x3FF;
00101 break;
00102 case 0x80:
00103 ADC0Value[7] = ( AD0DR7 >> 6 ) & 0x3FF;
00104 break;
00105 default:
00106 break;
00107 }
00108 AD0CR &= 0xF8FFFFFF;
00109 ADC0IntDone = 1;
00110 }
00111
00112 IDISABLE;
00113 VICVectAddr = 0;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 void ADC1Handler (void) __irq
00126 {
00127 unsigned int regVal;
00128
00129 IENABLE;
00130
00131 regVal = AD1STAT;
00132 if ( regVal & 0x0000FF00 )
00133 {
00134 regVal = (regVal & 0x0000FF00) >> 0x08;
00135
00136
00137 switch ( regVal )
00138 {
00139 case 0x01:
00140 regVal = AD1DR0;
00141 break;
00142 case 0x02:
00143 regVal = AD1DR1;
00144 break;
00145 case 0x04:
00146 regVal = AD1DR2;
00147 break;
00148 case 0x08:
00149 regVal = AD1DR3;
00150 break;
00151 case 0x10:
00152 regVal = AD1DR4;
00153 break;
00154 case 0x20:
00155 regVal = AD1DR5;
00156 break;
00157 case 0x40:
00158 regVal = AD1DR6;
00159 break;
00160 case 0x80:
00161 regVal = AD1DR7;
00162 break;
00163 default:
00164 break;
00165 }
00166 AD1CR &= 0xF8FFFFFF;
00167 ADC1IntDone = 1;
00168 return;
00169 }
00170
00171 if ( regVal & ADC_ADINT )
00172 {
00173 switch ( regVal & 0xFF )
00174 {
00175 case 0x01:
00176 ADC1Value[0] = ( AD1DR0 >> 6 ) & 0x3FF;
00177 break;
00178 case 0x02:
00179 ADC1Value[1] = ( AD1DR1 >> 6 ) & 0x3FF;
00180 break;
00181 case 0x04:
00182 ADC1Value[2] = ( AD1DR2 >> 6 ) & 0x3FF;
00183 break;
00184 case 0x08:
00185 ADC1Value[3] = ( AD1DR3 >> 6 ) & 0x3FF;
00186 break;
00187 case 0x10:
00188 ADC1Value[4] = ( AD1DR4 >> 6 ) & 0x3FF;
00189 break;
00190 case 0x20:
00191 ADC1Value[5] = ( AD1DR5 >> 6 ) & 0x3FF;
00192 break;
00193 case 0x40:
00194 ADC1Value[6] = ( AD1DR6 >> 6 ) & 0x3FF;
00195 break;
00196 case 0x80:
00197 ADC1Value[7] = ( AD1DR7 >> 6 ) & 0x3FF;
00198 break;
00199 default:
00200 break;
00201 }
00202 AD1CR &= 0xF8FFFFFF;
00203 ADC1IntDone = 1;
00204 }
00205
00206 IDISABLE;
00207 VICVectAddr = 0;
00208 }
00209 #endif
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 unsigned int ADCInit( unsigned int ADC_Clk )
00221 {
00222 AD0CR = ( 0x01 ) |
00223 ( ( Fpclk / ADC_Clk - 1 ) << 8 ) |
00224 ( 1 << 16 ) |
00225 ( 0 << 17 ) |
00226 ( 1 << 21 ) |
00227 ( 0 << 22 ) |
00228 ( 0 << 24 ) |
00229 ( 0 << 27 );
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 AD1CR = ( 0x01 << 0 ) |
00245 ( ( Fpclk / ADC_Clk - 1 ) << 8 ) |
00246 ( 0 << 16 ) |
00247 ( 0 << 17 ) |
00248 ( 1 << 21 ) |
00249 ( 0 << 22 ) |
00250 ( 0 << 24 ) |
00251 ( 0 << 27 );
00252
00253
00254
00255 #if ADC_INTERRUPT_FLAG
00256 AD0INTEN = 0x11E;
00257 AD1INTEN = 0x1FF;
00258
00259 if ( install_irq( ADC0_INT, (void *)ADC0Handler ) == FALSE )
00260 {
00261 return (FALSE);
00262 }
00263 if ( install_irq( ADC1_INT, (void *)ADC1Handler ) == FALSE )
00264 {
00265 return (FALSE);
00266 }
00267 #endif
00268
00269 return (TRUE);
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 unsigned int ADC0Read( unsigned char channelNum )
00282 {
00283 #if !ADC_INTERRUPT_FLAG
00284 unsigned int regVal, ADC_Data;
00285 volatile unsigned int timeout=0;
00286 #endif
00287
00288
00289 if ( channelNum >= ADC_NUM )
00290 {
00291 channelNum = 0;
00292 }
00293 AD0CR &= 0xFFFFFF00;
00294 AD0CR |= (1 << 24) | (1 << channelNum);
00295
00296 #if !ADC_INTERRUPT_FLAG
00297 while ( timeout++<5000 )
00298 {
00299 regVal = *(volatile unsigned long *)(AD0_BASE_ADDR
00300 + ADC_OFFSET + ADC_INDEX * channelNum);
00301
00302 if ( regVal & ADC_DONE )
00303 {
00304 break;
00305 }
00306 }
00307
00308 AD0CR &= 0xF8FFFFFF;
00309 if ( regVal & ADC_OVERRUN )
00310
00311 {
00312 return ( 0 );
00313 }
00314 ADC_Data = ( regVal >> 6 ) & 0x3FF;
00315 return ( ADC_Data );
00316 #else
00317 return ( channelNum );
00318
00319
00320 #endif
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 unsigned int ADC1Read( unsigned char channelNum )
00333 {
00334 #if !ADC_INTERRUPT_FLAG
00335 unsigned int regVal;
00336 unsigned int ADC_Data;
00337 #endif
00338
00339
00340 if ( channelNum >= ADC_NUM )
00341 {
00342 channelNum = 0;
00343 }
00344 AD1CR &= 0xFFFFFF00;
00345 AD1CR |= (1 << 24) | (1 << channelNum);
00346
00347 #if !ADC_INTERRUPT_FLAG
00348 while ( 1 )
00349 {
00350 regVal = *(volatile unsigned long *)(AD1_BASE_ADDR
00351 + ADC_OFFSET + ADC_INDEX * channelNum);
00352
00353 if ( regVal & ADC_DONE )
00354 {
00355 break;
00356 }
00357 }
00358
00359 AD1CR &= 0xF8FFFFFF;
00360 if ( regVal & ADC_OVERRUN )
00361
00362 {
00363 return ( 0 );
00364 }
00365
00366 ADC_Data = ( regVal >> 6 ) & 0x3FF;
00367 return ( ADC_Data );
00368 #else
00369 return ( channelNum );
00370 #endif
00371 }
00372
00373 void ADC0triggerSampling(unsigned char selectChannels)
00374 {
00375 AD0CR |= (selectChannels);
00376
00377 }
00378
00379 void ADC0getSamplingResults(unsigned char selectChannels, unsigned int * channelValues)
00380 {
00381 int i;
00382
00383 for (i=0;i<8;i++)
00384 if (selectChannels&(1<<i))
00385 {
00386 unsigned int regVal;
00387
00388 regVal=*(volatile unsigned long *)(AD0_BASE_ADDR
00389 + ADC_OFFSET + ADC_INDEX * i);
00390
00391 if ((regVal&(ADC_OVERRUN|ADC_DONE))==0)
00392 channelValues[i]=0;
00393 else
00394 channelValues[i]=( regVal >> 6 ) & 0x3FF;
00395 }
00396 }
00397
00398
00399
00400