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.
- 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 be saved) 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).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.
- 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.
- 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).
- Press the Generate button.
- In System Module, choose the
clock source as the internal oscillator with 24 MHz clock.
- Add code to the generated files.
- 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 inmcc.c
,RTC_SetOVFIsrCallback()
is defined inrtc.c
andADC0_StartConversion()
andADC0_GetConversionResult()
are defined inadc.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 theTIMER_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.
- 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 the following code to the