When I was reading the PIC16F1619 datasheet about how the ADC module is working and messing around in MPLAB Code Configurator, I found that enabling and working with the module is relatively easy. So in this post I'll explain the basic steps to enable it in MCC and putting some code in "main.c" file to test it. The test is to make the four LEDs switch in consecutive order, following the voltage across the potentiometer. The ADC module will read the voltage and will output the result to the LEDs.
A short video from my YouTube Channel describing the idea:
A short video from my YouTube Channel describing the idea:
You will have to have the following:
- The microcontroller.
- A PIC programmer.
- The software.
- A computer (PC or laptop).
- And time to spend :)
The first two a got them in one. I happen to have a Curiosity Development Board (Part number: DM164137), which comes with PIC16F1619.
The software you can download from Microchip website for free. You'll need to have installed MPLAB X IDE (link), XC8 compiler (link) and the Code Configurator (link).
Ok. Let's open the MPLAB X IDE and create a New Project. There are 7 steps to follow to create the project.
Select Microchip Embedded and Standalone Project. Hit Next.
Step 2. Select Device
On the next window you'll have to select your device. In this case it is PIC16F1619. Hit Next.
Step 3. Select Header
Leave the selection field as None. We'll not use the debug header for now.
Step 4. Select Tool
Here you have to select your tool for programming. In this case we'll use the built-in programmer in Curiosity Board. So select Starter Kits (PKOB).
Step 5. Select Plugin Board
This step is skipped.
Step 6. Select Compiler
Here you'll have to select the installed version of the compiler. In this case I'll select XC8 and the last version installed.
Step 7. Select Project Name and Folder
In this last step you'll have to select the name of the project and folder.
Hit Finish when you are done. Your screen should look like the one below and it depends from the version you use. At the time of creating this post the version is 4.01.
Next thing to do is to launch the MPLAB Code Configurator. You can do it in 2 ways, by the menu Tools or by clicking the icon on the top right corner.
After waiting some time to open the MCC you'll get the next screen, don't panic you'll get used to it.
First thing to do now is to choose the right package of the PIC, in this case it is PDIP20 - the device in the Curiosity board. I do this now because it is easier latter to navigate through pins and make decision where to place them. You can see it at the right corner, there is a representation of the PIC16F1619 with all 20 pins. Three of them are not available to the user - VDD (+5VDC,+3.3VDC), VSS (Ground) and MCLR (Reset).
Now we can include the ADC module in our project. Click on the black triangle close to ADC in the Device Resources and then double click on ADC. It will automatically add the ADC module to the project and will configure the settings to their default values.
You should see the following screen. The ADC is added to your Peripherals.
Ok. I'll focus now on Hardware Settings. As you can see in the image above the ADC is Enabled automatically and because I did not change the System Clock (500 kHz) in the System Module, the software calculates the Conversion Time to 46.0 us (for the experiment it is not so critical for now to change it to something smaller than that). Please also note that you should set the System Clock Select to INTOSC (Internal Oscillator) if you will use other than 8MHz or 16MHz frequencies with PLL enabled, otherwise you'll get a warning about PLL and errors may appear in the frequency. One important thing to modify is to change the Result Alignment from Left to Right.
I'll not explain in full detail all the options in this post, stick around for more detailed posts. Next thing to do is to open the Curiosity Development Board Datasheet and find the page with the schematics. It is needed so we can find the pins of interest. Below is a partial snapshot.
You can clearly see that the potentiometer is connected to pin RC0. So you need to click on that pin in the Pin Manager: Grid (see the image below). That way we assigned RC0 to be the Input of the ADC, from this pin it will read the analogue information and will convert it to a digital one.
The LEDs are connected to pins PGEC (RA1), RA2, RA5 and RC5. We have to add them in Pin Manager: Grid by clicking on the padlock on output row, they will turn to locked and green in color.
After that clicking on the Pin Module in Project Resources we'll have to set additional options. We have to disable the analog function by unchecking in the Analog column the box for RA1 and RA2. And also disable the Weak Pull-Up (WPU) resistor function for all four pins.
Finally you'll get the representation of the device with all assigned pins like the following.
And when you hit Generate, nearby Project Resources, and save the Configuration you'll see the files generated in the Projects file tree.
The main area we need to focus now is "main.c" file. MCC will generate a standard "blank" file, in which you need to add your code. I wrote "blank", but in fact it is not. Inside it is the skeleton with which we'll work. You can find also commented (not active) elements of code that can be used in your custom program.
Bellow is the code you need to enter in the your main function. Code lines in bold are the ones in interest.
--------------------------------------------------------------------------------
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
static uint16_t adcResult;
LED4_LAT = LED5_LAT = LED6_LAT = LED7_LAT = LOW;
while (1)
{
adcResult = ADC_GetConversion(channel_AN4);
if (adcResult >= 0 && adcResult <= 256)
{
LED4_LAT = HIGH;
LED5_LAT = LED6_LAT = LED7_LAT = LOW;
}
else if (adcResult > 256 && adcResult <= 512)
{
LED4_LAT = LED5_LAT = HIGH;
LED6_LAT = LED7_LAT = LOW;
}
else if (adcResult > 512 && adcResult <= 768)
{
LED4_LAT = LED5_LAT = LED6_LAT = HIGH;
LED7_LAT = LOW;
}
else if (adcResult > 768 && adcResult <= 1023)
{
LED5_LAT = LED6_LAT = LED4_LAT = LED7_LAT = HIGH;
}
else
{
LED4_LAT = LED6_LAT = HIGH;
LED5_LAT = LED7_LAT = LOW;
}
}
}
/**
End of File
*/
--------------------------------------------------------------------------------
Some explanations:
static uint16_t adcResult;
This line reserves a place in the memory with the name adcResult, also this place can't be modified by placing the static in front. This place is 16 bits long unsigned binary number.
Now we can include the ADC module in our project. Click on the black triangle close to ADC in the Device Resources and then double click on ADC. It will automatically add the ADC module to the project and will configure the settings to their default values.
You should see the following screen. The ADC is added to your Peripherals.
Ok. I'll focus now on Hardware Settings. As you can see in the image above the ADC is Enabled automatically and because I did not change the System Clock (500 kHz) in the System Module, the software calculates the Conversion Time to 46.0 us (for the experiment it is not so critical for now to change it to something smaller than that). Please also note that you should set the System Clock Select to INTOSC (Internal Oscillator) if you will use other than 8MHz or 16MHz frequencies with PLL enabled, otherwise you'll get a warning about PLL and errors may appear in the frequency. One important thing to modify is to change the Result Alignment from Left to Right.
I'll not explain in full detail all the options in this post, stick around for more detailed posts. Next thing to do is to open the Curiosity Development Board Datasheet and find the page with the schematics. It is needed so we can find the pins of interest. Below is a partial snapshot.
You can clearly see that the potentiometer is connected to pin RC0. So you need to click on that pin in the Pin Manager: Grid (see the image below). That way we assigned RC0 to be the Input of the ADC, from this pin it will read the analogue information and will convert it to a digital one.
The LEDs are connected to pins PGEC (RA1), RA2, RA5 and RC5. We have to add them in Pin Manager: Grid by clicking on the padlock on output row, they will turn to locked and green in color.
After that clicking on the Pin Module in Project Resources we'll have to set additional options. We have to disable the analog function by unchecking in the Analog column the box for RA1 and RA2. And also disable the Weak Pull-Up (WPU) resistor function for all four pins.
Finally you'll get the representation of the device with all assigned pins like the following.
And when you hit Generate, nearby Project Resources, and save the Configuration you'll see the files generated in the Projects file tree.
The main area we need to focus now is "main.c" file. MCC will generate a standard "blank" file, in which you need to add your code. I wrote "blank", but in fact it is not. Inside it is the skeleton with which we'll work. You can find also commented (not active) elements of code that can be used in your custom program.
Bellow is the code you need to enter in the your main function. Code lines in bold are the ones in interest.
--------------------------------------------------------------------------------
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
static uint16_t adcResult;
LED4_LAT = LED5_LAT = LED6_LAT = LED7_LAT = LOW;
while (1)
{
adcResult = ADC_GetConversion(channel_AN4);
if (adcResult >= 0 && adcResult <= 256)
{
LED4_LAT = HIGH;
LED5_LAT = LED6_LAT = LED7_LAT = LOW;
}
else if (adcResult > 256 && adcResult <= 512)
{
LED4_LAT = LED5_LAT = HIGH;
LED6_LAT = LED7_LAT = LOW;
}
else if (adcResult > 512 && adcResult <= 768)
{
LED4_LAT = LED5_LAT = LED6_LAT = HIGH;
LED7_LAT = LOW;
}
else if (adcResult > 768 && adcResult <= 1023)
{
LED5_LAT = LED6_LAT = LED4_LAT = LED7_LAT = HIGH;
}
else
{
LED4_LAT = LED6_LAT = HIGH;
LED5_LAT = LED7_LAT = LOW;
}
}
}
/**
End of File
*/
--------------------------------------------------------------------------------
Some explanations:
static uint16_t adcResult;
This line reserves a place in the memory with the name adcResult, also this place can't be modified by placing the static in front. This place is 16 bits long unsigned binary number.
------------------------------------------------------------------------------------------------------------------------
Not final. To be continued :)
Comments
Post a Comment