I am not sure if I write in a right forum, so please move it if necessary.
I am a beginner in electronics and serial interfaces. So maybe the questions in this message are trivial but I don't have an idea how to solve them.
I am trying to generate a sinusoidal wave signal using Arduino Uno and YL-40 DAC. The problem is that maximal frequency I achieved is only about 12Hz (checked by an oscilloscope). I made a global array sinVals with precalculated sine values in order to speed up the program by releasing Arduino Uno CPU from calculating sine values which is an expensive operation. I have two questions:
- When I put sinVals in flash (using PROGMEM keyword) the generated wave is not sine but some random-looking one. In contrast, when sinVals is in dynamic memory the sine wave looks as expected. Why putting the array in the flash memory does not work as expected and how to fix it?
- How to achieve higher frequency of generated signal? What can be the maximal frequency using the YL-40 DAC?
Waking up YL-40 for sending only one byte at time is inefficient. Sending the whole sinVals array at once by Wire.write(sinVals,255) makes new problems: even though the function returns 255 (so the whole wave is sent) my oscilloscope shows that only a small initial part of sine wave is generated, though the frequency of the cut-off wave is then ~240Hz.
Here is the code:
[/list]
#include "Wire.h"
#define PCF8591 (0x90 >> 1) // I2C bus address
//When byte const PROGMEM sinVals[] is in the line below the generated wave is random, not the sinusoidal one as the precalculated values represent
byte const sinVals[] = {64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 66, 66, 67, 67, 68,
68, 69, 70, 70, 71, 72, 73, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
86, 87, 88, 89, 90, 92, 93, 94, 96, 97, 99, 100, 101, 103, 104, 106, 107,
109, 110, 112, 113, 115, 116, 118, 119, 121, 123, 124, 126, 127, 128, 130,
131, 133, 134, 136, 137, 139, 141, 142, 144, 145, 147, 148, 150, 151, 152,
154, 155, 157, 158, 159, 161, 162, 163, 165, 166, 167, 168, 169, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 181, 182, 183, 184, 184, 185,
186, 186, 187, 187, 188, 188, 189, 189, 189, 190, 190, 190, 190, 190, 190,
190, 190, 190, 190, 190, 190, 190, 190, 190, 189, 189, 189, 188, 188, 187,
187, 186, 186, 185, 184, 184, 183, 182, 181, 181, 180, 179, 178, 177, 176,
175, 174, 173, 172, 171, 170, 168, 167, 166, 165, 163, 162, 161, 159, 158,
157, 155, 154, 153, 151, 150, 148, 147, 145, 144, 142, 141, 139, 138, 136,
134, 133, 131, 130, 128, 127, 126, 124, 123, 121, 120, 118, 117, 115, 114,
112, 111, 109, 108, 106, 105, 103, 102, 100, 99, 97, 96, 95, 93, 92, 91,
89, 88, 87, 86, 85, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 74, 73, 72, 71,
70, 70, 69, 68, 68, 67, 67, 66, 66, 65, 65, 65, 64, 64, 64, 64, 64, 64, 64, 64};
void setup()
{
Wire.begin();
}
void loop()
{
for (int i=0; i<256; i++)
{
Wire.beginTransmission(PCF8591); // wake up PCF8591
Wire.write(0x40); // control byte - turn on DAC (binary 1000000)
Wire.write(sinVals[i]); // value to send to DAC
//the line above when replaced by int nTransfered=Wire.write(sinVals,255) and "for" is removed, the sine wave is not complete
//even though returned value nTransfered is 255
Wire.endTransmission(); // end tranmission
}
}