# software 1bit DAC

Discussion in 'Programmer's Corner' started by kubeek, Sep 25, 2011.

1. ### kubeek Thread Starter AAC Fanatic!

Sep 20, 2005
4,670
804
Hi,

im trying to write a 1bit sigma-delta DAC in C, but I don´t really know where to start. I wrote this piece of code to try it, but I really have no idea how to correctly do the integration which i feel should be there somewhere. Basically i would like to get it to compute this http://upload.wikimedia.org/wikipedia/en/2/20/Pulse-density_modulation_1_period.gif, or actually the 16 bits that approximate the sine first quadrant.

Here is my code:
Code ( (Unknown Language)):
1. #include <math.h>
2. #include <stdio.h>
3. #define PI 3.141592654
4.
5. int main(int argc, char** argv) {
6.     double sinv[16];
7.     for(int i=0;i<16;i++){
8.     sinv[i]=sin(((double)i+1)*PI/32);
9.     }
10.
11.
12.     double val=0.25;
13.     double delta, sum=0;
14.
15.
16.     for(int i=0;i<16;i++){
17.     //val=sinv[i];
18.
19.     delta=sum-val;
20.     if(delta<=0){
21.         sum=(sum*1.0+1.0/16)/1;
22.         printf("1 %f %f\n",sum, val);
23.     }
24.     else {
25.         sum=(sum*1.0-1.0/16)/1;
26.        printf("0 %f %f\n", sum, val);
27.     }
28.
29.     }
30.
31.     return 0;
32. }
33.
34.
35. [/i][/i]

2. ### kubeek Thread Starter AAC Fanatic!

Sep 20, 2005
4,670
804
Ok so I got it to work like this with the help of this cool page http://www.beis.de/Elektronik/DeltaSigma/DeltaSigma.html

#include <math.h>
#include <stdio.h>

//using namespace std;

#define PI 3.141592654
#define SIZE 64
/*
*
*/
int main(int argc, char** argv) {

double sinv[SIZE];

for(int i=0;i<SIZE;i++){
sinv=sin(((double)i+1)*PI/SIZE/2);
}

double val=0.05;
double delta, sum=0, out=0, oldsum=0;

for(int i=0;i<SIZE;i++){
val=sinv;

delta=val-out;
sum=delta+oldsum;
if( oldsum>=0)
{
printf("1 %f %f %f \n",sum, oldsum,val);
out=1.0;
}
else {
printf("0 %f %f %f\n", sum, oldsum,val);
out=-1.0;
}
oldsum=sum;
}

return 0;
}

Last edited: Sep 27, 2011