Pages: [1] 2 3   Go Down
Author Topic: Arduino DDS Sinewave Generator  (Read 22269 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


has anyone tryed to run this code on a Mega 1280

http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/

since i have not such a board i cant check where the problem is.
my suspicion is the timer2 unit, please compare in both datasheets the register description of timer2.

martin
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8952
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On an Arduino Mega the pin mappings are somewhat different.  The compare output for Timer 2A is on pin 10 instead of 11.

Pin mappings:
https://spreadsheets.google.com/pub?key=rtHw_R6eVL140KS9_G8GPkA&gid=0
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Auckland
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Web Developer going physical interface Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, thanks guys, I can confirm that it works a charm using pin10 instead of 11 smiley Good reference, that spreadsheet!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good work.
Can i generate a 40kHz senoidal signal?.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can i generate a 40kHz senoidal signal?.
Do you mean "sinusoidal"?
With that software, no - you'd need to boost the clock to at least 80kHz, and I'm not sure the ISR would run at that frequency.

Do you really need a 40kHz sine wave?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can i generate a 40kHz senoidal signal?.
Do you mean "sinusoidal"?
With that software, no - you'd need to boost the clock to at least 80kHz, and I'm not sure the ISR would run at that frequency.

Do you really need a 40kHz sine wave?

I managed to do it. My current 8bit look up table dds has a 4us interrupt, I have been able to reach 80khz sampling rate but this reaches the limit of the Arduino.
 44k is Ok, but without much CPU overhead for somerhing else. Im working on midi synth with 22k rate atm.

EDIT : With this code, I have been able to operate my DIY 8 BIT SPI DAC at a frequency of 330kHz. The output of the DAC was a saw signal (image of the counter...) at 1.35kHz / 256 samples per cycle.
So a 44kHz DDS using a Look up Table is highly feasible indeed.

Quote
//Pin connected to ST_CP of 74HC595
int latchPin = 10;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 11;
////
unsigned char test_char=0;

//--- Used to setup SPI based on current pin setup
//    this is called in the setup routine;
void setupSPI(){
  byte clr;
  SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
  SPCR &= ~( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
  clr=SPSR; // clear SPI status reg
  clr=SPDR; // clear SPI data reg
  SPSR |= (1<<SPI2X); // set prescaler bits
  delay(10);
}


//--- The really fast SPI version of shiftOut
byte spi_transfer(byte data)
{
  SPDR = data;           // Start the transmission
  loop_until_bit_is_set(SPSR, SPIF);
  return SPDR;           // return the received byte, we don't need that
}

void setup() {
  // put your setup code here, to run once:
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(latchPin, HIGH);
  digitalWrite(clockPin, LOW);
  digitalWrite(dataPin, LOW);
  byte clr;
  SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
  SPCR &= ~( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
  clr=SPSR; // clear SPI status reg
  clr=SPDR; // clear SPI data reg
  SPSR |= (1<<SPI2X); // set prescaler bits
  delay(10);
  Serial.begin(9600);
  Serial.println("Init Ok");
}

void loop() {
  // put your main code here, to run repeatedly:
  //shiftOut(dataPin, clockPin, MSBFIRST, test_char);  
  spi_transfer(test_char);
  PORTB &= ~(4); //digitalWrite(latchPin, LOW);
  //Serial.println(test_char,DEC);
  PORTB |= 4; //digitalWrite(latchPin, HIGH);
  //delay(1000);
  test_char += 1;
  //delay(1000);
}

« Last Edit: July 13, 2011, 03:03:17 pm by D4p0up » Logged

Arduino sound Reseach - http://bit.ly/fullmaj [fr][en] | Homemade Free VST - http://zomg.zxq.net [en]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@D4p0up

why not call  setupSPI();  in  setup()  ??

The same code....

and try Serial.begin(115200)  then you have more time for other things....

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

With this code, I have been able to operate my DIY 8 BIT SPI DAC at a frequency of 330kHz.

Very nice - I was wondering, from the page you referenced, 1% resistors are needed for 6 bit, and more precise for higher bits - has anyone had success using potentiometers to get more accurate resistances?
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just thinking about it, you reach a point where even the soldering flux has an impact on the total resistor of the resulting design...

integrated chip designers do nanometric adjusments of the R2R ladders to reach the expected accuracy. So with pots... you might end up spending your life seeking for a percentage of digit on your multimeter... in vain.
Logged

Arduino sound Reseach - http://bit.ly/fullmaj [fr][en] | Homemade Free VST - http://zomg.zxq.net [en]

Offline Offline
Jr. Member
**
Karma: 1
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

COntinuing the discussion and answering the question...

About the SPI, I feel like digging into the Datasheet of the AVR and configuring every single bit on my own instead of using the Arduino dedicated functions. Of course they are much more user friendly, but I want to learn more about the HW and be closer as possible to it, so i might do the same, I prefer ton configure it on my own.

About the ADC, I have today very good audio results with my 8 bits DAC presented in the links above. Resistors are basic 5% tolerance, so there is non linearities spread among the way, it can be heared, but for a "lowfi but still accurate" project, it's worth it.

I will eventually use MCP4921's for my final Synth Shield, but only for "cleaning" purpose, I feel perfectly at ease with the current resistor based 8 bit DAC. I have not tried higher resolutions though, because of this table you mention.
Logged

Arduino sound Reseach - http://bit.ly/fullmaj [fr][en] | Homemade Free VST - http://zomg.zxq.net [en]

0
Offline Offline
Shannon Member
****
Karma: 214
Posts: 12434
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand why you don't just lose the shift register and have the R-2R ladder on 8 Arduino pins (say port D then writing is a single instruction).  You'll get to MHz with the sinewave table perhaps?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Jr. Member
**
Karma: 1
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Good question, simple answer: The lower the pin count, the better it is.

In the audio synth application I'm currently working on, I need the pins 0 & 1 for Midi In/Out purpose, and also need four analog inputs for control Knobs and so on. Add to this three Leds for Synth operation purpose (gate + lfo rate + general purpose), four buttons for operation like preset managment and edition, and make the breakout:

2 + 4 + 3 + 4 = 13 pins, with 9 being discretes.

With this basic setup, I have no room for Port dedicated 8 Bit DAC on a Uno board.

Then, in the future I plan to use a MCP4921 as a DAC. This will lead to better results, so using a homemade SPI DAC in this case is the best way to anticipate design evolutions.

However, as I mentionned on my blog, I started using a rough 6bits DAC on PortC with great success... except for dynamic range of course smiley

Logged

Arduino sound Reseach - http://bit.ly/fullmaj [fr][en] | Homemade Free VST - http://zomg.zxq.net [en]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

        Is it possible to have a three phase sine wave with this generator ?
        I would like to generate a 10 kHz to idealy 18 kHz three phase wave.
        Does miro-Hertz precision will pertain with this change ?

ten Khz is anyway a problem, the atmega is to slw for that. using a dedicated DDS chip would be a solution
maybe some manufactor has a 3 phase version.

martin
« Last Edit: September 07, 2011, 01:38:18 am by marnaw » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You definitely could with the R2R SPI DAC trick presented above:

Just chain 3 x 74HC595 with their associated DAC, and use the same method as described above, except that the 3 respective phase accumulators should be initialised with 0, (1/3)x2^16, (2/3)x2^16.

This will make 3 outputs !
Logged

Arduino sound Reseach - http://bit.ly/fullmaj [fr][en] | Homemade Free VST - http://zomg.zxq.net [en]

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for those two replies, the original post about frequency production limit came from an email that I addressed to marnaw about 10 Khz to 18 Khz 3-phase sine wave.
I found this and marnaw is in the pdf file.(I)

I did not be able to contact the author and he claimed of 13 Khz, I think it's about switching pwm, don't you ?
So, first, my question still pertain : does I really need a dedicated dds to perform my signals ?

Second, does marnaw work deal with this, sometimes also called magic sine waves ?(II)
Best regards.
Sril

Edit (09/23/11) :
(I) is a non finish student work about control motor, does not need to be read, sorry.
(II) deal with a method to suppress harmony under a certain level from base frequency and is not the same as described heare, sorry.
I insist to present my apologizes for stupid lost time to read me.

Go-on my work with a 3-phased generating circuit.
« Last Edit: September 23, 2011, 06:49:28 am by sriliam » Logged

Pages: [1] 2 3   Go Up
Jump to: