• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

DAC Sin Wave.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

ShadowPho

Member
Joined
Jun 8, 2005
Location
I am in your stack, SUBbing your registers!
Here's my setup. I have a microproccesor MSP430F2012 ( linky ) connected to a DAC8830 ( linky ).


The CPU has a sin table which it transmits via SPI to the DAC.


The problem is that the output from the DAC is very not perfect. I am attaching a screenshot of the scenario up close and far away. It looks to be a good sin wave... just dirty. Why? Can someone please help me with this?


Full code attached to this post. I am posting small snippets here.

Code:
//Main Loop

  while(1)
  {
    P1OUT |= 0x01; //Turn LED on
    
    for(i=0;i<511;i++)
         spi_send_byte(sinwave[i]); //transfer sine wave down-up
    
    P1OUT &= ~0x01; //Turn LED off
    
    for(i=512;i>1;i--)
         spi_send_byte(sinwave[i]); //transfer sin wave up-down
  }

Code:
//sindata.h

#ifndef SINDATA_H_
#define SINDATA_H_


const unsigned int sinwave[]={0,201,402,603,804,1005,1206,1407,1608,1809,2010,2211,2412,\
  2613,2814,3015,3216,3417,3617,3818,4019,4219,4420,4621,4821,5022,5222,5422,5623\
  ,5823,6023,6224,6424,6624,6824,7024,7224,7423,7623,7823,8022,8222,8421,8621,\
  8820,9019,9218,9417,9616,9815,10014,10212,10411,10609,10808,11006,11204,11402\
  ,11600,11798,11996,12193,12391,12588,12785,12983,13180,13376,13573,13770,\
  13966,14163,14359,14555,14751,14947,15143,15338,15534,15729,15924,16119,16314\
  ,16508,16703,16897,17091,17285,17479,17673,17867,18060,18253,18446,18639,18832\
  ,19024,19216,19409,19600,19792,19984,20175,20366,20557,20748,20939,21129,21320\
  ,21510,21699,21889,22078,22268,22457,22645,22834,23022,23210,23398,23586,23774\
  ,23961,24148,24335,24521,24708,24894,25080,25265,25451,25636,25821,26005,26190\
  ,26374,26558,26742,26925,27108,27291,27474,27656,27838,28020,28202,\
  28383,28564,28745,28926,29106,29286,29466,29645,29824,30003,30182,30360,\
  30538,30716,30893,31071,31248,31424,31600,31776,31952,32127,32303,32477,32652,\
  32826,33000,33173,33347,33520,33692,33865,34037,34208,34380,34551,34721,34892\
  ,35062,35231,35401,35570,35738,35907,36075,36243,36410,36577,36744,36910,37076\
  ,37241,37407,37572,37736,37900,38064,38228,38391,38554,38716,38878,39040,39201\
 ,39362,39523,39683,39843,40002,40161,40320,40478,40636,40794,40951,41108,41264,\
 41420,41576,41731,41886,42040,42194,42348,42501,42654,42806,42958,43110,43261,\
 43412,43562,43713,43862,44011,44160,44308,44456,44604,44751,44898,45044,45190,\
 45335,45480,45625,45769,45912,46056,46199,46341,46483,46624,46765,46906,47046\
 ,47186,47325,47464,47603,47741,47878,48015,48152,48288,48424,48559,48694,48828\
 ,48962,49095,49228,49361,49493,49624,49756,49886,50016,50146,50275,50404,50532\
 ,50660,50787,50914,51041,51166,51292,51417,51541,51665,51789,51911,52034,52156\
 ,52277,52398,52519,52639,52759,52878,52996,53114,53232,53349,53465,53581,53697\
 ,53812,53926,54040,54154,54267,54379,54491,54603,54714,54824,54934,55043,55152\
 ,55260,55368,55476,55582,55689,55794,55900,56004,56108,56212,56315,56418,56520\
 ,56621,56722,56823,56923,57022,57121,57219,57317,57414,57511,57607,57703,57798\
 ,57892,57986,58079,58172,58265,58356,58448,58538,58628,58718,58807,58896,58983\
 ,59071,59158,59244,59330,59415,59499,59583,59667,59750,59832,59914,59995,60075\
 ,60156,60235,60314,60392,60470,60547,60624,60700,60776,60851,60925,60999,61072\
 ,61145,61217,61288,61359,61429,61499,61568,61637,61705,61772,61839,61906,61971\
 ,62036,62101,62165,62228,62291,62353,62415,62476,62536,62596,62655,62714,62772\
 ,62830,62886,62943,62998,63054,63108,63162,63215,63268,63320,63372,63423,63473\
 ,63523,63572,63621,63668,63716,63763,63809,63854,63899,63944,63987,64031,64073\
 ,64115,64156,64197,64237,64277,64316,64354,64392,64429,64465,64501,64536,64571\
 ,64605,64639,64672,64704,64735,64766,64797,64827,64856,64884,64912,64940,64967\
 ,64993,65018,65043,65067,65091,65114,65137,65159,65180,65200,65220,65240,65259\
 ,65277,65294,65311,65328,65343,65358,65373,65387,65400,65413,65425,65436,65447\
 ,65457,65467,65476,65484,65492,65499,65505,65511,65516,65521,65525,65528,65531,65533,65535,65535};

#endif /*SINDATA_H_*/
 

