How can i get 2 sin waves on the Due at same time one out of phase with other

Im using this tutorial as a guide -> http://arduino.cc/en/Tutorial/DueSimpleWaveformGenerator#.Uxi4qvldVWw
I can get a single wave form to generate on the DAC1 but no matter what I try I can't get anything on DAC0.

Can anyone advice me as to what I must do to generate 2 sine waves, one out of phase with the other at the same time on the Due using both DAC0 and DAC1

So how about showing us the code you have?

void setup() {
analogWriteResolution(12);
}

void loop() {
for(int i=0; i<360; i++) {
analogWrite(DAC0, sinTable*);*
int k=i+180;
if(k>= 360) k=k-360;
analogWrite(DAC1, sinTable[k]);
delay(1);
}
}
I got this off some other post in this forum. The main problem with this is as follows ->
When I try to use the code from the poster that I liked to, I can get a decent output from DAC1, however I can't get any output form DAC0. Even when I edit out DAC1, DAC0 doesn't work. I tried the sample blink program and had DAC1 as the output instead of pin 13 and it worked flawlessly. When I tried it with DAC0, it continued to work flawlessly. However when I try out this program it refuses to work.
My project is due in a month and this first step is already driving me up the wall.

That's not the code you have, it doesn't even compile

sketch_mar08a.ino: In function 'void loop()':
sketch_mar08a:7: error: 'sinTable' was not declared in this scope

Here is the whole code

const uint16_t sinTable[] = {
0x7ff, 0x823, 0x846, 0x86a, 0x88e, 0x8b1, 0x8d5, 0x8f9, 0x91c, 0x93f,
0x963, 0x986, 0x9a9, 0x9cc, 0x9ee, 0xa11, 0xa33, 0xa56, 0xa78, 0xa9a,
0xabb, 0xadd, 0xafe, 0xb1f, 0xb40, 0xb60, 0xb81, 0xba1, 0xbc0, 0xbe0,
0xbff, 0xc1e, 0xc3c, 0xc5a, 0xc78, 0xc95, 0xcb2, 0xccf, 0xcec, 0xd08,
0xd23, 0xd3e, 0xd59, 0xd73, 0xd8d, 0xda7, 0xdc0, 0xdd8, 0xdf1, 0xe08,
0xe1f, 0xe36, 0xe4c, 0xe62, 0xe77, 0xe8c, 0xea0, 0xeb4, 0xec7, 0xeda,
0xeec, 0xefe, 0xf0f, 0xf1f, 0xf2f, 0xf3f, 0xf4d, 0xf5c, 0xf69, 0xf77,
0xf83, 0xf8f, 0xf9a, 0xfa5, 0xfaf, 0xfb9, 0xfc2, 0xfca, 0xfd2, 0xfd9,
0xfdf, 0xfe5, 0xfeb, 0xfef, 0xff3, 0xff7, 0xffa, 0xffc, 0xffd, 0xffe,
0xfff, 0xffe, 0xffd, 0xffc, 0xffa, 0xff7, 0xff3, 0xfef, 0xfeb, 0xfe5,
0xfdf, 0xfd9, 0xfd2, 0xfca, 0xfc2, 0xfb9, 0xfaf, 0xfa5, 0xf9a, 0xf8f,
0xf83, 0xf77, 0xf69, 0xf5c, 0xf4d, 0xf3f, 0xf2f, 0xf1f, 0xf0f, 0xefe,
0xeec, 0xeda, 0xec7, 0xeb4, 0xea0, 0xe8c, 0xe77, 0xe62, 0xe4c, 0xe36,
0xe1f, 0xe08, 0xdf1, 0xdd8, 0xdc0, 0xda7, 0xd8d, 0xd73, 0xd59, 0xd3e,
0xd23, 0xd08, 0xcec, 0xccf, 0xcb2, 0xc95, 0xc78, 0xc5a, 0xc3c, 0xc1e,
0xbff, 0xbe0, 0xbc0, 0xba1, 0xb81, 0xb60, 0xb40, 0xb1f, 0xafe, 0xadd,
0xabb, 0xa9a, 0xa78, 0xa56, 0xa33, 0xa11, 0x9ee, 0x9cc, 0x9a9, 0x986,
0x963, 0x93f, 0x91c, 0x8f9, 0x8d5, 0x8b1, 0x88e, 0x86a, 0x846, 0x823,
0x7ff, 0x7db, 0x7b8, 0x794, 0x770, 0x74d, 0x729, 0x705, 0x6e2, 0x6bf,
0x69b, 0x678, 0x655, 0x632, 0x610, 0x5ed, 0x5cb, 0x5a8, 0x586, 0x564,
0x543, 0x521, 0x500, 0x4df, 0x4be, 0x49e, 0x47d, 0x45d, 0x43e, 0x41e,
0x3ff, 0x3e0, 0x3c2, 0x3a4, 0x386, 0x369, 0x34c, 0x32f, 0x312, 0x2f6,
0x2db, 0x2c0, 0x2a5, 0x28b, 0x271, 0x257, 0x23e, 0x226, 0x20d, 0x1f6,
0x1df, 0x1c8, 0x1b2, 0x19c, 0x187, 0x172, 0x15e, 0x14a, 0x137, 0x124,
0x112, 0x100, 0xef, 0xdf, 0xcf, 0xbf, 0xb1, 0xa2, 0x95, 0x87,
0x7b, 0x6f, 0x64, 0x59, 0x4f, 0x45, 0x3c, 0x34, 0x2c, 0x25,
0x1f, 0x19, 0x13, 0xf, 0xb, 0x7, 0x4, 0x2, 0x1, 0x0,
0x0, 0x0, 0x1, 0x2, 0x4, 0x7, 0xb, 0xf, 0x13, 0x19,
0x1f, 0x25, 0x2c, 0x34, 0x3c, 0x45, 0x4f, 0x59, 0x64, 0x6f,
0x7b, 0x87, 0x95, 0xa2, 0xb1, 0xbf, 0xcf, 0xdf, 0xef, 0x100,
0x112, 0x124, 0x137, 0x14a, 0x15e, 0x172, 0x187, 0x19c, 0x1b2, 0x1c8,
0x1df, 0x1f6, 0x20d, 0x226, 0x23e, 0x257, 0x271, 0x28b, 0x2a5, 0x2c0,
0x2db, 0x2f6, 0x312, 0x32f, 0x34c, 0x369, 0x386, 0x3a4, 0x3c2, 0x3e0,
0x3ff, 0x41e, 0x43e, 0x45d, 0x47d, 0x49e, 0x4be, 0x4df, 0x500, 0x521,
0x543, 0x564, 0x586, 0x5a8, 0x5cb, 0x5ed, 0x610, 0x632, 0x655, 0x678,
0x69b, 0x6bf, 0x6e2, 0x705, 0x729, 0x74d, 0x770, 0x794, 0x7b8, 0x7db,
};

