2.2 Creating the Hardware 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-11. New Project
    3. Click Next (Microchip Embedded Stand-alone Project is selected by default).
      Figure 2-12. 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-13. Device Selection
    5. Select the XC8 2.20 compiler and click Next.
      Figure 2-14. Compiler Selection
    6. Give a name to the project (and the location where to be saved) and click Finish.
      Figure 2-15. 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-16. System Module Configuration
    2. Configure the Sleep options so that Sleep is enabled, and the mode is Standby.
      Info: Go to the Registers page of the System Module and scroll down until reaching SLPCTRL. In the SLPCTRL.CTRLA register, modify the options to be enabled and Standby mode.
      Figure 2-17. Sleep Configuration
    3. 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-18. VREF Configuration
    4. Add the ADC module from device resources and configure it in 12-bit mode, right-adjusted results, 128 results accumulation, 31 ticks for sample length, and to Run-In Standby (RUNSTBY).
      Info: The right-shifted results option must be disabled. The sample length and sample accumulation number are selected in the Hardware Settings tab. The 12-bit mode is set by default. The RUNSTBY bit can be changed from the register view and is found in the CTRLA register.
      Figure 2-19. ADC Configuration
      Figure 2-20. Run in Standby ADC Configuration
    5. Enable the Result Ready Interrupt for the ADC and the Global Interrupts.
      Info: In the ADC0 module, in the Interrupt Setting, activate Result Ready Interrupt Enable. This setting can be observed in the figure for the previous step. In the Interrupt Manager, enable Global Interrupt.
      Figure 2-21. Interrupt Manager Configuration
    6. Press the Generate button.
      Figure 2-22. Generate Button
  3. Add code to the project.
    1. Add the following code to the main.c file:
      #include "mcc_generated_files/mcc.h"
      #include <avr/sleep.h>
      
      uint16_t result;
      
      int main(void)
      {
      
          SYSTEM_Initialize();
          while (1){
              ADC0_StartConversion(ADC_MUXPOS_TEMPSENSE_gc);
              sleep_cpu();
              result = ADC0_GetConversionResult();
          }
      }
      
      Info: SYSTEM_Initialize() is defined in mcc.c, ADC0_StartConversion(channel) and ADC0_GetConversionResult() are defined in adc0.c.
      • SYSTEM_Initialize() is a function that configures the microcontroller and peripherals according to the settings 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_GetConversionResult() returns the result of the last conversion.
      • The program initiates a conversion and then enters Sleep. It is woken up by the Result Ready Interrupt, reads the result and then starts a new one.
    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 readings.
    3. Press the Clean and Build button from the toolbar and verify that the program builds without errors.
      Figure 2-23. Clean and Build