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

Dependencies:   MODDMA mbed


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.


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


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

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



File content as of revision 3:67b9a01ad7b0:

 * @file    xifo.h
 * @brief   xifo circular buffer
 * @details xifo supplies object oriented circular buffer with 32 bit size elements. \n
 *                     To use either as FIFO (First In First Out) or as FILO (First In Last Out)
 * @author    Jeroen Lodder
 * @date        April 2013
 * @version 2
 * @{
 *    Code has been tested on Cortex M0 (LPC1114)
 *  Performance table, number of core clocks
 *    Measured with a timer before and after
 *    r1 = timer->TC
 *        test_function 
 *  r2 = timer->TC
 *  Function    Speed        Worst Case
 *  write            69            71
 *  read_mr        59      59
 *  read_lr        63      70
 *  pop_mr        76      78
 *    pop_lr        45      45

#ifndef _xifo_H_
#define _xifo_H_

#include <inttypes.h>

#if defined(__arm__)  || defined(__DOXYGEN__)
#pragma anon_unions                /**< Allow unnamed unions */

 * @brief   Circular Buffer object.
 * @details This struct holds the object of a circular buffer
typedef struct  {
/* Pointers: */
        uint32_t *startpool;  /**< @brief First element in pool */
        uint32_t *endpool;        /**< @brief Last element in pool */
        uint32_t *read;                /**< @brief Read pointer */
        uint32_t *write;            /**< @brief Write pointer */
/* Variables: */
        uint32_t full;        /**< @brief Flag indicating buffer is full */
    uint32_t elementcount;/**< @brief Number of elements used */
        uint32_t size;                /**< @brief Size of buffer */
/* Locally used in functions to prevent stack use: */
        /**< @brief union to prevent lvalue typecasting */  
        union {                         
            uint32_t temp;            /**< @brief temp variable and padding for even sized block */
            uint32_t *ptemp;         /**< @brief temp variable and padding for even sized block */

 * @brief   Circular Buffer memory pool type.
typedef unsigned int xifo_pool_t;

#ifdef __cplusplus
extern "C" {
/* xifo Common */
void xifo_init(xifo_t *c, uint32_t size, uint32_t *startpool);
void xifo_clear(xifo_t *c);
uint32_t xifo_write(xifo_t *c, uint32_t data);
/* FIFO use */
uint32_t xifo_read_lr(xifo_t *c, uint32_t index);
uint32_t xifo_pop_lr(xifo_t *c);
/* LIFO use */
uint32_t xifo_read_mr(xifo_t *c, uint32_t index);
uint32_t xifo_pop_mr(xifo_t *c);
/* Extractors */
uint32_t xifo_get_size(xifo_t *c);
uint32_t xifo_get_used(xifo_t *c);
uint32_t xifo_get_full(xifo_t *c);
uint32_t xifo_get_free(xifo_t *c);
#ifdef __cplusplus
#endif //_xifo_H_

/** @} */