Skip to content

This code example demonstrates how to generate a 100 Hz sine wave without CPU intervention using the built-in DAC and DMA peripherals of the PIC microcontroller. Some compatible PIC18F-Q43 family of MCUs are: PIC18F27Q43, PIC18F47Q43, PIC18F57Q43.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f57q43-dac-dma-sinewave-generation-mplab-mcc

Repository files navigation

MCHP

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.

Related Documentation

Objective

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.

block_diagram

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.

Software Used

Hardware Used

Hardware Setup

Connection_diagram

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.

Demo Operation

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.

waveform_result

Peripheral Configuration

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

Summary

The DAC and DMA peripherals of the PIC18F57Q43 microcontroller are used to generate a 100Hz frequency sine wave without the intervention of the CPU.

About

This code example demonstrates how to generate a 100 Hz sine wave without CPU intervention using the built-in DAC and DMA peripherals of the PIC microcontroller. Some compatible PIC18F-Q43 family of MCUs are: PIC18F27Q43, PIC18F47Q43, PIC18F57Q43.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published