GithubHelp home page GithubHelp logo

microchip-pic-avr-examples / dspic33ck-dac-dma-sinewave Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 17.32 MB

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: Other

Makefile 2.66% C 94.33% Assembly 3.00%
ccp dac dma signal-generation

dspic33ck-dac-dma-sinewave's Introduction

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

dspic33ck-dac-dma-sinewave's People

Contributors

harishagari avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.