void setup() {
analogWriteResolution(12);
}

void loop() {
for(int i=0; i<360; i++) {
analogWrite(DAC0, sinTable*);*
int k=i+180;
if(k>= 360) k=k-360;
analogWrite(DAC1, sinTable[k]);
delay(1);
}
}
I didn';t put the first part as it was too big

Nope, try again:

sketch_mar08a.ino: In function 'void loop()':
sketch_mar08a:47: error: invalid conversion from 'const uint16_t*' to 'uint32_t'
sketch_mar08a:47: error: initializing argument 2 of 'void analogWrite(uint32_t, uint32_t)'

Did you not wonder why the code turned to italics after "analogWrite(DAC0, sinTable"?


Rob

analogWrite(DAC0, sinTable[i]);
int k=i+180;
if(k>= 360) k=k-360;
analogWrite(DAC1, sinTable[k]);

remove the astrisk

shareefm:
analogWrite(DAC0, sinTable[i]);
int k=i+180;
if(k>= 360) k=k-360;
analogWrite(DAC1, sinTable[k]);

remove the astrisk

No.... you read the how to use the forum sticky post, and you post the code correctly.

const uint16_t sinTable[] = {
0x7ff, 0x823, 0x846, 0x86a, 0x88e, 0x8b1, 0x8d5, 0x8f9, 0x91c, 0x93f,
0x963, 0x986, 0x9a9, 0x9cc, 0x9ee, 0xa11, 0xa33, 0xa56, 0xa78, 0xa9a,
0xabb, 0xadd, 0xafe, 0xb1f, 0xb40, 0xb60, 0xb81, 0xba1, 0xbc0, 0xbe0,
0xbff, 0xc1e, 0xc3c, 0xc5a, 0xc78, 0xc95, 0xcb2, 0xccf, 0xcec, 0xd08,
0xd23, 0xd3e, 0xd59, 0xd73, 0xd8d, 0xda7, 0xdc0, 0xdd8, 0xdf1, 0xe08,
0xe1f, 0xe36, 0xe4c, 0xe62, 0xe77, 0xe8c, 0xea0, 0xeb4, 0xec7, 0xeda,
0xeec, 0xefe, 0xf0f, 0xf1f, 0xf2f, 0xf3f, 0xf4d, 0xf5c, 0xf69, 0xf77,
0xf83, 0xf8f, 0xf9a, 0xfa5, 0xfaf, 0xfb9, 0xfc2, 0xfca, 0xfd2, 0xfd9,
0xfdf, 0xfe5, 0xfeb, 0xfef, 0xff3, 0xff7, 0xffa, 0xffc, 0xffd, 0xffe,
0xfff, 0xffe, 0xffd, 0xffc, 0xffa, 0xff7, 0xff3, 0xfef, 0xfeb, 0xfe5,
0xfdf, 0xfd9, 0xfd2, 0xfca, 0xfc2, 0xfb9, 0xfaf, 0xfa5, 0xf9a, 0xf8f,
0xf83, 0xf77, 0xf69, 0xf5c, 0xf4d, 0xf3f, 0xf2f, 0xf1f, 0xf0f, 0xefe,
0xeec, 0xeda, 0xec7, 0xeb4, 0xea0, 0xe8c, 0xe77, 0xe62, 0xe4c, 0xe36,
0xe1f, 0xe08, 0xdf1, 0xdd8, 0xdc0, 0xda7, 0xd8d, 0xd73, 0xd59, 0xd3e,
0xd23, 0xd08, 0xcec, 0xccf, 0xcb2, 0xc95, 0xc78, 0xc5a, 0xc3c, 0xc1e,
0xbff, 0xbe0, 0xbc0, 0xba1, 0xb81, 0xb60, 0xb40, 0xb1f, 0xafe, 0xadd,
0xabb, 0xa9a, 0xa78, 0xa56, 0xa33, 0xa11, 0x9ee, 0x9cc, 0x9a9, 0x986,
0x963, 0x93f, 0x91c, 0x8f9, 0x8d5, 0x8b1, 0x88e, 0x86a, 0x846, 0x823,
0x7ff, 0x7db, 0x7b8, 0x794, 0x770, 0x74d, 0x729, 0x705, 0x6e2, 0x6bf,
0x69b, 0x678, 0x655, 0x632, 0x610, 0x5ed, 0x5cb, 0x5a8, 0x586, 0x564,
0x543, 0x521, 0x500, 0x4df, 0x4be, 0x49e, 0x47d, 0x45d, 0x43e, 0x41e,
0x3ff, 0x3e0, 0x3c2, 0x3a4, 0x386, 0x369, 0x34c, 0x32f, 0x312, 0x2f6,
0x2db, 0x2c0, 0x2a5, 0x28b, 0x271, 0x257, 0x23e, 0x226, 0x20d, 0x1f6,
0x1df, 0x1c8, 0x1b2, 0x19c, 0x187, 0x172, 0x15e, 0x14a, 0x137, 0x124,
0x112, 0x100, 0xef, 0xdf, 0xcf, 0xbf, 0xb1, 0xa2, 0x95, 0x87,
0x7b, 0x6f, 0x64, 0x59, 0x4f, 0x45, 0x3c, 0x34, 0x2c, 0x25,
0x1f, 0x19, 0x13, 0xf, 0xb, 0x7, 0x4, 0x2, 0x1, 0x0,
0x0, 0x0, 0x1, 0x2, 0x4, 0x7, 0xb, 0xf, 0x13, 0x19,
0x1f, 0x25, 0x2c, 0x34, 0x3c, 0x45, 0x4f, 0x59, 0x64, 0x6f,
0x7b, 0x87, 0x95, 0xa2, 0xb1, 0xbf, 0xcf, 0xdf, 0xef, 0x100,
0x112, 0x124, 0x137, 0x14a, 0x15e, 0x172, 0x187, 0x19c, 0x1b2, 0x1c8,
0x1df, 0x1f6, 0x20d, 0x226, 0x23e, 0x257, 0x271, 0x28b, 0x2a5, 0x2c0,
0x2db, 0x2f6, 0x312, 0x32f, 0x34c, 0x369, 0x386, 0x3a4, 0x3c2, 0x3e0,
0x3ff, 0x41e, 0x43e, 0x45d, 0x47d, 0x49e, 0x4be, 0x4df, 0x500, 0x521,
0x543, 0x564, 0x586, 0x5a8, 0x5cb, 0x5ed, 0x610, 0x632, 0x655, 0x678,
0x69b, 0x6bf, 0x6e2, 0x705, 0x729, 0x74d, 0x770, 0x794, 0x7b8, 0x7db,
};

