I'm trying making a sine wave generator using the DAC MCP4921 via SPI, i wrote my code
#include <SPI.h>
const int sine[24]={14336,14865,15359,15783,16109,16313,16383,16313,16109,15783,15359,14865,14336,13806,13312,12888,12562,12358,12288,12358,12562,12888,13312,13806};
const int slaveSelectPin = 10;
void loop()
{
int i;
for(i=0;i<=23;i++)
{
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(highByte(sine*));*
_ SPI.transfer(lowByte(sine*));_
_ digitalWrite(slaveSelectPin,HIGH);_
_ }_
_}_
_[/quote]_ _as you see, my sine wave is made from 24 steps of voltage, each step needs 16 bits, with the SPI clock at 8MHz, i think the code above will create 8000000/(2416) ~ 20KHz sine wave. However, it can only make a 2.9KHz sine wave_ Can you help me with this problem?
16,000,000 cycles/second/24 samples means absolute best frquency possible would be 666,667 Hz
divide by at least 6 if all the instructions in reply #2 executed in 1 clock cycle
down to 111,111 Hz
each SPI transfer is 8 clock cycles, so divide by 16
down to 6944 Hz.
So 20 KHz does seem like a stretch.
It takes about 100 instructions to send a byte via software spi. So your case, it should be around 200 instructions or 12us per step. Times 24 steps gets you to about 300us for the whole cycle.
I think he does that just to see if we are awake... I Sure wish he'd stop it... it doesn't bring anything new to the discussion but rather points out...
sorry all, i've just waken up, it's 8AM here
first, thank you all for answering my problem
yes, maximum frequency i can have is 3KHz
with CrossRoads's help, the frequency increases a bit. Actually, this project is for testing an amplifier, i need to make sine wave that have frequency range from 10Hz to about 20KHz in order to create the frequency response of the amplifier
i'm still trying all the solutions you gave but it seems it doesn't work. Maybe i should look for another solutions instead of using DAC
If you translated those sine values to a resistor array, you could use a shift register to "Scan them" at any clock rate required and create sine waves from that method very easily just by clocking a shift register although the maf ferq is 1/8th of the clock rate, would require a 160 KHz shift clock for 20 KHZ...
CrossRoads:
each SPI transfer is 8 clock cycles, so divide by 16
16 cycles minimum. Maximum SPI rate is clock/2 and you are clocking out 8 bits. Plus there is a school of thought in another thread that you need one more cycle.