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){ 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; }
void fis_ADC_config(void){ unsigned int config1; unsigned int config2; unsigned int config3; unsigned int configportL,configportH, configscanL, configscanH; config1 = ADC_MODULE_OFF & ADC_IDLE_CONTINUE & ADC_FORMAT_INTG & ADC_CLK_MANUAL & ADC_AUTO_SAMPLING_ON; config2 = ADC_VREF_AVDD_AVSS & ADC_SCAN_ON & ADC_INTR_2_CONV & ADC_ALT_BUF_OFF & ADC_ALT_INPUT_OFF; config3 = ADC_SAMPLE_TIME_10 & ADC_CONV_CLK_SYSTEM & ADC_CONV_CLK_1Tcy; //registros ya configurados en pic_pc104_config.c configportL = ENABLE_AN11_ANA & ENABLE_AN13_ANA; //_PCFG11=0; //pin 0-ADC 1- I/O //_PCFG13=0; //pin 0-ADC 1- I/O configportH = 0xFFFF; //1=digital 0=analog // 7654321076543210 configscanL = 0b0010100000000000;//ADC_SCAN_AN11 & ADC_SCAN_AN13; //1=scan 0=skip configscanH = 0x0000; //_CSSL11=1; //_CSSL13=1; AD1CHS0= ADC_CH0_NEG_SAMPLEA_VREFN & ADC_CH0_POS_SAMPLEA_AN11; OpenADC10_v2(config1,config2,config3,configportL,configportH,configscanL,configscanH); EnableADC1; //AD1CON1bits.SAMP = 1; ConfigIntADC10(ADC_INT_DISABLE & ADC_INT_PRI_2 ); //IFS0bits.AD1IF = 0; //printf("adc_config\n"); }
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 platform_setup_adcs() { unsigned id; for( id = 0; id < NUM_ADC; id ++ ) adc_init_ch_state( id ); CloseADC10(); // Ensure the ADC is off before setting the configuration OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, PARAM5); ConfigIntADC10(ADC_INT_ON | ADC_INT_PRI_3 | ADC_INT_SUB_PRI_1); platform_adc_set_clock( 0, 0 ); }
int main(void) { BOARD_Init(); // Configure Timer 1 using PBCLK as input. This default period will make the LEDs blink at a // pretty reasonable rate to start. OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, 0xFFFF); // Set up the timer interrupt with a priority of 4. INTClearFlag(INT_T1); INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T1, INT_ENABLED); // Enable interrupts for the ADC ConfigIntADC10(ADC_INT_PRI_2 | ADC_INT_SUB_PRI_0 | ADC_INT_ON); // Set B2 to an input so AN0 can be used by the ADC. TRISBCLR = 1 << 2; // Configure and start the ADC // Read AN0 as sample a. We don't use alternate sampling, so setting sampleb is pointless. SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); OpenADC10( ADC_MODULE_ON | ADC_IDLE_CONTINUE | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON, ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_8 | ADC_BUF_16 | ADC_ALT_INPUT_OFF, ADC_SAMPLE_TIME_29 | ADC_CONV_CLK_PB | ADC_CONV_CLK_51Tcy2, ENABLE_AN2_ANA, SKIP_SCAN_ALL); EnableADC10(); /*************************************************************************************************** * Your code goes in between this comment and the following one with asterisks. **************************************************************************************************/ printf("Welcome to the Lab 6 Extra Credit blank. Please remove before starting."); /*************************************************************************************************** * Your code goes in between this comment and the preceding one with asterisks **************************************************************************************************/ while (1); }
/**************************************************************************** Function AD_Init Parameters Pins, used #defined AD_PORTxxx OR'd together for each A/D Pin Returns SUCCESS or ERROR Description Initializes the A/D pins requested into analog inputs and configures the A/D subsystem. It then generates the mapping for correctly reading the pin and then starts the A/D system. Notes None. Author Max Dunne, 2011.12.10 ****************************************************************************/ unsigned char AD_Init(unsigned int Pins) { unsigned int cssl = 0; unsigned int pcfg = 0; unsigned char CurPin = 0; unsigned int PinCount2 = 0x00; int ADMapping[NUM_AD_PINS_UNO]; if ((Pins == 0) || (Pins > 0x3FFF)) { return ERROR; } for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) { ADMapping[CurPin] = -1; } for (CurPin = 0; CurPin < NUM_AD_PINS; CurPin++) { PortMapping[CurPin] = -1; if ((Pins & (1 << CurPin)) != 0) { cssl |= AD1CSSL_MASKS[CurPin]; pcfg |= AD1PCFG_MASKS[CurPin]; ADMapping[AD1PCFG_POS[CurPin]] = CurPin; PinCount++; } } for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) { if (ADMapping[CurPin] != -1) { PortMapping[ADMapping[CurPin]] = PinCount2; PinCount2++; } } UsedPins = Pins; cssl = ~cssl; OpenADC10(ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON, ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ((PinCount - 1) << _AD1CON2_SMPI_POSITION) | ADC_BUF_16, ADC_SAMPLE_TIME_31 | ADC_CONV_CLK_32Tcy | ADC_CONV_CLK_PB, pcfg, cssl); ConfigIntADC10(ADC_INT_ON | ADC_INT_PRI_1 | ADC_INT_SUB_PRI_3); EnableADC10(); return SUCCESS; }
void GP2D12_Init(){ // Intitialize distance! distance = 0.0; // configure and enable the ADC CloseADC10(); // ensure the ADC is off before setting the configuration // define setup parameters for OpenADC10 // Turn module on | ouput in integer | trigger mode auto | enable autosample DWORD PARAM1 = ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON; // define setup parameters for OpenADC10 // ADC ref external | disable offset test | disable scan mode | perform 2 samples | use dual buffers | use alternate mode DWORD PARAM2 = ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_BUF_8 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF; // define setup parameters for OpenADC10 // use ADC internal clock | set sample time DWORD PARAM3 = ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_1 | (0x00FF << _AD1CON3_ADCS_POSITION); // define setup parameters for OpenADC10 // set AN4 and AN5 as analog inputs DWORD PARAM4 = 0; DWORD PARAM6 = 0; //ENABLE_AN4_ANA | ENABLE_AN5_ANA switch(GP2D12_ADC_CHANNEL){ case 0: PARAM4 = ENABLE_AN0_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0; break; case 1: PARAM4 = ENABLE_AN1_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN1; break; case 2: PARAM4 = ENABLE_AN2_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2; break; case 3: PARAM4 = ENABLE_AN3_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN3; break; case 4: PARAM4 = ENABLE_AN4_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4; break; case 5: PARAM4 = ENABLE_AN5_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN5; break; case 6: PARAM4 = ENABLE_AN6_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN6; break; case 7: PARAM4 = ENABLE_AN7_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN7; break; case 8: PARAM4 = ENABLE_AN8_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8; break; case 9: PARAM4 = ENABLE_AN9_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN9; break; case 10: PARAM4 = ENABLE_AN10_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN10; break; case 11: PARAM4 = ENABLE_AN11_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11; break; case 12: PARAM4 = ENABLE_AN12_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN12; break; case 13: PARAM4 = ENABLE_AN13_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN13; break; case 14: PARAM4 = ENABLE_AN14_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN14; break; case 15: PARAM4 = ENABLE_AN15_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN15; break; default: PARAM4 = ENABLE_ALL_DIG; break; } if(PARAM4 == ENABLE_ALL_DIG) return; // define setup parameters for OpenADC10 // do not assign channels to scan DWORD PARAM5 = SKIP_SCAN_ALL; // use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN5 for input B // configure to sample AN4 & AN5 SetChanADC10(PARAM6); // configure to sample AN4 & AN5 OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above //ConfigIntADC10(ADC_INT_PRI_3 | ADC_INT_SUB_PRI_2 | ADC_INT_ON); ConfigIntADC10(ADC_INT_OFF); EnableADC10(); // Enable the ADC }
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; }