This MPLAB® X project demonstrates FreeRTOS with two tasks for monitoring and displaying Passive Infrared (PIR) sensor output. The first task, prvQueueReadADCTask
, starts a conversion for the sensor's analog output voltage using the ADC after a 2-second delay and then enters a Suspended state. It is resumed by an ADC interrupt when motion is detected, that means the output voltage is outside the set window thresholds. Upon resumption, it triggers the second task, prvQueuePrintMessageTask
, to print a message to the serial terminal, providing system feedback. The project highlights FreeRTOS' multitasking efficiency and real-time capabilities by managing motion detection and serial communication through task suspension and resumption.
More details and code examples on the AVR128DA48 can be found at the following links:
- FreeRTOS™
- FreeRTOS Demo for the Microchip AVR Dx Port
- AVR128DA48 Product Page
- AVR128DA48 Code Examples on GitHub
- MPLAB X IDE v6.20 or newer
- MPLAB® XC8 v2.50 or a newer compiler
- AVR-Dx v2.6.303 or newer Device Pack
-
The AVR128DA48 Curiosity Nano Development board (DM164151) is used as test platform:
-
PIR Click MikroE board:
The following peripheral and clock configurations are set up using the MPLAB® Code Configurator (MCC) Melody for the AVR128DA48:
- Clock Control:
- FreeRTOS Library:
- USART1:
- Enable USART Receiver: Enabled
- Enable USART Transmitter: Enabled
- ADC:
- Clock Prescaler: CLK_PER divided by 256
- Sample Accumulation Number: 16 results accumulated
- Sample Length (# of ADC Clock): 2
- Enable ADC: Enabled
- Resolution Selection: 12-bit mode
- Left Adjust Result: Enabled
- Free Running Mode: Enabled
- Positive Channel Input Selection: ADC input pin 4
- Window Comparator Interrupt: Enabled
- Window Comparator Mode: Outside Window
- Window Comparator High Threshold(V): 30.4 (Number of Accumulated Samples * High Threshold Voltage Reference = 16 * 1.9V = 30.4 V)
- Window Comparator Low Threshold(V): 0.08 (Number of Accumulated Samples * Low Threshold Voltage Reference = 16 * 0.005V = 0.08 V)
- VREF:
- VDD (V): 3.3
- ADC Voltage Reference Source: VDD as reference
- Pin Grid View:
NOTE: The freertos_interface.c
source file must be manually configured. More details and the explained code can be found in the interface source file.
To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
In the demo section, the GIF below visually demonstrates the real-time monitoring of the PIR sensor's output voltage, which is continuously updated using the ADC. Simultaneously, the serial terminal displays the corresponding messages, confirming the system's operational status. The smooth interaction between tasks highlights the efficiency of FreeRTOS in handling multiple processes concurrently.
Also, a screenshot provides a static snapshot of the system in action for quick reference.
This FreeRTOS project involves two tasks running in parallel: one reads the output voltage of a PIR sensor using the ADC, and the other prints status messages to a serial terminal. The project demonstrates the capability of FreeRTOS in managing real-time tasks, showcasing efficient multitasking, with an 8-bit AVR device. It highlights the seamless interaction between sensor data acquisition and serial communication, making it ideal for embedded system applications.
This chapter demonstrates how to use the MPLAB X IDE to program an AVR device with an Example_Project.X. This is applicable to other projects.
-
Connect the board to the PC.
-
Open the Example_Project.X project in MPLAB X IDE.
-
Set the Example_Project.X project as main project.
Right click the project in the Projects tab and click Set as Main Project. -
Clean and build the Example_Project.X project.
Right click the Example_Project.X project and select Clean and Build. -
Select AVRxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
Right click the project and click Properties.
Click the arrow under the Connected Hardware Tool.
Select AVRxxxxx Curiosity Nano (click the SN), click Apply and then click OK: -
Program the project to the board.
Right click the project and click Make and Program Device.