Academic Program

Main.c

Main.c

Step 10 - The ADC result will produce a value between 0 and 1023 depending on the position of the potentiometer. A value of 0 is the position all the way counterclockwise. A value of 1023 is all the way clockwise. The half-way point is a value of 512. We shall test the position by comparing the value to 512. If it is larger than 512, then the LED will be lit. If it is smaller than or equal to 512, then the LED will be off.

Add the following code to the end of the main.c file:

while (1) {
        ADCC_Initialize();

        if (ADCC_GetConversion(Pot_A0) < 512) {
            D2_LED_SetHigh();
        } else {
            D2_LED_SetLow();
        }
    }

 

The ADCC functions in main.c are generated by MCC and defined in the adcc.h file.

This macro initializes the ADCC peripheral:
ADCC_Initialize()
This macro is used to select desired channel for conversion:
ADCC_GetConversion(Pot_A0)

The D2_LED_SetHigh() and D2_LED_SetLow() functions are macros that were generated by MCC, and their definitions are located in the pin_manager.h file.

#define D2_LED_SetHigh()            do { LATAbits.LATA4 = 1; } while(0)
#define D2_LED_SetLow()             do { LATAbits.LATA4 = 0; } while(0)

 

Step 11 - Click on the Build Project icon (the hammer) to compile the code. You should see a "BUILD SUCCESSFUL" message in the output window.

Main_Build_Project.png

BUILD SUCCESSFUL