X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f X-Recipient: geda-user AT delorie DOT com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=IejcKr2UM4vhWRUZtlYUuSJRGLZpO81cpuXQUQKjRG8=; b=PViGOENzKgUs8PiXnIRB8GLHj3Qb3Bt0jcsEEQD0qiyFdq45OfuLl5RyjQ4JhZ/kWr +a4hCRyF88NR+PcZMU/Bgz/f21tcalHhXHkmpT9m+9oEu+I+2Et8mKc5KIjxTB4wpNVM 1ccoBI/rp8rqDMzo90Z5TXeE+dMDUk3mkClJP0oMkC9SfTCkkRKWikqz+1WnXv5eoDZy N+7lD6c9DWuzMboVZ3SMJVYFTin+rRTfcQlnlvsWaXj8pSQ995GQU/LA/y7LeUMtJOis jkIbfNRSFpBiNDfr9oD12tMlS6IKLl5YMuCa94FnIV6cLOplFrMZsV6H0w3GwVJdQDRo 2ndw== MIME-Version: 1.0 X-Received: by 10.220.64.69 with SMTP id d5mr40780771vci.11.1388517984207; Tue, 31 Dec 2013 11:26:24 -0800 (PST) Date: Tue, 31 Dec 2013 14:26:24 -0500 Message-ID: Subject: [geda-user] Help sending a sine wave to a speaker From: Rob Butts To: geda-user AT delorie DOT com Content-Type: multipart/alternative; boundary=089e010d97303689d404eed98910 Reply-To: geda-user AT delorie DOT com --089e010d97303689d404eed98910 Content-Type: text/plain; charset=ISO-8859-1 It has been a while since I've written major C code so I'm having a hard time coming up with the code. I have a simple design where I use a microchip pic16f1825 to read a byte from an eeprom via spi and send it to a dac and ultimately to a speaker. As a dac and speaker test I want to send a simple 1000Hz sine wave to the dac at an 8KHz rate. Can anyone check my thoughts below? Here's my thinking... If I set my timer to interrupt at an 8KHz rate and if I want to play the tone for 10 seconds the for loop might look something like this: for(j=0; j < 80000; j++){ // j is the sample index // straight sine function means one cycle per 2*pi samples // sample(j) = sin(j) // multiply by 2*pi now it is one cycle per sample // sample(j) = sin((2 * pi) * j) // multiply by 1,000 samples per second now it is cycles per second // sample(j) = sin(1000 * 2 * pi * j) // divide by 8000 samples per second now it is cycles per 8000 samples // sample(j) = sin(1000 * 2 * pi * j / 8000) // the 1000 cycles is the frequency so in those terms // sample(j) = sin(freq * 2 * pi * j / 8000) // now if I want to normalize the amplitude to 0xff the final value is // sample(j) = 0xff * sin(freq * 2 * pi * j / 8000) // so... declaring sample as a float sample = 0xff * sin(freq * 2 * pi * j / 8000); while(!update_dac){ // where update_dac is set to 1 in the timer interrupt handler } update_ dac = 0; send_dac((UINT8) sample); } When I execute the code and step through it the value for sample when j = 1 is 180.238where I expect 3.146 Can someone see my error or what is going on? Thanks --089e010d97303689d404eed98910 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
It has been a while since I've written major C co= de so I'm having a hard time=20 coming up with the code.
I have a simple design where I use = a microchip=A0pic16f1825 to=20 read a byte from an eeprom via spi and send it to a dac and ultimately to a= =20 speaker.=A0 As a dac and speaker test I want to send a simple 1000Hz sine w= ave to=20 the dac at an 8KHz rate.=A0
=A0=A0=A0
Can anyone=A0check my thoughts= below?=A0 Here's my thinking...
If I=20 set my timer to interrupt at an 8KHz rate and if I want to play the tone fo= r 10=20 seconds the for loop might look something like this:
=A0
for(j=3D0;= =A0j <=20 80000; j++){
=A0=A0=A0=A0 //=A0j is the sample index
=A0=A0=A0 // s= traight sine function=20 means one cycle per 2*pi samples
=A0=A0=A0 //=A0sample(j) =3D sin(j) =A0=A0=A0 //=20 multiply by 2*pi now it is one cycle per sample
=A0=A0=A0 // sample(j) = =3D sin((2 *=20 pi) * j)
=A0=A0=A0 // multiply by 1,000 samples per second now it is cy= cles per=20 second
=A0=A0=A0 // sample(j) =3D sin(1000 * 2 * pi * j)
=A0=A0=A0 = // divide by 8000=20 samples per second now it is cycles per 8000 samples
=A0=A0=A0 // sampl= e(j) =3D=20 sin(1000 * 2 * pi * j / 8000)
=A0=A0=A0 // the 1000 cycles is the frequ= ency so in=20 those terms
=A0=A0=A0 //=A0 sample(j) =3D sin(freq * 2 * pi * j / 8000)=
=A0=A0=A0 // now=20 if I want to normalize the amplitude to 0xff=A0 the final value is
=A0= =A0=A0 //=20 sample(j) =3D 0xff * sin(freq * 2 * pi * j / 8000)
=A0=A0=A0 // so...= =A0=A0=A0=A0declaring=20 sample as a float
=A0=A0=A0 sample =3D 0xff * sin(freq * 2 * pi * j / 8= 000);
=A0=A0=A0=20 while(!update_dac){=A0=A0=A0 // where update_dac is set to 1 in the timer i= nterrupt=20 handler
=A0=A0=A0 }
=A0=A0=A0 update_ dac =3D 0;
=A0=A0=A0 send= _dac((UINT8) sample);=20
}=A0
=A0
When I execute the code and step through it the value f= or sample=A0when j =3D 1 is 180.238where I expect 3.146
=A0
=
Can someone see my error or what is going on?
=A0
= Thanks
--089e010d97303689d404eed98910--