Revision 2:e1781d02ec0d, committed 2011-02-18
- Comitter:
- bobanderson
- Date:
- Fri Feb 18 02:36:18 2011 +0000
- Parent:
- 1:ecca38babc13
- Commit message:
- Rev to allow either MPC3304 or MPC3208 A/D converters to be used.
Changed in this revision
--- a/main.cpp Fri Feb 18 00:43:05 2011 +0000
+++ b/main.cpp Fri Feb 18 02:36:18 2011 +0000
@@ -40,26 +40,30 @@
AnalogIn ain( p20 );
-DigitalOut mpc3304Select(p8);
+DigitalOut extAdcSelect(p8);
DigitalOut led4(LED4);
SPI spi(p5,p6,p7); // spi(mosi,miso,sck)
+#define MCP3208 // or MCP3304
+
+#define EXT_ADC_CHAN 5
+
#define NUM_SAMPLES 1000000
#define HGRAM_MAX_INDEX 60
int histoGramA[ HGRAM_MAX_INDEX + 1 ]; // histogram array for internal ADC
int histoGramB[ HGRAM_MAX_INDEX + 1 ]; // histogram array for (possible) external ADC
-int glitchCount = 0;
+int glitchCount = 0;
bool glitchSuppressionWanted = false;
bool useExternalADC = false;
void setADCclockToMaxConversionRate( void );
int glitchSuppressedAnalogIn( bool );
-int readExternalADC( void );
+int readExternalADC( int );
void displayMenu( void );
-int getCenterValue( bool externalADC );
+int getCenterValue( bool externalADC );
int max( int a, int b ) {
if ( a > b ) return a;
@@ -90,7 +94,7 @@
led4 = 0; // This led shows progress during data acquisition.
- mpc3304Select = EXT_ADC_OFF; // deselect
+ extAdcSelect = EXT_ADC_OFF; // deselect
spi.format( 8, 0 );
spi.frequency( 500000 );
@@ -127,7 +131,7 @@
case 't':
for ( int cnt = 0; cnt < 50; cnt++ )
- printf( "EXT ADC = %d\r\n", readExternalADC() );
+ printf( "EXT ADC = %d\r\n", readExternalADC(EXT_ADC_CHAN) );
break;
case 's':
@@ -160,7 +164,7 @@
led4 = 0;
int glitchCountInternal = glitchCount;
- int glitchCountExternal;
+ int glitchCountExternal = 0;
if ( useExternalADC )
{
@@ -183,11 +187,9 @@
led4 = 0;
glitchCountExternal = glitchCount;
- }
-
+ }
-
- // Output histoGram
+ // Output histoGram(s)
for ( int j = 0; j <= HGRAM_MAX_INDEX; j++ ) {
if ( useExternalADC )
printf( "%4d %8d %8d\r\n", j - (HGRAM_MAX_INDEX/2), histoGramA[j], histoGramB[j] );
@@ -195,6 +197,7 @@
printf( "%4d %8d\r\n", j - (HGRAM_MAX_INDEX/2), histoGramA[j] );
}
+ // Show glitch stats if glitch suppression was enabled
if ( glitchSuppressionWanted ) {
if ( useExternalADC )
printf( "\nglitchCount: Internal = %d External = %d\r\n",
@@ -224,7 +227,7 @@
int v1,v2,delta;
if ( externalADC )
- v1 = readExternalADC();
+ v1 = readExternalADC(EXT_ADC_CHAN);
else
v1 = ain.read_u16() >> 4;
@@ -236,7 +239,7 @@
// within the tolerance band --- i.e., no glitch
while(1){
if ( externalADC )
- v2 = readExternalADC();
+ v2 = readExternalADC(EXT_ADC_CHAN);
else
v2 = ain.read_u16() >> 4;
delta = v1 - v2;
@@ -246,12 +249,24 @@
}
}
-int readExternalADC( void ) { // Hardcoded for channel 0 (for quick test)
- mpc3304Select = EXT_ADC_ON;
- int byte1 = spi.write( 0xc ); // start=1 sgl=1 d2=0 d1=0
- int byte2 = spi.write( 0x0 ) & 0xf; // d0 = 0
+int readExternalADC( int channel ) {
+ int ch = channel & 0x7;
+ extAdcSelect = EXT_ADC_ON;
+
+ #ifdef MCP3208
+ int byte1 = spi.write( 0x6 | (ch >> 2) );
+ int byte2 = spi.write( (ch << 6) ) & 0xf; // d0 = 0
+ #else
+ #ifdef MCP3304
+ int byte1 = spi.write( 0xc | (ch >> 1) );
+ int byte2 = spi.write( (ch << 7) ) & 0xf; // d0 = 0
+ #else
+ error( "Undefined external ADC device" );
+ #endif
+ #endif
+
int byte3 = spi.write( 0xaa ); // dummy value, but nice pattern of logic analyzer
- mpc3304Select = EXT_ADC_OFF;
+ extAdcSelect = EXT_ADC_OFF;
int value = (byte2 << 8) | byte3;
//printf( "byte2 = %2x byte3 = %2x value = %d\r\n", byte2, byte3, value );
return value;