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.
- Create a new MPLAB X project for
AVR128DA48.
- Open MPLAB X.
- Select File → New Project or the New Project button.
- Click Next (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 the XC8 2.20 compiler and click Next.
- Give a name to the project (and the location where to be saved) and click Finish.
- Open MPLAB Code Configurator (MCC)
and configure the peripherals.
- 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.
- 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.
- 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,
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.
- 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.
- Press the Generate button.
- Configure the System Module
to run on the internal oscillator at 24 MHz.
- Add code to the project.
- 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 inmcc.c
,ADC0_StartConversion(channel)
andADC0_GetConversionResult()
are defined inadc0.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.
-
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. - Press the Clean and Build button from the toolbar and verify that the program builds without errors.
- Add the following code to the