コード例 #1
0
ファイル: main.c プロジェクト: ctapang/v0_70_01b
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;
}
コード例 #2
0
ファイル: drv_adc_static.c プロジェクト: ECE4534-Team21/gary
// *****************************************************************************
// *****************************************************************************
// Section: ADC Static Driver Functions
// *****************************************************************************
// *****************************************************************************
void DRV_ADC_Initialize(void)
{
    /* Select Clock Source */
    PLIB_ADC_ConversionClockSourceSelect(DRV_ADC_ID_1, ADC_CLOCK_SOURCE_SYSTEM_CLOCK);
    /* Select Clock Prescaler */
    PLIB_ADC_ConversionClockSet(DRV_ADC_ID_1, SYS_CLK_FREQ, 320000000);

    /* Select Power Mode */
    PLIB_ADC_StopInIdleDisable(DRV_ADC_ID_1);
	
    /* Select Voltage Reference */
    PLIB_ADC_VoltageReferenceSelect(DRV_ADC_ID_1, ADC_REFERENCE_VDD_TO_AVSS);

    /* Sampling Selections */
    /* Select Sampling Mode */
    PLIB_ADC_SamplingModeSelect(DRV_ADC_ID_1, ADC_SAMPLING_MODE_MUXA);
    /* Number of Samples Per Interrupt */
    PLIB_ADC_SamplesPerInterruptSelect(DRV_ADC_ID_1, ADC_2SAMPLES_PER_INTERRUPT);

    /* Conversion Selections */
    /* Select Trigger Source */
    PLIB_ADC_ConversionTriggerSourceSelect(DRV_ADC_ID_1, ADC_CONVERSION_TRIGGER_INTERNAL_COUNT);
    /* Select Result Format */
    PLIB_ADC_ResultFormatSelect(DRV_ADC_ID_1, ADC_RESULT_FORMAT_INTEGER_16BIT);
    /* Buffer Mode */
    PLIB_ADC_ResultBufferModeSelect(DRV_ADC_ID_1, ADC_BUFFER_MODE_ONE_16WORD_BUFFER);

    /* Channel Selections */
    /* MUX A Negative Input Select */
    PLIB_ADC_MuxChannel0InputNegativeSelect(DRV_ADC_ID_1, ADC_MUX_A, ADC_INPUT_NEGATIVE_VREF_MINUS);
 

    /* MUX A Positive Input Select */
    PLIB_ADC_MuxChannel0InputPositiveSelect(DRV_ADC_ID_1, ADC_MUX_A, ADC_INPUT_POSITIVE_AN0);
 
    /* Initialize ADC Interrupt */
    PLIB_INT_SourceFlagClear(INT_ID_0, INT_SOURCE_ADC_1);
    PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_ADC_1);
    PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_AD1, INT_PRIORITY_LEVEL1);
    PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_AD1, INT_SUBPRIORITY_LEVEL0);	
}