int main(void){ mPORTBSetPinsAnalogIn(0xFFFF); //Enable all analog mPORTDClearBits(BIT_0 |BIT_1 | BIT_8); mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_8); SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); CloseADC10(); SetChanADC10(INITCH); OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN); ConfigIntADC10(CFGINT); EnableADC10(); mPMPOpen(PMP_CONTROL, PMP_MODE, PMP_PORT, PMP_INT); mPMPEnable(); //mPORTDClearBits(BIT_0 |BIT_1 | BIT_8); INTEnableSystemMultiVectoredInt(); OpenTimer2(T2_ON | T2_PS_1_64, 256); ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_5 | T2_INT_SUB_PRIOR_2); EnableIntT2; mPORTDToggleBits(BIT_0); while(1){ //Blink so we know we're still running noteOn(67, 100); //delayMs(1000); mPORTDToggleBits(BIT_0); //noteOff(67); DelayMs(1000); //mPORTDToggleBits(BIT_0); } return 0; }
int main(int argc, char** argv) { //DDPCONbits.JTAGEN = 0; // Disable JTAG mPORTGSetPinsDigitalIn(0x00FF); mPORTBSetPinsAnalogIn(0x00FF); //Enable all analog SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); CloseADC10(); SetChanADC10(INITCH); OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN); ConfigIntADC10(CFGINT); EnableADC10(); //Initialize the DB_UTILS IO channel DBINIT(); // Display the introduction DBPRINTF("Welcome to the Analog Input Test.\n"); DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n"); while (1){ //Get damper value from PIC analog input int i = 0; for(i;i<1;i++){ int analog = ReadADC10(i); DBPRINTF("Hammer %d = %d", i, analog); DBPRINTF("\n"); } int digital = mPORTGReadBits(BIT_0); DBPRINTF("digital: %X \n \n", digital ); } return 0; }
int main(void){ mPORTASetPinsDigitalIn(0xFFFF); mPORTBSetPinsAnalogIn(0xFFFF); //Enable all analog SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); CloseADC10(); SetChanADC10(INITCH); OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN); ConfigIntADC10(CFGINT); EnableADC10(); //Initialize the DB_UTILS IO channel DBINIT(); // Display the introduction DBPRINTF("Welcome to the Analog Input Test.\n"); DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n"); while (1){ //Get damper value from PIC analog input int i = 0; for(i;i<16;i++){ int damper = ReadADC10(i); DBPRINTF("Damper %d = %d", i, damper); DBPRINTF("\n"); } //DBPRINTF("%d \n", LATA ); } return 0; }
void init_adc(void) { // configure and enable the ADC // A0 = AN2, A1 = AN4 CloseADC10(); // ensure the ADC is off before setting the configuration AD1CHS = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2; // configure to sample AN2 mPORTBSetPinsAnalogIn(SKIP_SCAN_ALL); AD1CSSL = ~(ENABLE_AN2_ANA | ENABLE_AN4_ANA); AD1CON3 = ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_15 | ADC_CONV_CLK_16Tcy; AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | \ ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF; AD1CON1 = ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_MANUAL | ADC_AUTO_SAMPLING_OFF; EnableADC10(); // Enable the ADC //AcquireADC10(); }
void initADC() { //portb as anaog input mPORTBSetPinsAnalogIn(BIT_3); // configure and enable the ADC CloseADC10(); // ensure the ADC is off before setting the configuration // define setup parameters for OpenADC10 // Turn module on | output in integer | trigger mode auto | enable autosample #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON // define setup parameters for OpenADC10 // ADC ref external | disable offset test | enable scan mode | perform 2 samples | use one buffer | use MUXA mode // note: to read X number of pins you must set ADC_SAMPLES_PER_INT_X #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF // define setup parameters for OpenADC10 // use ADC internal clock | set sample time #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15 // define setup parameters for OpenADC10 // set AN3 #define PARAM4 ENABLE_AN3_ANA // define setup parameters for OpenADC10 // do not assign channels to scan #define PARAM5 SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN4 | SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15 // use ground as neg ref for A SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF); // use ground as the negative reference OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, PARAM5); // configure ADC using parameter define above EnableADC10(); // Enable the ADCd while (!mAD1GetIntFlag()) { } // wait for the first conversion to complete so there will be valid data in ADC result registers }
int main(void) { /******************* Setup *****************************/ mPORTBSetPinsAnalogIn(0xFFFF); //Enable all analog //mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_8); SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); CloseADC10(); SetChanADC10(INITCH); OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN); ConfigIntADC10(CFGINT); EnableADC10(); //Initialize the DB_UTILS IO channel DBINIT(); // Display the introduction DBPRINTF("Welcome to the PIC32 Starter Kit Tutorial.\n"); DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n"); /********************* Declarations *********************/ int damper = 0; //ch1 binary read int damperchnum = 0; //damper adc number int hammerchnum = 1; //hammer adc number int samplesize = 1000; int hammerdata[10][samplesize]; //collection of hammer values int damperdata[10][samplesize]; //collection of damper values int index = 0; //index of hammerdata and damperdata arrays int collection[2] = {0,0}; int count = 0; /******************** Initialize Arrays ******************/ int x; int y; for( x = 0; x < 10; x++) { for( y = 0; y < samplesize; y++) { damperdata[x][y] = 0; } } /************************ Stuff to do *********************/ while (count < 4){ damper = ReadADC10(damperchnum); if(damper > 100) { //store data collection[0] = collection[1]; //we are collecting data, falling edge detection collection[1] = 1; if(index<samplesize) { hammerdata[count][index] = ReadADC10(hammerchnum); damperdata[count][index] = damper; index++; //DBPRINTF("Damper: %d \n", damperdata[count][index-1]); } }else{ collection[0] = collection[1]; collection[1] = 0; } if(collection[1] < collection[0]) //rising edge detection { DBPRINTF("Count: %d \n", count); count++; index = 0; } } int i; /******************** Output Memory at End **********************/ for( i = 0; i < 1000; i++) { DBPRINTF("Damper data: %d ", damperdata[1][i]); DBPRINTF("\n"); } return 0; }