In this example, the ADCC will be configured to sample the Temperature Indicator Module in order to measure the device temperature of a PIC18F16Q41 microcontroller.
- MPLAB® X IDE v6.20.0 or newer
- MPLAB XC8 v3.0.0 or newer compiler
- MPLAB Code Configurator (MCC)
- Melody Library v5.1.4 or newer
- MPLAB Data Visualizer Plugin or other serial terminal
Step #1: Creating the Project
- On the tool bar, click New Project
- Microchip Embedded; Standalone Project
- Enter the Device
- For this Project: PIC18F16Q41
- Enter a name for this project, such as adcc-temp-sensor
- Name: “adcc-temp-sensor”
- Note: The project name cannot have any empty spaces
Step #2: MPLAB Code Configurator (MCC)
- Modify the Clock Control
- Set “Clock Source” to High Frequency Internal Oscillator (HFINTOSC)
- Set “HF Internal Clock” to 64_MHz
- Set “Clock Divider” to 1
- Set Configuration Bits
- Disable "External Oscillator Mode Selection" in CONFIG 1
- Set "Power-up Default Value for COSC" in CONFIG 1 to "HFINTOSC with HFFRQ = 64MHz and CDIV = 1:1"
- Ensure that "WDT Operating Mode" in CONFIG 5 is set to "WDT Disabled; SWDTEN is ignored"
Step #3: Adding ADCC, FVR, Memory, Timer, and UART Peripherals
For this project to work properly, the application goes through a specific series of steps to minimize noise in the measurement.
These steps are:
- Analog-to-Digital Converter with Computation (ADCC) is set up to run periodically from a hardware trigger
- The device is put to sleep
- The hardware trigger activates
- The temperature sensor is measured multiple times and averaged
- The microcontroller is woken up after the average is computed
- The result is converted to temperature
- The results are printed using UART
- In Device Resources:
- Drivers>ADCC>ADCC
- Drivers>FVR>FVR
- Drivers>Memory>MEMORY
- Drivers>Timer>TMR2
- Drivers>UART>UART1
Once the peripherals are added, change to following settings:
Please refer to the Temperature Indicator Module on the PIC18F16Q41 data sheet when configuring these peripherals.
- TMR2
- Hardware Settings
- Control Mode: Roll over pulse
- Start/Reset Option: Software control
- Timer Clock
- Clock Source: LFINTOSC
- Prescaler: 1:8
- Postscaler: 1:12
- Timer Period(s)
- Time Period (s): 0.5
- Hardware Settings
- FVR
- Custom Name: FVR
- Enable FVR: Enabled
- FVR_buffer 1 Gain (to ADC): 2x
- Enable Temperature Sensor: Enabled
- Voltage Range Selection: Hi_Range
- ADCC
- Hardware Settings
- Operating Mode: Burst Average
- Result Alignment: right
- Positive Reference: FVR
- Auto-conversion Trigger: TMR2
- Acquisition Count: 8191
- ADC Clock
- Clock Source: ADCRC
- Computation Feature
- Error Calculation Mode: First derivative of Single measurement
- Threshold Interrupt Mode: enabled
- Repeat: 8
- Accumulator Right Shift: 3
- Interrupt settings
- ADC Threshold is enabled
- Hardware Settings
- UART1
- Software Settings:
- Enable "Redirect STDIO to UART" in order to use the function (printf) for sending messages
- Hardware Settings:
- Enable UART box must be checked
- Enable transmit and Receive must be checked
- Set the Baud Rate to 19200
- Everything else can be left as default settings
- Software Settings:
- In order for the temperature calculation to be made, gain and offset need to be read from the Device Information Area (DIA). By configuring the memory peripheral, this allows access to read those values.
- Memory
- Custom Name: FLASH
- Add Data EE Routines: Enabled
Step #4: Configure the Pins
- TX1 is connected to pin RB7
- RX1 is connected to pin RB5
Step #5: Generate the Project
- Click the generate button in MCC to create the appropriate header and source files for this configuration
Step #6: Modifying main.c
- Once the project is generated, the new MCC generated header and source files will be in the project window. Select the
main.c
file and you will see an empty while(1) loop where you can add your application code. - Refer to section 39.2 on the PIC18F16Q41 data sheet to fully understand the temperature calculation
#include "mcc_generated_files/system/system.h"
#define SetAcquisitionChannel(X) do { ADPCH = X; } while (0)
//Sets sampling channel of ADCC without starting conversion
int main(void)
{
SYSTEM_Initialize();
int16_t gain;
int16_t offset;
gain = FLASH_ReadWord(DIA_TSHR1);
offset = FLASH_ReadWord(DIA_TSHR3);
INTERRUPT_GlobalInterruptEnable();
uint16_t ADC_MEAS = 0;
int24_t temp_c = 0;
SetAcquisitionChannel(channel_Temp);
while(1)
{
asm ("SLEEP");
asm ("NOP");
ADC_MEAS = ADCC_GetConversionResult();
temp_c = (int24_t) (ADC_MEAS) * gain;
temp_c = temp_c / 256;
temp_c = temp_c + offset;
temp_c = temp_c / 10;
printf("Device Temperature: %dC \r\n", temp_c);
}
}
- Make and Program the Device
Step #7: MPLAB Data Visualizer
-
Open up the Data Visualizer on the host computer and select the COM port associated with the Curiosity Nano
-
Ensure that the Terminal tab is selected
- Once selected, configure the data visualizer to communicate at: 19200 baud, no parity, and 1 stop bit
- If everything is set up correctly, then the Data Visualizer will start displaying the device temperature every half a second
- If you cover or touch the DIE on the device, you will notice the temperature start to increase
This application demonstrates how to set up an ADCC and utilize the Temperature Indicator Module to measure the device temperature.