Scans through the DAC values, and copies a set of ADC values to file on USB local filesystem. Reads from the ADC directly. Connect pin 18 to pin 17.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Scans through the D/A converter values, and copies a set of A/D converter values to file on USB local filesystem.
00002 // Connect pin 18 to pin 17.
00003 
00004 #include "mbed.h"
00005 
00006 AnalogOut aout(p18);
00007 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00008 
00009 const int N = 100;  //number of samples per D/A value
00010 int i;
00011 int n;
00012 unsigned short x[N];
00013 short trim = 3; // ADC trim; appears this is added to the ADC readings in two's complement (i.e, -8 gives results in too low readings, 0 gives about correct readings, +3 gives really correct readings for my system, and +7 gives too high readings.) 
00014 
00015 // /4 /26 -> mbed
00016 // /8 /3 -> nice
00017 // /1 /8 -> 12MHz (max) (ugly)
00018 // /1 /12 (i.e., 11 in reg.) -> nice
00019  
00020 int main() {
00021     // A/D power on, set A/D clk divider /4
00022     LPC_SC->PCONP |= (1 << 12);        // power on, PCADC bit
00023     LPC_SC->PCLKSEL0 &= ~(0x1 << 24);  // PCLK_ADC=0,1,2,3 -> CCLK /4,/1,/2,/8
00024 
00025     // software-controlled ADC settings
00026     LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected yet
00027               | (11 << 8)    // CLKDIV: PCLK_ADC0 is divided by this number+1 (8 bits)
00028               | (0 << 16)    // BURST: 0 = software control 
00029               | (1 << 21)    // PDN: 1 = operational
00030               | (0 << 24);   // START: 0 = no start
00031 
00032     //p15 = LPC1768 pin  9 = P0.23 = AD0.0 = PINSEL1 bit 15&14 01  
00033     //p16 = LPC1768 pin  8 = P0.24 = AD0.1 = PINSEL1 bit 17&16 01
00034     //p17 = LPC1768 pin  7 = P0.25 = AD0.2 = PINSEL1 bit 19&18 01
00035     //p19 = LPC1768 pin 21 = P1.30 = AD0.4 = PINSEL3 bit 29&28 11 
00036     //p20 = LPC1768 pin 20 = P1.31 = AD0.5 = PINSEL3 bit 31&30 11
00037     //Same bits in PINMODEi, set these to 10 to disconnect pull up/pull down resistors
00038     LPC_PINCON->PINSEL1  &= ~((unsigned int)0x3 << 18);
00039     LPC_PINCON->PINSEL1  |=   (unsigned int)0x1 << 18;
00040     LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 18);
00041     LPC_PINCON->PINMODE1 |=   (unsigned int)0x2 << 18;   // neither pull up nor pull down
00042     LPC_ADC->ADCR &= ~0xFF;
00043     LPC_ADC->ADCR |= 1 << 2;   // ADC0[2]
00044 /*  LPC_PINCON->PINSEL3  &= ~((unsigned int)0x3 << 28);
00045     LPC_PINCON->PINSEL3  |=   (unsigned int)0x3 << 28;
00046     LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 28);
00047     LPC_PINCON->PINMODE3 |=   (unsigned int)0x2 << 28;   // neither pull up nor pull down
00048     LPC_ADC->ADCR &= ~0xFF;
00049     LPC_ADC->ADCR |= 1 << 4;  // ADC0[4]
00050 */
00051     LPC_ADC->ADTRM&=~0x00F0;
00052     LPC_ADC->ADTRM|=(trim<<4) & 0x00F0;   //set the trim
00053 
00054     FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00055     for (i=0;i<1024;i++) {
00056         aout.write_u16(i<<6);
00057         wait(0.000100);
00058         for (n=0;n<N;n++) {
00059             // Select channel and start conversion
00060             LPC_ADC->ADCR |= 1 << 24;
00061     
00062             // Repeatedly get the sample data until DONE or OVERRUN bit
00063             unsigned int data;
00064             do {
00065                data = LPC_ADC->ADGDR;
00066             } while ((data & ((unsigned int)3 << 30)) == 0);
00067             x[n]=data;
00068         }
00069         // Stop conversions    
00070         LPC_ADC->ADCR &= ~(1 << 24);
00071 
00072         fwrite(x,sizeof(x[0]),sizeof(x)/sizeof(x[0]),fp);
00073     }
00074     fclose(fp);
00075 }
00076