1.4 Creating the Software Implementation Program

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 1-3. New Project
    3. Click Next (the Microchip Embedded Stand-alone Project is selected by default).
      Figure 1-4. 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 1-5. Device Selection
    5. Select XC8 2.20 compiler and click Next.
      Figure 1-6. Compiler Selection
    6. Give a name to the project (and the location where to be saved) and click Finish.
      Figure 1-7. Project Name
  2. Open MPLAB Code Configurator (MCC) and configure the peripherals.
    1. In System Module, choose the clock source as the internal oscillator with 24 MHz clock.
      Info: Choose the Clock source as Internal Oscillator, the Oscillator Frequency Options as the 24 MHz system clock, and disable the Prescaler.
      Figure 1-8. System Module Configuration
    2. In the Registers tab of System Module, enable Sleep and set the mode to Standby.
      Info: Go to the Registers tab in System Module and scroll down to SLPCTRL. Modify the CTRLA register to enable Sleep and select the mode.
      Figure 1-9. 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 1-10. VREF Configuration
    4. Add the ADC module from device resources and configure it in 12-bit mode, right-adjusted results, no accumulation, one tick for sample length, and to Run-In Standby (RUNSTBY).
      Info: 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 Registers tab and it is found in the CTRLA register.
      Figure 1-11. ADC Configuration
      Figure 1-12. RUNSTBY ADC Configuration
    5. Add the RTC module from device resources and configure it to use the internal 32.768 kHz oscillator, have a period of one second, a prescaling factor 1, and Run-In Standby (RUNSTBY) activated.
      Info: The clock, prescaling factor, and period are set from the Hardware Settings tab, while the RUNSTBY bit is found in the register pane in the CTRLA register.
      Figure 1-13. RTC Configuration
      Figure 1-14. RUNSTBY RTC Configuration
    6. Enable the Global Interrupts and enable the Result Ready Interrupt for the ADC and the overflow interrupt for the RTC.
      Info: The Global Interrupts are enabled from the Interrupt Manager, the ADC interrupt is enabled from the Interrupt Settings tab of the ADC module, and the RTC interrupt is enabled from the Interrupt Settings tab of the RTC module (seen in the pictures for the previous steps).
      Figure 1-15. Interrupt Manager Configuration
    7. Press the Generate button.
      Figure 1-16. Generate Button
  3. Add code to the generated files.
    1. Add the following code to the main.c file.
      #include <avr/io.h>
      #include <avr/sleep.h>
      #include "mcc_generated_files/mcc.h"
      
      void TIMER_interrupt(void);
      
      uint16_t result;
      
      int main(void)
      {
      
          SYSTEM_Initialize();
          RTC_SetOVFIsrCallback(TIMER_interrupt);
      
          while (1){
              sleep_cpu();
              result = ADC0_GetConversionResult();
          }
      }
      
      void TIMER_interrupt()
      {
          ADC0_StartConversion(ADC_MUXPOS_TEMPSENSE_gc);
      }
      
      Info: SYSTEM_Initialize() is defined in mcc.c, RTC_SetOVFIsrCallback() is defined in rtc.c and ADC0_StartConversion() and ADC0_GetConversionResult() are defined in adc.c.
      • SYSTEM_Initialize() sets all the configuration registers for the CPU and peripherals. The function is generated by MCC.
      • RTC_SetOVFIsrCallback() is a function that sets the callback (the function that will be called when the interrupt is triggered) to the TIMER_interrupt() functioned defined in the main.
      • The TIMER_interrupt() function starts the conversion for the ADC.
      • The ADC0_StartConversion() function starts the conversion on the temperature channel of the ADC.
      • The ADC0_GetConversionResult() returns the last result of the conversion.
    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 1-17. Clean and Build