Attachments

  • DACtry.zip
    2.9 KB · Views: 219
  • closeupoutput.png
    closeupoutput.png
    22.1 KB · Views: 399
  • CSvsSCLK2.png
    CSvsSCLK2.png
    23.7 KB · Views: 384
  • farOutput.png
    farOutput.png
    22.7 KB · Views: 384
  • SDIvsSCLKpic1.png
    SDIvsSCLKpic1.png
    24 KB · Views: 382
My guess would be that the noise comes from pumping the data into the DAC bit by bit. ie, when a full 8 bits have been received the sample is correct, but once you start the next bit(s), the DAC reinterprets the data with the new bits and some old bits, until the full new 8 bits have shifted in.

Edit

Two things I notice from the 8830 doc:

1. This is a 16-bit device, msb first. If you are only sending a byte, then that's a problem.
2. The DAC output is supposed to update itself at the rising edge of /CS with falling edge of /CS
indicating the start of a new 16-bit word. How do you have /CS wired?
 
Last edited:
My guess would be that the noise comes from pumping the data into the DAC bit by bit. ie, when a full 8 bits have been received the sample is correct, but once you start the next bit(s), the DAC reinterprets the data with the new bits and some old bits, until the full new 8 bits have shifted in.

Edit

Two things I notice from the 8830 doc:

1. This is a 16-bit device, msb first. If you are only sending a byte, then that's a problem.
2. The DAC output is supposed to update itself at the rising edge of /CS with falling edge of /CS
indicating the start of a new 16-bit word. How do you have /CS wired?

Code:
 void spi_send_byte(u16 input) // I guess it's a 16bit byte 
{
  while (!(USICTL1&USIIFG)); //wait until USIIFG is clear-Prev. tran. complete
  P1OUT &= ~(CS_LINE); //Put the SS low
  USISR=input;              //give the input
  USICNT=USI16B+16;    //do 16 bits at a time, cnt = 16.
  while (!(USICTL1&USIIFG)); //block until complete
  P1OUT |= (CS_LINE); //Set SS high - DAC update
}

So, I check/wait if it's still transferring. Then I put SS/CS low, put input, set counter, wait to complete and pull SS high.

Also,
Code:
 typedef unsigned int u16;

So u16 should be unsigned int, and this is a 16bit chip so I think it transfers 16 bits.

Thank you for looking at the code so far! It is really helpful! :)
 
Ok, I wasn't sure, thanks for the clarification.
The code looks right then.
How about a low-pass filter on the output?

Edit

Just for fun you could try reversing the bit order in your sine table entries,
perhaps it is an unseen bit ordering issue.
 
Ok, I wasn't sure, thanks for the clarification.
The code looks right then.
How about a low-pass filter on the output?

Edit

Just for fun you could try reversing the bit order in your sine table entries,
perhaps it is an unseen bit ordering issue.

Ah! Figured it out!

My SPIPHC bit needed to be set (Shifts the clock one half to the right. This means aquisition happens on rising clock.)
After I set it it started working perfectly.
 

Attachments

  • sinwave1.png
    sinwave1.png
    25.1 KB · Views: 361
Cool.

Why is the wave rectified like that? Not really a sine wave now.

Because if I want to flip it that'd require negative voltages :), which would require work, and this is just a test.

The ultimate goal is to really generate sound (music) from a SD card, which I thought only requires changing waveform and not necessary negative voltages.
 
no, your dac takes values from like 0-256 right? range from 0-5V??

make your "0" = 128 so you will be able to rectify a full wave. with a swing from 0-5V, 2.5v being your "0"
 
Is the DAC meant for sound? I had the mistake my first time of choosing a DAC that wasn't good enough to be used for music, just didn't cut it.
 
Is the DAC meant for sound? I had the mistake my first time of choosing a DAC that wasn't good enough to be used for music, just didn't cut it.

Yes. I think this one is good... 16 bits precision, 1MHz speed.

Now I gotta interface with an SD card. Gonna be fun.

EDIT:
Any recomendations? Any ideas on how big capacitor to choose? Is TDA702 good for amp purpose?
 
Yes. I think this one is good... 16 bits precision, 1MHz speed.

Now I gotta interface with an SD card. Gonna be fun.

EDIT:
Any recomendations? Any ideas on how big capacitor to choose? Is TDA702 good for amp purpose?

Idk what that is. Do you need a very big one? Would a LM386 do?

Also, I remember what a nightmare it was to interface with a SD card using SPI interface. What made it worse was I was using a PIC microcontroller that only had UART, so I had to write a bitbanging routine that used GPIO pins and handles all the SPI stuff. What a nightmare, and I had no oscilloscope or anything to troubleshoot with. Just a volt meter.
 
Idk what that is. Do you need a very big one? Would a LM386 do?

Also, I remember what a nightmare it was to interface with a SD card using SPI interface. What made it worse was I was using a PIC microcontroller that only had UART, so I had to write a bitbanging routine that used GPIO pins and handles all the SPI stuff. What a nightmare, and I had no oscilloscope or anything to troubleshoot with. Just a volt meter.

LM386 would not do, but TDA702 is a good one. Highly recommend! The kit already includes everything, even the knob to change volume. It works amazingly well, and the speaker draws 100 milliamps in max volume. (bye bye battery).

And I feel your pain... when I first got to the university I had nothing, no oscilloscope, no microcontrollers... it was a pain at first. Also, PICs in my experience have been extremely underpowered and hard to use.
Luckily msp430f2012 has an SPI module, but alas, not interrupt driven one. Thus I have to wait for it to complete before I can pull the line down and do something else.
 
Back