Generate sine waves with 2 mbeds synchronised. Configurable amplitude and phase. Built for 50 Hz mains simulations.

Dependencies:   MODDMA mbed

Description

Small program based on the MODDMA buffered sine wave example.

This programs reads pin 22 to operate in Master (low) or Slave mode. Then configures pin 21 as output (master) or input (slave), in master mode led2 is on. Use a resistor (100 ohm) between the pin21's of master to slave.

The program then calculates a buffer of sine waves for the DMA with parameters given. And starts the DMA and DAC to generate the sine.

On the callbacks of the dma complete (there are 2) slave waits for a sync and master gives a sync, p21. Master waits a few extra clocks to make sure slave is ready.

Commands can be given over Serial port to modify the parameters on the run. Frequency can be changed for master and slave, but it is better to keep the same. Use "f xx.xx". Phase can be changed for master and slave Amplitude can be changed for master and slave.

Hookup

  • Wire p22 high or low.
  • Serial sr(p9,p10) from master to slave.
  • Wire trigger p21 to p21.
  • Output p18 (analogout)

Information

Do not forget a small RC filter on the DAC output.

Master Commands

<master/slave/frequency> <frequency/phase/amplitude> <space> <number> <line feed>

Example commands for serial:

  • master frequency 50.1 hz
    • mf 50.1\n
  • frequency 50.1 Hz
    • f 50.1\n
  • master phase 3.1415 (or 1.0)
    • mp 1\n
  • slave phase 1.5 pi
    • sp 1.5\n

Or use this GUI

https://dl.dropboxusercontent.com/s/uvwsroyu41vzkwg/2013-06-19%2010_35_52-WaveSim.png

Download, or Download C# Code (Visual Studio 2010)

main.cpp

Committer:
jeroen3
Date:
2013-05-30
Revision:
0:46bb6ad79423
Child:
1:b97d61d415ff

File content as of revision 0:46bb6ad79423:

/*
 * Demonstrates sending a buffer repeatedly to the DAC using DMA.
 * Connect an oscilloscope to Mbed pin 18. This example doesn't
 * output anything else (nothing on any serial ports).
 */
#include "mbed.h"
#include "MODDMA.h"
#include <cmath>

Serial pc(USBTX, USBRX);

const double PI   = 3.141592653589793238462;
const float  PI_F = 3.14159265358979f;
 
// Make the buffer size match the number of degrees
// in a circle since we are going to output a sinewave.
#define BUFFER_SIZE 360
 
// Set DAC output power mode.
#define DAC_POWER_MODE  (1 << 16)
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalIn    modesel(p22);
DigitalInOut trigger(p21);

DigitalOut timetest(p23);
 
int buffer[2][BUFFER_SIZE];

typedef enum {MASTER, SLAVE} modesel_t;
modesel_t mode;
 
AnalogOut signal(p18);
 
MODDMA dma;
MODDMA_Config *conf0, *conf1;
 
void TC0_callback(void);
void ERR0_callback(void);
 
void TC1_callback(void);
void ERR1_callback(void);

// Config phase (2*pi is one period)
double phase_master         = PI * 0;
double phase_slave          = PI * 0;
// Config amplitude, x * 3.3 Volt
double amplitude_master =   1;
double amplitude_slave  = 0.5;

void wait_for_sync(){
    switch(mode){
        case MASTER:
            // Wait a few clocks to make sure slave is waiting
            for(uint32_t i=0; i<10; i++){ __NOP(); __NOP(); __NOP(); __NOP(); }
            // Trigger toggle
            LPC_GPIO2->FIOPIN ^= (1<<5);
        break;
        case SLAVE:
            // Wait for pinchange
            {
            uint32_t pinstate = trigger;
            while( pinstate == trigger);
            }
        break;
    }
}
 
void calculate_sines(){
     // Create a sinewave buffer for testing.           
        switch(mode){
        case MASTER:
            for (int i =   0; i <=  360; i++) buffer[0][i] =  amplitude_master * (512 * sin((3.14159/180.0 * i) + phase_master)) + 512;                
        break;
        case SLAVE:
            for (int i =   0; i <=  360; i++) buffer[0][i] =  amplitude_slave * (512 * sin((3.14159/180.0 * i) + phase_slave)) + 512;                
        break;
        }
        
    // Adjust the sinewave buffer for use with DAC hardware.
    for (int i = 0; i < 360; i++) {
        buffer[0][i] = DAC_POWER_MODE | ((buffer[0][i] << 6) & 0xFFC0);
        buffer[1][i] = buffer[0][i]; // Just create a copy of buffer0 to continue sinewave.
    }
}
 
