void SYS_PORTS_PinModeSelect ( PORTS_MODULE_ID index, PORTS_ANALOG_PIN pin, PORTS_PIN_MODE mode) { #if defined(PLIB_PORTS_ExistsPinMode) if(PLIB_PORTS_ExistsPinMode(index)) { PLIB_PORTS_PinModeSelect ( index, pin, mode); } #endif }
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; }
void SYS_PORTS_Initialize(void) { /* AN and CN Pins Initialization */ PLIB_PORTS_AnPinsModeSelect(PORTS_ID_0, SYS_PORT_AD1PCFG, PORTS_PIN_MODE_DIGITAL); PLIB_PORTS_CnPinsPullUpEnable(PORTS_ID_0, SYS_PORT_CNPUE); PLIB_PORTS_CnPinsEnable(PORTS_ID_0, SYS_PORT_CNEN); PLIB_PORTS_ChangeNoticeEnable(PORTS_ID_0); /* PORT D Initialization */ PLIB_PORTS_DirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_B, SYS_PORT_D_TRIS ^ 0xFFFF); //PLIB_PORTS_DirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_B, 0x2000); PLIB_PORTS_OpenDrainEnable(PORTS_ID_0, PORT_CHANNEL_B, SYS_PORT_D_ODC); PLIB_PORTS_PinModeSelect (PORTS_ID_0, PORTS_ANALOG_PIN_11, PORTS_PIN_MODE_DIGITAL); }