Sine Wave Generation Using Digital-to-Analog Converter (DAC) and Direct Memory Access (DMA) of PIC18F57Q43 Microcontroller
This code example demonstrates how to generate a 100 Hz sine wave without CPU intervention using the built-in DAC and DMA peripherals of PIC® microcontrollers. Some compatible PIC18F-Q43 family of MCUs are: PIC18F27Q43, PIC18F47Q43, PIC18F57Q43.
- PIC18F57Q43 Data Sheet
- 8-bit PIC MCU Peripheral Deep Dive (DAC Chapter)
This application example highlights the usage of 8-bit buffered DAC and DMA peripherals of the PIC18F57Q43 microcontroller to generate a sinusoidal waveform. This device has a DMA module which can be used for seamless and direct data transfers between different memory regions without intervention from the CPU. The signal is generated by continuously adjusting the output of a DAC to create an analog signal made of a series of discrete steps. The DAC values are loaded from a Look-up Table (LUT) stored in Program Flash Memory (PFM) using DMA.
In this application, the DAC is configured to use the Fixed Voltage Reference (FVR) as the positive voltage reference and ground as the negative reference. The frequency of the sine wave is decided by how frequently the DAC DATA register is updated and the total number of samples in one signal cycle. The number of data samples per cycle of the sine wave is set at 250 and is stored in the LUT present in the PFM. This data is transferred to the DAC data register using the DMA triggered by the Timer 2 overflow interrupt occurring every 40 µs.
Therefore, the signal frequency is:
f = 1 / (Total no of samples per cycle * Timer 2 period) = 1/ (250 * 40µS) = 100 Hz
.
If the number of DAC samples in the sine wave is constant, the signal frequency is changed by changing the period of Timer 2. The DAC output is routed to pin RA2 and can be observed using the output of an oscilloscope.
- MPLAB® X IDE v6.20.0
- XC8 Compiler v3.0.0
- MPLAB® Code Configurator (MCC) v5.7.1
- Microchip PIC18F-Q series Device Support (PIC18F-Q_DFP v1.26.442)
- PIC18F57Q43 Curiosity Nano Evaluation Kit
- Logic Analyzer/Oscilloscope
This demo is implemented using the PIC18F57Q43 Curiosity Nano Evaluation Kit. The sine wave generated on pin RA2 is observed on a logic analyzer/oscilloscope. Connect pin RA2 to any analog channel of a logic analyzer. Connect the GND pin of the Curiosity Nano board to the ground pin of the logic analyzer/oscilloscope.
After the hardware connections are complete and the firmware is programmed, the waveforms can be observed. The logic analyzer/oscilloscope is configured to measure frequency/period. A sine wave of 100 Hz frequency is generated using the DAC and DMA.
This section explains how to configure the peripherals using the MPLAB® X IDE with MCC plugin for recreation of the project.
Refer to the Software Used section to install required tools to recreate the project.
Additional Links: MCC Melody Technical Reference
Peripherals | Configuration | Usage |
---|---|---|
Clock Control | Clock Control: Clock source - HFINTOSC HF Internal Clock - 16MHz Clock Divider – 1 |
16 MHz System clock |
TMR2 | Enable Timer Control Mode - Roll over pulse Start/Reset Option - Software Control Clock Source - Fosc/4 Polarity - Rising edge Prescaler - 1:32 Postscaler - 1:1 Time Period - 40µs TMR Interrupt Enabled |
Used to hardware trigger DMA |
DAC1 | Enable DAC DAC Positive reference selection - FVR DAC Negative reference selection - VSS DAC Output Enable Selection - DACOUT1 Enabled and DACOUT2 Disabled |
Used to generate 100Hz sine wave |
DMA | DMA: DMA Dependency - DMA1 Source Address - 0x000000 Source Message Size - 250 Destination Region - SFR Destination Module - DAC1 Destination SFR - DAC1DATL Destination Message Size – 1 DMA1: DMA Enabled Source mode - Incremented Source region - Program Flash Destination mode - Unchanged Abort Trigger - None Start Trigger - TMR2 |
Used for data transfer between LUT in PFM and DAC DATA |
FVR | FVR Enabled FVR_buffer 2 - 2x |
Positive reference voltage to DAC |
The DAC and DMA peripherals of the PIC18F57Q43 microcontroller are used to generate a 100Hz frequency sine wave without the intervention of the CPU.