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.
  1. Create a new MPLAB X project for AVR128DA48.
    1. Open MPLAB X.
    2. Select File → New Project or the New Project button.
      Figure 2-1. New Project
    3. Click Next (Microchip Embedded Stand-alone Project is selected by default).
      Figure 2-2. Project Type
    4. 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
    5. Select the XC8 2.20 compiler and click Next.
      Figure 2-4. Compiler Selection
    6. Give a name to the project (and the location where to be saved) and click Finish.
      Figure 2-5. Project Name
  2. Open MPLAB Code Configurator (MCC) and configure the peripherals.
    1. 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
    2. 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
    3. 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
    4. Press the Generate button.
      Figure 2-9. Generate Button
  3. Add code to the project.
    1. 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 in mcc.c, ADC0_StartConversion(channel), ADC0_IsConversionDone(), and ADC0_GetConversionResult() are defined in adc0.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 channel
      • ADC0_IsConversionDone() returns the status of the conversion: 0 for in progress and 1 for finished
      • ADC0_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
    2. In the pin_manager.c file found in Source Files → MCC Generated files → src 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.
    3. Press the Clean and Build button from the toolbar and verify that the program builds without errors.
      Figure 2-10. Clean and Build