4.3 ATmega4809 Software Overview

The project is developed using MPLAB X, and the peripherals required were configured with MCC. A new project is created by pressing File>New Project within MPLAB X IDE. The ATmega4809 device must be selected.

The whole project is provided in the GitHub repository. The purpose of this section is to familiarize users with MPLAB Code Configurator, the structure of the generated code, and to explain the usage of every peripheral and its configurations.

  1. System Module Configuration

    This is where the system’s clock is configured. This application uses a 20 MHz oscillator prescaled ten times to 2 MHz.

    Clock Source: Internal Oscillator

    Internal Oscillator Frequency: 20 MHz

    Prescaler Enable: Yes

    Prescaler: 10X
    Figure 4-1. System Module Configuration
  2. Interrupt Manager Configuration

    The peripherals that use the interrupts will be configured further. For now, Global Interrupts need to be enabled.

    Global Interrupt Enable: Yes

    Figure 4-2. Interrupt Manager Configuration

    The peripherals configured next can be added from the Devices Resources tab found under the Project Resources tab.

  3. USART0 Configuration

    This is the USART instance that communicates with the RN4870 module and sends commands to it. The API prefix has been changed and needs to be called exactly that, to work with the code.

    API Prefix: USART_RN4870

    Interrupt Driven: Yes

    RX Buffer Size: 8 bytes

    TX Buffer Size: 8 bytes

    Printf support: Yes

    Mode: Async Mode

    Baud Rate: 115200

    Enable USART Receiver: Yes

    Enable USART Transmitter: Yes

    Parity Mode: No parity

    Stop Bit Mode: 1 stop bit

    Character Size: 8 bit

    The receive interrupt also needs to be enabled in Interrupt Settings.

    Figure 4-3. USART0 Configuration
    Figure 4-4. USART0 Interrupt Settings Configuration
  4. USART3 Configuration

    This USART instance communicates with the embedded debugger, which acts as a serial bridge and sends the information to the USB connected device. The statement about the API prefix also applies here.

    API Prefix: USART_TERMINAL

    Interrupt Driven: Yes

    RX Buffer Size: 8 bytes

    TX Buffer Size: 8 bytes

    Printf support: No

    Mode: Async Mode

    Baud Rate: 115200

    Enable USART Receiver: Yes

    Enable USART Transmitter: Yes

    Parity Mode: No parity

    Stop Bit Mode: 1 stop bit

    Character Size: 8 bit

    Figure 4-5. USART3 Configuration
    Figure 4-6. USART3 Interrupt Settings Configuration
  5. Timer A (TCA0) Configuration

    This timer is used for counting to five seconds, after which the microcontroller reads the sensor values and stores them into the RN4870 module’s characteristics. The timer will be enabled at a later time in the code.

    API Prefix: TIMER_0

    Enable Timer: No

    Clock Selection: System Clock/256

    Timer mode: 16 Bit (Normal)

    Count Direction: Up

    Requested Timeout: 5s

    Enable Overflow Interrupt: Yes

    Waveform Generation Mode: Normal Mode

    Figure 4-7. TCA0 Configuration
  6. Two Wire Interface (TWI0) Configuration

    The TWI or I2C interface is used for communicating with the sensor.

    API Prefix: I2C0

    Interrupt Driven: No

    Enable I2C: Yes

    Mode: Master

    I2C Clock (Hz): 100000

    FM Plus Enable: No

    Figure 4-8. TWI0 Configuration
  7. Weather Click Configuration

    The click libraries can be found at the bottom of Device Resources in the MikroE Clicks drop down and then in the Sensors drop-down menu. The configuration can be changed depending on the application, but this document will present the simplest way to configure it. Setting the Sample Application field to anything else except Custom will ignore the chosen settings in favor of that application’s preset.

    Sensor Mode: Forced

    Standby Time (ms): 10

    IIR Filter Coefficient: OFF

    Oversampling for temperature, pressure and humidity: X1

    Sample Applications: Custom

    Figure 4-9. Weather Click Configuration

    When the Weather Click resource is added, two other modules will be loaded in the Libraries and then in the Foundation Services drop-down menu. They are DELAY and I2CSIMPLE. No changes can be done in DELAY, while the I2CSIMPLE module requires a peripheral instance of TWI as the master. If only TWI0 is enabled, as in the current application, the MCC will choose it by default, but care must be taken when there are several instances of the TWI peripheral.

  8. Pin Module Configuration

    The pins have to be configured similarly to how the hardware is mounted on the adapter. This document will provide the hardware configuration used here and its pin configuration, but it can be changed to suit various needs.

    The RN4870 module is set into the first mikroBUS slot and the Weather click in the second one. This allows the instances of the USART and the TWI to communicate with them with no issues.

    Figure 4-10. Pin Assignments

    Firstly, pin PC6 and PD7 are configured as outputs in the pin module interface. The waveform generation of TCA0 is not used in this project, but the module requires a pin assignment. If they do not interfere with the other functions, it is irrelevant which ones are used. They are set to PE0, PE1, and PE2. The TWI0 is set to PA2 and PA3 to communicate with the Weather click. USART0 communicates with the RN4870 module and is set to PA0 and PA1. USART3 communicates with the debugger and is set to PB0 and PB1.

    Pin module: GPIO Output: PC6, PD7

    TCA0: PE0, PE1, PE2

    TWI0: PA2, PA3

    USART0: PA0, PA1

    USART3: PB0, PB1

    Finally, in the Pin Module in Project Resources, pin PC6 needs to be ‘Weather_CS’ and set as Start High, and PD7 needs to be named ‘RN_Reset_Pin’.

    PC6: Name: ‘Weather_CS’, Start High: Yes

    PD7: Name: ‘RN_Reset_Pin’

    Figure 4-11. Pin Module Configuration