/** * @private * Initialise the Analog Digital Converter * @param CONFIG1 bit-masks first turns the module on, formats the output as 32-bit integers, * triggers its conversions automatically, and automatically samples. * @param CONFIG2 bit-masks sets the voltage references to Vdd and Vss, disables the offset calculation, * enables scanning, specifies how many samples are to be read per interrupt (which * should be changed if multiple analog pins are being read?ADC_SAMPLES_PER_INT_x where x is the * number of pins to be read), disables the buffer, and disables input sample mode. * @param CONFIG3 bit-masks selects which clock to use, and selects the auto-sample time bits. * @param CONFIGPORT bit-masks selects which analog pins are enabled. This is similar to setting the AD1PCFG register. * @param CONFIGSCAN bit-masks selects which analog pins are to be scanned or skipped. */ void _initADC(){ CloseADC10(); SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF); OpenADC10(CONFIG1, CONFIG2, CONFIG3, CONFIGPORT, CONFIGSCAN); EnableADC10(); // mAD1GetIntFlag() checks the interrupt flag for the AD10. // Waits till a conversion is finished so that there's // values in the ADC result registers. while (!mAD1GetIntFlag() ) { } }
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 }
/* * Set up ADC10 to read Cerebot battery monitor AN8. * If you are having problems, verify jumper JP4 is on. */ void init_battery_monitor() { // configure and enable the ADC // ensure the ADC is off before setting the configuration CloseADC10(); // use ground as neg ref for A | use AN8 for input A // configure to sample AN8 SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8); // Configure the ADC // 16 bit integer, auto convert, auto sampling select. // // Use AVDD and AVSS for reference, disable calibration, do not scan, // interrupt at completion of every 2nd sample, use 2 8 word buffers, // alternate between muxes. // // Use internal clock, set sample time // // Do not assign channels to scan // // Set AN8 as analog input OpenADC10( ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON, ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON, ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15, ENABLE_AN8_ANA, SKIP_SCAN_ALL); // Enable the ADC EnableADC10(); // Wait for the first conversion to complete so there will be vaild // data in ADC result registers while ( ! mAD1GetIntFlag() ); }
//********************************************************************* int analogRead(uint8_t pin) { int analogValue; unsigned int offset; // buffer offset to point to the base of the idle buffer unsigned int param4; uint8_t channelNumber; //* in most cases (except the uno board) this will be a 1 to 1 mapping channelNumber = analogInPinToBit(pin); //* May 1, 2011 Gene Apperson of Digitlent sent me PIC32MX5XX-6XX-7XX Errata.pdf //* item #26 documents a condition that I/O pins take time if previously set to outputs //* first attempt, set all to 0 // AD1PCFG = 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 #define PARAM1 ADC_FORMAT_INTG16 | 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 #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_ON // 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 AN4 and AN5 as analog inputs param4 = channelNumber; // define setup parameters for OpenADC10 // do not assign channels to scan #define 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 // SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 | ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5); // configure to sample AN4 & AN5 // SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0 | ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN1); // configure to sample AN4 & AN5 // OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above switch(channelNumber) { case 0: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0); break; case 1: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN1); break; case 2: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); break; case 3: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN3); break; case 4: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4); break; case 5: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN5); break; case 6: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN6); break; case 7: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN7); break; case 8: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8); break; case 9: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN9); break; case 10: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN10); break; case 11: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11); break; case 12: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN12); break; case 13: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN13); break; case 14: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN14); break; case 15: SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN15); break; } // configure to sample what ever pin was specified OpenADC10( PARAM1, PARAM2, PARAM3, param4, PARAM5 ); // configure ADC using the parameters defined above EnableADC10(); // Enable the ADC //* A delay is needed for the the ADC start up time //* this value started out at 1 millisecond, I dont know how long it needs to be //* 99 uSecs will give us the same approximate sampling rate as the AVR chip // delay(1); delayMicroseconds(99); while ( ! mAD1GetIntFlag() ) { // wait for the first conversion to complete so there will be vaild data in ADC result registers } analogValue = ReadADC10(0); mAD1ClearIntFlag(); return (analogValue); }
virtual reg_t read_analog(){ while (!mAD1GetIntFlag()) Nop(); reg_t val = ReadADC10(bit); mAD1ClearIntFlag(); return val; }
int main(void) { unsigned int temp; // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); /*PORT CONFIGURATION for UART*/ // Blinky LED for Uart activity //mPORTAClearBits(BIT_7); //mPORTASetPinsDigitalOut(BIT_7); // PINS used for the buttons PORTSetPinsDigitalIn(IOPORT_B, BIT_2 | BIT_3 | BIT_4); #define CONFIG (CN_ON | CN_IDLE_CON) #define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2) mCNOpen(CONFIG, CN4_ENABLE | CN5_ENABLE | CN6_ENABLE, CN4_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE); temp = mPORTBRead(); //Analog input CloseADC10(); #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_BUF_16 | ADC_ALT_INPUT_OFF #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_5 #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_ANA #define PARAM5 SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | 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 SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0); OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); EnableADC10(); //PORT D for motors mPORTDClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9 | BIT_10 | BIT_11 | BIT_12 | BIT_13 | BIT_14 | BIT_15); // Turn off PORTD on startup. mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9 | BIT_10 | BIT_11 | BIT_12 | BIT_13 | BIT_14 | BIT_15); // Make PORTD output. // Explorer-16 uses UART2 to connect to the PC. // This initialization assumes 36MHz Fpb clock. If it changes, // you will have to modify baud rate initializer. UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), DESIRED_BAUDRATE); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART2 RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); // Congifure Change/Notice Interrupt Flag ConfigIntCN(INTERRUPT); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); WriteString("*** UART Interrupt-driven Example ***\r\n"); unsigned int channel1, channel2; unsigned int motor1Time, motor2Time; // Let interrupt handler do the work while (1) { while ( ! mAD1GetIntFlag() ); channel1 = ReadADC10(0); channel2 = ReadADC10(1); motor1Time = (channel1*(60000)/(1023) + 80000); motor2Time = (channel2*(60000)/(1023) + 80000); //maximo valor de motorTime = 140000 //use motor time for stepping delay if (MotorsON) { if (M1forward) { adelante(1, motor1Time, 1); } else { atras(1, motor1Time, 1); } if (M2forward) { adelante(1, motor2Time, 3); } else { atras(1, motor2Time, 3); } } mAD1ClearIntFlag(); } return 0; }