2.1 Creating the Software Implementation Project
Todo: Configure
the hardware modules that will be used in this project and add code to the
project.
- Create a new MPLAB X project for
AVR128DA48.
- Open MPLAB X.
- Select
Figure 2-1. New Project
or the New Project button. - Click Next (Microchip
Embedded Stand-alone Project is selected by default).
Figure 2-2. Project Type - In the Device field search
for: AVR128DA48. In the Tool category, select the Curiosity Nano
board if it is connected to the computer, otherwise select
None. Click Next.
Figure 2-3. Device Selection - Select the XC8 2.20
compiler and click Next.
Figure 2-4. Compiler Selection - Give a name to the project
(and the location where to be saved) and click Finish.
Figure 2-5. Project Name
- Open MPLAB Code Configurator (MCC)
and configure the peripherals.
- Configure the System Module
to run on the internal oscillator at 24 MHz.Info: The clock source will be the internal oscillator and the frequency 24 MHz. The prescaler option must be disabled.
Figure 2-6. System Module Configuration - Add the VREF module from
Device resources and configure it to provide 2.048V reference to the
ADC.Info: The ADC voltage reference must be configured to the internal 2.048V reference. Do not enable the Force ADC Voltage option.Info: The VREF is used to enable the temperature sensor of the microcontroller.
Figure 2-7. VREF Configuration - Add the ADC module from
device resources and configure it in 12-bit mode, right-adjusted results, no
accumulation and 31 ticks for sample length.Info: Right-shifted results option must be disabled. The sample length is selected in the Hardware Settings tab. The 12-bit mode is set by default. The same is with the no accumulation selection.
Figure 2-8. ADC Configuration - Press the Generate
button.
Figure 2-9. Generate Button
- Configure the System Module
to run on the internal oscillator at 24 MHz.
- Add code to the project.
- Add the following code to the
main.c
file.#include "mcc_generated_files/mcc.h" uint32_t accumulator = 0; int main(void) { SYSTEM_Initialize(); while (1){ accumulator = 0; for(uint8_t i = 0; i<128; i++) { ADC0_StartConversion(ADC_MUXPOS_TEMPSENSE_gc); while(!ADC0_IsConversionDone()) { ; } accumulator += ADC0_GetConversionResult(); } } }
Info:SYSTEM_Initialize()
is defined inmcc.c
,ADC0_StartConversion(channel)
,ADC0_IsConversionDone()
, andADC0_GetConversionResult()
are defined inadc0.c
.SYSTEM_Initialize()
is a function that configures the microcontroller and peripherals according to what was set in MCC. It is generated by MCC and needs to be called at the beginning of the program.ADC0_StartConversion(channel)
starts an ADC conversion on the given channelADC0_IsConversionDone()
returns the status of the conversion: 0 for in progress and 1 for finishedADC0_GetConversionResult()
returns the result of the last conversion- The for loop repeats the steps for starting a conversion, waiting for it to end and adding the result to the accumulator 128 times
- In the
pin_manager.c
file found in replace the code for the direction of the pins with:PORTA.DIR = 0xFF; PORTB.DIR = 0xFF; PORTC.DIR = 0x3F; PORTD.DIR = 0xFF; PORTE.DIR = 0xFF; PORTF.DIR = 0xFF;
Info: This is done to prevent floating pins that will disturb the power reading. - Press the Clean and
Build button from the toolbar and verify that the program builds
without errors.
Figure 2-10. Clean and Build
- Add the following code to the