int main() {
    volatile int life_counter = 0;
        pc.baud(115200);
        timetest = 0;
        
        if(modesel == 1){
            pc.printf("slave\r\n");
            trigger.input();
            mode = SLAVE;
            led2 = 0;
        }else{
          pc.printf("master\r\n");
            trigger.output();
            led2 = 1;
            mode = MASTER;
        }
    
    calculate_sines();
    
    // Prepare the GPDMA system for buffer0.
    conf0 = new MODDMA_Config;
    conf0
     ->channelNum    ( MODDMA::Channel_0 )
     ->srcMemAddr    ( (uint32_t) &buffer[0] )
     ->dstMemAddr    ( MODDMA::DAC )
     ->transferSize  ( 360 )
     ->transferType  ( MODDMA::m2p )
     ->dstConn       ( MODDMA::DAC )
     ->attach_tc     ( &TC0_callback )
     ->attach_err    ( &ERR0_callback )     
    ; // config end
    
    
    // Prepare the GPDMA system for buffer1.
    conf1 = new MODDMA_Config;
    conf1
     ->channelNum    ( MODDMA::Channel_1 )
     ->srcMemAddr    ( (uint32_t) &buffer[1] )
     ->dstMemAddr    ( MODDMA::DAC )
     ->transferSize  ( 360 )
     ->transferType  ( MODDMA::m2p )
     ->dstConn       ( MODDMA::DAC )
     ->attach_tc     ( &TC1_callback )
     ->attach_err    ( &ERR1_callback )     
    ; // config end
 
    
    // Calculating the transfer frequency:
    // By default, the Mbed library sets the PCLK_DAC clock value
    // to 24MHz. One complete sinewave cycle in each buffer is 360
    // points long. So, for a 1Hz wave we would need to transfer 360
    // values per second. That would be 24000000/360 which is approx
    // 66,666. But that's no good! The count val is only 16bits in size
    // so bare this in mind. If you need to go slower you will need to
    // alter PCLK_DAC from CCLK/4 to CCLK/8.
    // For our demo we are going to have the sinewave run at 1kHz.
    // That's 24000000/360000 which is approx 66. Experimentation
    // however showed 65 to get closer to 1kHz (on my Mbed and scope 
    // at least).
        const double dacclk = 24000000;
        const double dacper = dacclk / 360;
        double wavefreq = 50;
        double ddacdiv = dacper / (wavefreq);
        unsigned int dacdiv = ddacdiv;
        pc.printf("dacdiv set to: %d\r\n",dacdiv);
    LPC_DAC->DACCNTVAL = dacdiv; // 6500 for 10Hz
 
    // Prepare first configuration.
    if (!dma.Prepare( conf0 )) {
        error("Doh!");
    }
    
        if(mode == MASTER){
            wait(0.1);
        }
        wait_for_sync();
        
    // Begin (enable DMA and counter). Note, don't enable
    // DBLBUF_ENA as we are using DMA double buffering.
    LPC_DAC->DACCTRL |= (3UL << 2);
    
    while (1) {
        // There's not a lot to do as DMA and interrupts are
        // now handling the buffer transfers. So we'll just
        // flash led1 to show the Mbed is alive and kicking.
        if (life_counter++ > 1000000) {
            led1 = !led1; // Show some sort of life.
            life_counter = 0;
        }
    } 
}
 
// Configuration callback on TC
void TC0_callback(void) {

        
    // Just show sending buffer0 complete.
    led3 = !led3; 
    
    // Implement wait for trigger if slave mode
        wait_for_sync();
    
    // Get configuration pointer.
    MODDMA_Config *config = dma.getConfig();
    
    // Finish the DMA cycle by shutting down the channel.
    dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
   
    // Swap to buffer1
    dma.Prepare( conf1 );
 
    // Clear DMA IRQ flags.
    if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); 
}
 
// Configuration callback on Error
void ERR0_callback(void) {
    error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
}
 
// Configuration callback on TC
void TC1_callback(void) {
    
    // Just show sending buffer1 complete.
    led4 = !led4; 
   
    // Implement wait for trigger if slave mode
        wait_for_sync();
     
    // Get configuration pointer.
    MODDMA_Config *config = dma.getConfig();
    
    // Finish the DMA cycle by shutting down the channel.
    dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
    
    // Swap to buffer0
    dma.Prepare( conf0 );
    
    // Clear DMA IRQ flags.
    if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); 
}
 
// Configuration callback on Error
void ERR1_callback(void) {
    error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
}