Skip to content

This MPLAB® X IDE example demonstrates how to generate a 100Hz sine wave without CPU intervention by configuring the DAC and DMA Drivers within MCC Melody.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/dspic33ck-dac-dma-sinewave

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microchip Logo.

Sine Wave Demo

Overall Project

Summary

This code example demonstrates how to generate a 100 Hz sine wave without CPU intervention using the DMA and DAC peripherals of the dsPIC33CK device. This demo is interchangeable between the Explorer 16/32 Board with dsPIC33CK256MP508 PIM and the dsPIC33CK Curiosity Development Board (with varying output pins).

The DMA peripheral will utilize the Capture/Compare event generated by the SCCP module to iterate and place values from a 256 point sine lookup table to a DAC peripheral for output.

Diagram

Related Documentation

DMA Driver

DAC Driver

PWM Driver (For running the SCCP module in capture/compare mode)

Software Used

Hardware Used

Setup

The sine wave demo uses the Explorer 16/32 Development board or the dsPIC33CK Curiosity Development board. The boards are plugged in for programming through PKOB(PICkit On-Board) or the PicKit 4.

The setup for each board is slightly different:

  • Explorer 16/32 Development Board
    • dsPIC33CK256MP508 PIM inserted into the designated slot
    • The Sine Wave signal (DACOUT) can be read in on P25
  • dsPIC33CK Curiosity Development board
    • The Sine Wave signal (DACOUT) can be read in on RA3

Software Setup

Peripheral Configurations

Omitted configurations are default values set by MCC Melody.

1. DMA Melody Driver's DMA Channel 0 Settings:

Setting Value Explanation
Enable Channel False Starts with channel disabled since it needs to be enabled after SCCP1 in PWM mode is initialized in the main.c.
Enable Reload True The registers are reloaded to previous values at the next start.
Transfer Mode Repeated One-Shot This allows the DMA to iterate and transfer data one value per event until it reaches the transfer count.
Trigger Sources SCCP1 Timer/OC/IC Event This checks for an of the SCCP1 capture/compare event. (Triggered by running SCCP1 in PWM mode)
Source Address Mode Incremented Increments the Source Address value by the Data Size
Source Address 0 This is set within the application code to use the address of the first sine wave point.
Destination Address Mode Unchanged This means the destination address will not be affected by the DMA iteration.
Destination Address 0 This is set within the application code.
Data Size 16 bit Sets that transaction size.
Transfer Count 0x100 The number of times to iterate in one-shot. (256 points)
Interrupt Driven True Enables the DMA to be interrupt driven.

DMA Configurations

2. DAC Melody Driver

Setting Value Explanation
DAC output Enable True This enables the DACOUT pin.

DAC Configurations

3. PWM Melody Driver

  • The SCCP1 module is being used in PWM mode to generate an capture/compare event. No pin will be configured as output.
Setting Value Explanation
Module Enable True Enables the PWM module.
Requested Frequency (Hz) 25600 This value is calculated based on the number of points to iterate multiplied by the expected output frequency. *Continued explanation in the "PWM Melody driver's requested frequency explanation" below.
Sync/Trigger Sync This is set since PWM is not triggered by any external interrupt.
Interrupt Driven Disabled This is disabled so the ISR does not impact the DMA functionality.
PWM PLIB Selector SCCP1 This generates a capture/compare event being used by the DMA driver.

PWM Configurations

3. System Clock Settings

  • The Clock settings are configured to the highest possible speed to allow the PWM to work at higher speeds.
Setting Value Explanation
System Clock Source FRC Oscillator with PLL FRC is the hardware clock and PLL is required to enable the maximum frequency below.
Set System(FOSC) Frequency to Maximum Enabled This enables the maximum frequency.

Clock Settings

Programming the Device

  1. Open the dspic33ck_dac_dma_sinewave.X project in MPLAB® X IDE.
  2. Right click the project in "Projects" tab of MPLAB® X IDE and select "Set as Main Project".
  3. Plug in the PKOB (PICkit On-Board) or the PICkit 4 from the board to the PC.
  4. Program the device by selecting the "Make and Program Device (Project dspic33ck_dac_dma_sinewave)" button on the menu bar.
  5. The project should build and program successfully.

Operation

After programming the device the output can be read off of P25 on the Explorer Board:

Explorer Board Pin

After programming the device the output can be read off of RA3 on the Curiosity Board:

Curiosity Board Pin

A 100 Hz sine wave will be present:

Sine Wave reading on an oscilloscope

PWM Melody driver's requested frequency explanation

The PWM Melody driver (not hardware peripheral) is being used to trigger the SCCP1 Output Compare event only.

With the DMA module configured to use the "SCCP1 Timer/OC/IC Event", only the Output Compare/Input Capture event will trigger the DMA to run not the Timer callback event as the description may imply. *The selections description may be updated for newer versions of the DMA driver.

The SCCP1 capture/compare event (being used as the DMA trigger to update the DAC value) occurs on every falling edge of the PWM. The frequency we select in the PWM driver in Hz represents the number of SCCP1 events per second. Since the sine lookup table is defined, the SCCP1 event needs to occur 256 times per 1 Hz of the expected output to represent the Correct period. This boils down to a multiplication problem:

PWM Frequency (Hz) = Points Length (Sine wave table Length) * Required Output Frequency (Hz)

For example, if there is a need to lower the frequency of the sine wave output to 10 Hz with a 256 point sine wave lookup table updating the PWM to use a frequency of 2560 Hz should represent that output.

There is however a hardware limit for the PWM requested frequency and the SCCP's interaction with the DMA driver

When configuring the requested frequency for the PWM there is the calculated frequency listed in Melody. This is done by using the formulas defined by the dsPIC33CK256MP508's datasheet to calculate the period register's values:

Period Register

For Higher requested frequencies the accuracy of the period is lower. The output compare buffer register is also affected by this due to its formulas:

Output Compare Data Buffer

In testing here are the following values for the Min and Max frequencies for PWM to trigger output compare events for sine wave outputs:

Requested PWM Frequency Sine wave output frequency
25.6 Hz (Near Minimum requested Frequency) 0.1 Hz
256 Hz 1 Hz
2560 Hz 10 Hz
25600 Hz 100 Hz
256000 Hz 1 kHz
2560000 Hz 9.8 kHz
25600000 Hz 78 kHz
100000000 Hz (Maximum Requested Frequency) 97 kHz

About

This MPLAB® X IDE example demonstrates how to generate a 100Hz sine wave without CPU intervention by configuring the DAC and DMA Drivers within MCC Melody.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published