1.5 Creating the Event System 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 1-18. New Project
    3. Click Next (the Microchip Embedded Stand-alone Project is selected by default).
      Figure 1-19. 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-20. Device Selection
    5. Select XC8 2.20 compiler and click Next.
      Figure 1-21. Compiler Selection
    6. Give a name to the project (and the location where to save) and click Finish.
      Figure 1-22. 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-23. 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-24. 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-25. 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). Also enable the STARTEI bit so that a conversion starts when the event is received.
      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 Registers tab and is found in the CTRLA register. The STARTEI bit is found in the EVCTRL register in the Registers tab.
      Figure 1-26. ADC Configuration
      Figure 1-27. RUNSTBY RTC Configuration
      Figure 1-28. Start Conversion on Event 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, 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. Disable the Overflow Interrupt if it is enabled.
      Figure 1-29. RTC Configuration
      Figure 1-30. RUNSTBY RTC Configuration
    6. Add the EVSYS module. Configure channel 0 event generator to be RTC_OVF and the event that will be triggered ADC0START. Configure channel 1 event generator to be ADC0_RESRDY and the event that will be triggered EVSYSEVOUTC.
      Figure 1-31. EVSYS Configuration
      Figure 1-32. EVSYS Configuration
    7. In Pin Manager: Grid View select PC2 as EVOUTC for the EVSYS.
      Figure 1-33. Pin Manager: Grid View
    8. In the Pin Module, enable the interrupt-on-change of the PC2 pin to sense both edges.
      Figure 1-34. Pin Module Configuration
    9. Enable Global Interrupts from Interrupt Manager.
      Figure 1-35. Interrupt Manager Configuration
    10. Press Generate.
      Figure 1-36. Generate Button
  3. Add code to the project.
    1. Add code to the main.c file:
      #include "mcc_generated_files/mcc.h"
      #include <avr/sleep.h>
      
      
      void GPIO_Interrupt(void);
      
      uint16_t volatile result;
      
      int main(void)
      {
      
          SYSTEM_Initialize();
          ADC0.MUXPOS = ADC_MUXPOS_TEMPSENSE_gc;
          PORTC_PC2_SetInterruptHandler(GPIO_Interrupt);
          
          while (1){
             
              sleep_cpu();
      
          }
      }
      
      void GPIO_Interrupt(void)
      {
          result = ADC0_GetConversionResult();
      }
      
      Info: SYSTEM_Initialize() is defined in mcc.c, ADC0_GetConverisonResult() is defined in adc.c, PORTC_PC2_SetInterruptHandler() is defined in pin_manager.c.
      • SYSTEM_Initialize() sets all the configuration registers for the CPU and peripherals. The function is generated by MCC.
      • The register ADC0.MUXPOS is set in main because the Event System starts the conversion immediately and there is no way to give a channel at that time.
      • PORTC_PC2_SetInterruptHandler(GPIO_Interrupt) sets the function that will be called when the interrupt-on-change on pin PC2 is triggered.
      • 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-37. Clean and Build