I made this code. My aim is to make an 8 bit synthesizer, making a R2R resistor ladder DAC and connect it to pins 0-7. Then connecting a capacitor to remove the offset. The problem is that for making it work at high frequencies, I would need a sampling rate of 44100hz (the standard), but I barely reach 1000Hz.
My question is: is there a way to use PORTD or something like that to convert a number to outputs in a digital pin and without all those lines of code I made below? I would like to simplify it in order to get higher sampling rates. For example, convert 255 to 11111111, meaning 1 as high and 0 as low, and interpret this directly to pins 0-7.
The array Val is just the values of a sinewave, and the delay at the bottom would set the sampling rate, but with lower values I've checked it in an oscilloscope and it is unstable
int i=0;
int R1,R2,R3,R4,R5,R6,R7,R8;
int Val[] = {128 , 132 , 136 , 139 , 143 , 147 , 151 , 155 , 159 , 163 , 167 , 171 , 174 , 178 , 182 , 185 , 189 , 192 , 196 , 199 , 202 , 206 , 209 , 212 , 215 , 218 , 220 , 223 , 226 , 228 , 231 , 233 , 235 , 237 , 239 , 241 , 243 , 245 , 246 , 247 , 249 , 250 , 251 , 252 , 253 , 253 , 254 , 254 , 255 , 255 , 255 , 255 , 255 , 254 , 254 , 253 , 253 , 252 , 251 , 250 , 249 , 247 , 246 , 245 , 243 , 241 , 239 , 237 , 235 , 233 , 231 , 228 , 226 , 223 , 220 , 218 , 215 , 212 , 209 , 206 , 202 , 199 , 196 , 192 , 189 , 185 , 182 , 178 , 174 , 171 , 167 , 163 , 159 , 155 , 151 , 147 , 143 , 139 , 136 , 132 , 127 , 123 , 119 , 116 , 112 , 108 , 104 , 100 , 96 , 92 , 88 , 84 , 81 , 77 , 73 , 70 , 66 , 63 , 59 , 56 , 53 , 49 , 46 , 43 , 40 , 37 , 35 , 32 , 29 , 27 , 24 , 22 , 20 , 18 , 16 , 14 , 12 , 10 , 9 , 8 , 6 , 5 , 4 , 3 , 2 , 2 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 10 , 12 , 14 , 16 , 18 , 20 , 22 , 24 , 27 , 29 , 32 , 35 , 37 , 40 , 43 , 46 , 49 , 53 , 56 , 59 , 63 , 66 , 70 , 73 , 77 , 81 , 84 , 88 , 92 , 96 , 100 , 104 , 108 , 112 , 116 , 119 , 123};
int v=0;
void setup() {
DDRD = B11111111;
}
void loop() {
R1=Val[i] & 1;
R2=Val[i] & 2;
R3=Val[i] & 4;
R4=Val[i] & 8;
R5=Val[i] & 16;
R6=Val[i] & 32;
R7=Val[i] & 64;
R8=Val[i] & 128;
digitalWrite(2,R1);
digitalWrite(3,R2);
digitalWrite(4,R3);
digitalWrite(5,R4);
digitalWrite(6,R5);
digitalWrite(7,R6);
digitalWrite(8,R7);
digitalWrite(9,R8);
i=i+1;
if (i==199)
{
i=0;
}
delayMicroseconds(1000);
}