void setup() {
  analogWriteResolution(12);
}

void loop() {
 for(int i=0; i<360; i++) {
 analogWrite(DAC0, sinTable[i]);
 int k=i+180;
 if(k>= 360) k=k-360;
 analogWrite(DAC1, sinTable[k]);
 delay(1);
 }
}

Here's the code.
Thanx for any help.

Thanks for posting correctly.
It looks like you are doing everything correctly although I would have written the loop function some what differently, like this:-

void loop() {
 int k=180;
 for(int i=0; i<360; i++) {
 k++;
 if(k>= 360) k=0;
 analogWrite(DAC1, sinTable[i]);
 analogWrite(DAC0, sinTable[k]);
 delay(1);
 }
}

By moving the definition outside the for loop you prevent the code from creating a new variable every time and thus having a large stack of unused variables cluttering up the place.
I also swapped DAC0 and 0. Do you still get nothing on DAC0? If so your arduino pin might be broken, these pins are very sensitive to over current and can only supply about 3mA so they are easy to blow.

thanx for the help.

Thanx for the help. Thing is, I sued the simple Blink program that comes with the software and assigned the LED pin to DAC0 first and then to DAC1 and they both worked. I also read somewhere that both DAC0 and DAC1 cannot be used sumultaniously and that yuou need a patch to get it to work. Is that true?

