Mischa Megens / DACDMAfuncgenlib

Dependencies:   DMAFuncGen MODDMA mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Demonstrates sending a buffer repeatedly to the DAC using DMA.
00003  * Connect an oscilloscope to Mbed pin 18.
00004  */
00005 
00006 #include "mbed.h"
00007 #include "DMAFuncGen.h"
00008 
00009 AnalogIn ain(p17);
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut led3(LED3);
00013 DigitalOut led4(LED4);
00014 
00015 MODDMA dma;
00016 DMAFuncGen fg(dma, MODDMA::Channel_0);
00017 
00018 int main() {
00019     wait(0.5);
00020    
00021     // Create waveform.
00022     fg.buffer_size=400;
00023     fg.buffer = new uint32_t[fg.buffer_size];
00024     const float PI = 3.1415927;
00025     for (int i=0; i<fg.buffer_size; i++) {
00026         int x = (511*sin(2*PI*i/fg.buffer_size)) +512;
00027         fg.set(i,x << 6);
00028     }
00029     
00030     fg.Connect();
00031     fg.Setup();
00032     fg.SetFrequency(0.5);
00033     printf("Frequency: %f\r\n",fg.Frequency());
00034     fg.Start();
00035     
00036     // Simple oscilloscope, using the LEDs -
00037     // Connect pin 18 (DAC) to p17 (ADC) for it to work.
00038     float z;
00039     while (1){
00040         z=ain.read();
00041         led1 = (z > 0.2) ? 1 : 0;
00042         led2 = (z > 0.4) ? 1 : 0;
00043         led3 = (z > 0.6) ? 1 : 0;
00044         led4 = (z > 0.8) ? 1 : 0;
00045     }
00046 }