int main(void) { /* Disable JTAG to free up PORTA pins */ DDPCONbits.JTAGEN = 0; /* Set lower 8 bits of PORTA as output for LEDs, clear the bits to ensure there is no mismatch when they are toggled */ PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); PLIB_PORTS_Clear(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); /* Set pin AN2 as analog input */ PLIB_PORTS_PinModeSelect(PORTS_ID_0, PORTS_ANALOG_PIN_2, PORTS_PIN_MODE_ANALOG); /* ADC setup - ouput in integer format, trigger mode auto, enable autosample */ PLIB_ADC_ResultFormatSelect(ADC_ID_1, ADC_RESULT_FORMAT_INTEGER_16BIT); PLIB_ADC_SamplingModeSelect(ADC_ID_1, ADC_SAMPLING_MODE_MUXA); PLIB_ADC_ConversionTriggerSourceSelect(ADC_ID_1, ADC_CONVERSION_TRIGGER_INTERNAL_COUNT); PLIB_ADC_VoltageReferenceSelect(ADC_ID_1, ADC_REFERENCE_VDD_TO_AVSS ); PLIB_ADC_SampleAcqusitionTimeSet(ADC_ID_1, 0x1F); PLIB_ADC_ConversionClockSet(ADC_ID_1, SYS_FREQUENCY, 32); /* Connect AN2 as positive input, Vref- as negative input */ PLIB_ADC_MuxChannel0InputPositiveSelect(ADC_ID_1, ADC_MUX_A, ADC_INPUT_POSITIVE_AN2); PLIB_ADC_MuxChannel0InputNegativeSelect(ADC_ID_1, ADC_MUX_A, ADC_INPUT_NEGATIVE_VREF_MINUS); /* Enable the ADC module */ PLIB_ADC_Enable(ADC_ID_1); int potValue; int i; int ledMask; /* Stuck in this loop, reading the potentiometer and writing to the LEDs */ while (1) { potValue = readPotentiometer(); potValue = potValue >> 7; /* 10-bit value to 3-bit value */ ledMask = 0; /* Creates a mask for the LEDs, corresponding to the value read from the potentiometer */ for (i = 0; i <= potValue; i++) { ledMask |= 1<<(i); } /* Write the mask to the LEDs */ PLIB_PORTS_Write(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)ledMask); } /* Program should not go here during normal operation */ return EXIT_FAILURE; }
//runs once void SENSOR_Initialize ( void ) { sensorData.sendCount = 0; //Initialize task variables sensorData.state = SENSOR_STATE_INIT; //Create a queue capable of holding 25 unsigned long numbers sensorData.q_adc_interrupt = xQueueCreate( 2500, sizeof( unsigned int ) ); if( sensorData.q_adc_interrupt == 0 ) { dbgOutputVal(SENSOR_QUEUE_FAIL); stopAll(); } //Create a timer with rollover rate 100ms sensorData.t_adc_interrupt = xTimerCreate( "SensorTimer", //Just a text name ( LR_SENSOR_TIMER_RATE / portTICK_PERIOD_MS ), //period is 100ms pdTRUE, //auto-reload when expires (void *) 29, //a unique id sensorTimerCallback ); //pointer to callback function //Start the timer if( sensorData.t_adc_interrupt == NULL ) { dbgOutputVal(SENSOR_TIMERINIT_FAIL); stopAll(); } else if( xTimerStart( sensorData.t_adc_interrupt, 0 ) != pdPASS ) { dbgOutputVal(SENSOR_TIMERINIT_FAIL); stopAll(); } //Initialize ADC A0 = Pic32 pin 25, RB0. Manual Sample Start and TAD based Conversion Start PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_0); PLIB_ADC_SampleAutoStartDisable(ADC_ID_1); PLIB_ADC_Enable(ADC_ID_1); }
inline void DRV_ADC_Open(void) { /* Enable ADC */ PLIB_ADC_Enable(DRV_ADC_ID_1); }