I also read somewhere that both DAC0 and DAC1 cannot be used sumultaniously and that yuou need a patch to get it to work.

Not true.

I have tried your code and it works for both outputs so it looks like you have a damaged board.
The period I get is 356mS or a frequency of 2.8Hz

I changed the code to give a 90 degree shift and took this picture:-

Well I'm back at university trying this out. The DAC1 works briliantly but DAC0 still doesn't work. It however does give a very small signal ~20mv that does follow the DAC1. Do you think I need a new board?

Do you think I need a new board?

Well as you can see it worked on mine, if there is nothing else but a scope connected to the pins then I would say yes.

thank you so much. I got a new board and it worked instantly.

Hi

Is ist possible to moifiy the code to "mask the sine wave with a pwm wave" in order to be able to change its frequency and duty cycle...I actually need 3 of those "sine wave masked pwm signals" each is 120 degree phase shifted

I work with Arduino Due

This is my code I have now 3 pwms with variable frequency and duty cycle . I just need to modifiy mask the pwms to sin waves and get the phase shift but i dont know why

#include "C:\Users\AHMED\Dropbox\Masterarbeit\ARDUINO\programme\Setting PWM Frequency\code\pwm01.h"

 




// Hilfe zur PWM ist unter file:///C:/Users/AHMED/Downloads/at91sam9263-ek/at91sam9263-ek/softpack-1.5/at91lib/peripherals/pwmc/pwmc_h0_unit_overview.html


