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.
- Create a new MPLAB X project for
AVR128DA48.
- Open MPLAB X.
- Select or the New Project button.
- Click Next (the Microchip Embedded Stand-alone Project is selected by default).
- 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.
- Select XC8 2.20 compiler and click Next.
- Give a name to the project (and the location where to save) and click Finish.
- Open MPLAB Code Configurator (MCC)
and configure the peripherals.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- In Pin Manager: Grid View select PC2 as EVOUTC for the EVSYS.
- In the Pin Module, enable the interrupt-on-change of the PC2 pin to sense both edges.
- Enable Global Interrupts from Interrupt Manager.
- Press Generate.
- In System Module, choose the clock
source as the internal oscillator with 24 MHz clock.
- Add code to the
project.
- 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 inmcc.c
,ADC0_GetConverisonResult()
is defined inadc.c
,PORTC_PC2_SetInterruptHandler()
is defined inpin_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.
- 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 readings. - Press the Clean and Build button from the toolbar and verify that the program builds without errors.
- Add code to the