void setup() 
{
  
  
// PWM Pulsbreite eingeben
    uint32_t  pwm_duty_u=  0.75*65536;//2^16 =65536 also maximaler Wert fuer duty cycle ist 65536-1
    uint32_t  pwm_duty_v=  0.75*65536;
    uint32_t  pwm_duty_w=  0.75*65536;
    
    
    
// PWM Frequenz in HZ eingeben
   
    uint32_t  pwm_freq1 =  10000;  
    
//PWM Auflösung

    pwm_set_resolution(16);  
    
    
    
//Hardware Ausgabe

    pwm_setup( 7, pwm_freq1, 2);  //pwm_setup (pin,freq,iclock 2 ist Clock B)
    pwm_setup( 8, pwm_freq1, 2);  
    pwm_setup( 9, pwm_freq1, 2);  
      
    
   
    pwm_write_duty( 7, pwm_duty_u ); // pin, duty_cycle_pro_phase
    pwm_write_duty( 8, pwm_duty_v );  
    pwm_write_duty( 9, pwm_duty_w );  


// Verzögerung in uS

delay(30000);  
        
// PWM Stop 

   // pwm_stop( 7 );
    //pwm_stop( 8 );
    //pwm_stop( 9 );
    
    
    
    //Configure PWM channels 0,1,2,3 (PWML0,PWMH0,PWML1,PWMH1,PWML2,PWMH2,PWML3,PWMH3), (port C.2,C.3,C.4,C.5,C.6,C.7,C.8,C.9), (pins P34,P35,P36,P37,P38,P39,P40,P41)
 REG_PIOC_PDR = 0x3FC;  //B1111111100, PIO Disable Register
 REG_PIOC_ABSR = REG_PIOC_ABSR | 0x3FCu; //B1111111100, Peripheral AB Select Register

 REG_PMC_PCER1 = REG_PMC_PCER1 | 16; //Peripheral Clock Enable Register 1 (activate clock for PWM, id36, bit5 of PMC_PCSR1)
 REG_PWM_ENA = REG_PWM_SR | B1111; //PWM Enable Register | PWM Status Register (activate channels 0,1,2,3)

 REG_PWM_CMR0 = 0x10000; //Channel Mode Register: Dead Time Enable DTE=1
 REG_PWM_CMR1 = 0x10000; //Channel Mode Register: Dead Time Enable DTE=1
 REG_PWM_CMR2 = 0x10000; //Channel Mode Register: Dead Time Enable DTE=1
 REG_PWM_CMR3 = 0x10000; //Channel Mode Register: Dead Time Enable DTE=1
 REG_PWM_DT0 = 0xA800A8; //PWM Channel Dead Time Register (168=2us for outputs PWML0,PWMH0)  
 REG_PWM_DT1 = 0xA800A8; //PWM Channel Dead Time Register (168=2us for outputs PWML1,PWMH1)
 REG_PWM_DT2 = 0xA800A8; //PWM Channel Dead Time Register (168=2us for outputs PWML2,PWMH2)  
 REG_PWM_DT3 = 0xA800A8; //PWM Channel Dead Time Register (168=2us for outputs PWML3,PWMH3)

 REG_PWM_CPRD0 = 5600; //1/5600 resolution for VoltsA (84mhz/5600=15khz=66.667us period)
 REG_PWM_CPRD1 = 5600; //1/5600 resolution for VoltsB (84mhz/5600=15khz=66.667us period)
 REG_PWM_CPRD2 = 65535; //16 bit resolution for VoltsA DC Power Supply amplitude (84mhz/65535=1.281khz=780.64us period)
 REG_PWM_CPRD3 = 65535; //16 bit resolution for VoltsB DC Power Supply amplitude (84mhz/65535=1.281khz=780.64us period)

        


}

void loop() 
{  
}

this is the header file

#include "C:\Programs\arduino-1.5.1r2\hardware\arduino\sam\libraries\Pwm01\pwm01.h"

void setup()
{
uint32_t pwm_duty = 32767;
uint32_t pwm_freq1 = 2;
uint32_t pwm_freq2 = 5000;

// Set PWM Resolution
pwm_set_resolution(16);

// Setup PWM Once (Up to two unique frequencies allowed
//-----------------------------------------------------
pwm_setup( 6, pwm_freq1, 1); // Pin 6 freq set to "pwm_freq1" on clock A
pwm_setup( 7, pwm_freq2, 2); // Pin 7 freq set to "pwm_freq2" on clock B
pwm_setup( 8, pwm_freq2, 2); // Pin 8 freq set to "pwm_freq2" on clock B
pwm_setup( 9, pwm_freq2, 2); // Pin 9 freq set to "pwm_freq2" on clock B

// Write PWM Duty Cycle Anytime After PWM Setup
//-----------------------------------------------------
pwm_write_duty( 6, pwm_duty ); // 50% duty cycle on Pin 6
pwm_write_duty( 7, pwm_duty ); // 50% duty cycle on Pin 7
pwm_write_duty( 8, pwm_duty ); // 50% duty cycle on Pin 8
pwm_write_duty( 9, pwm_duty ); // 50% duty cycle on Pin 9

delay(30000); // 30sec Delay; PWM signal will still stream

// Force PWM Stop On All Pins
//-----------------------------
pwm_stop( 6 );
pwm_stop( 7 );
pwm_stop( 8 );
pwm_stop( 9 );
}

void loop()
{
}

mask the sine wave with a pwm wave

What is a mask?

Sorry I think that I used thr wrong word..

I mean I want to creat a sin wave throught a pwm signal

like this one :