delorie.com/archives/browse.cgi   search  
Mail Archives: geda-user/2013/12/31/15:23:41

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:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=cmEndJ4ylB6G3OdwY/I1xbK1Stcpkm8HDAZhNIBEv64=;
b=OH34lloCTp/tUplK/nqGCWjaXdEvDxYYHpAzxbV3SIZKgUlRmEUfkAqF/7NBcyoJlK
iKhnjZDl5ocSe8C861TUbpQ0km6bysCkVItWlD1o1y7W/drMW41/r709GzcvBbEV+LKs
zkaZwNGG6NHtiVSx1PeEaBOiA7FgtBAnZzn7DdzK+ywUsohUmXVJ4qeTV5MA/v+BZo1d
UYCB+4tjM5HH2sDe8YaEaVwj2TFm0xKp1a3w0Ua657RNMNQdDw/SicYYheirxEPm8+r5
pqiZPRNfgT5oHOo9dGMDcSuDWkINm4VvPRnEiQ7M+6mGrWM4XVACyVcR4vmCZDDDKb1j
TOrw==
MIME-Version: 1.0
X-Received: by 10.52.179.105 with SMTP id df9mr1536671vdc.83.1388521376201;
Tue, 31 Dec 2013 12:22:56 -0800 (PST)
In-Reply-To: <CAM4zO3pAfPZ5aSWwwaykcXtB=Ruyxme8a1ZSGb46XLQfFY-FCw@mail.gmail.com>
References: <CALSZ9go-H-4HnAfvdQyLsVEcbNG0pT33wEM=G-CfZmDXVcMQrw AT mail DOT gmail DOT com>
<CAM4zO3pAfPZ5aSWwwaykcXtB=Ruyxme8a1ZSGb46XLQfFY-FCw AT mail DOT gmail DOT com>
Date: Tue, 31 Dec 2013 15:22:56 -0500
Message-ID: <CALSZ9gqm6+rD_A6agdO5TH2yE2xNGj-26bxPXao19JT5OoD98Q@mail.gmail.com>
Subject: Re: [geda-user] Help sending a sine wave to a speaker
From: Rob Butts <r DOT butts2 AT gmail DOT com>
To: geda-user AT delorie DOT com
Reply-To: geda-user AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: geda-user AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

--bcaec517234d64407b04eeda5335
Content-Type: text/plain; charset=ISO-8859-1

Thanks Bill, since posting I realized that is my problem.


On Tue, Dec 31, 2013 at 2:59 PM, Ozzy Lash <ozzy DOT lash AT gmail DOT com> wrote:

> On Tue, Dec 31, 2013 at 1:26 PM, Rob Butts <r DOT butts2 AT gmail DOT com> wrote:
>
>> 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
>>
>
> If freq=1000 and j=1, then sample is 255*sin(pi/4).  sin(pi/4)=sqrt(2)/2,
> so sample should be about 180
>
> Did you do the sin on a calculator and have it in degrees mode instead of
> radians?
>
> Bill
>

--bcaec517234d64407b04eeda5335
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thanks Bill, since posting I realized that is my problem.<=
/div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Tue, =
Dec 31, 2013 at 2:59 PM, Ozzy Lash <span dir=3D"ltr">&lt;<a href=3D"mailto:=
ozzy DOT lash AT gmail DOT com" target=3D"_blank">ozzy DOT lash AT gmail DOT com</a>&gt;</span> w=
rote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><div class=3D"h5"><div=
 class=3D"gmail_extra"><div class=3D"gmail_quote">On Tue, Dec 31, 2013 at 1=
:26 PM, Rob Butts <span dir=3D"ltr">&lt;<a href=3D"mailto:r DOT butts2 AT gmail DOT co=
m" target=3D"_blank">r DOT butts2 AT gmail DOT com</a>&gt;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;padding=
-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-l=
eft-style:solid"><div dir=3D"ltr"><div>It has been a while since I&#39;ve w=
ritten major C code so I&#39;m having a hard time=20
coming up with the code. <br></div><div>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<br>=A0=A0=A0 <br>Can anyone=A0check my thoughts=
 below?=A0 Here&#39;s my thinking... <br>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: <br>=A0 <br>for(j=3D0;=
=A0j &lt;=20
80000; j++){ <br>=A0=A0=A0=A0 //=A0j is the sample index <br>=A0=A0=A0 // s=
traight sine function=20
means one cycle per 2*pi samples <br>=A0=A0=A0 //=A0sample(j) =3D sin(j) <b=
r>=A0=A0=A0 //=20
multiply by 2*pi now it is one cycle per sample <br>=A0=A0=A0 // sample(j) =
=3D sin((2 *=20
pi) * j) <br>=A0=A0=A0 // multiply by 1,000 samples per second now it is cy=
cles per=20
second <br>=A0=A0=A0 // sample(j) =3D sin(1000 * 2 * pi * j) <br>=A0=A0=A0 =
// divide by 8000=20
samples per second now it is cycles per 8000 samples <br>=A0=A0=A0 // sampl=
e(j) =3D=20
sin(1000 * 2 * pi * j / 8000) <br>=A0=A0=A0 // the 1000 cycles is the frequ=
ency so in=20
those terms <br>=A0=A0=A0 //=A0 sample(j) =3D sin(freq * 2 * pi * j / 8000)=
 <br>=A0=A0=A0 // now=20
if I want to normalize the amplitude to 0xff=A0 the final value is <br>=A0=
=A0=A0 //=20
sample(j) =3D 0xff * sin(freq * 2 * pi * j / 8000) <br>=A0=A0=A0 // so...=
=A0=A0=A0=A0declaring=20
sample as a float <br>=A0=A0=A0 sample =3D 0xff * sin(freq * 2 * pi * j / 8=
000); <br>=A0=A0=A0=20
while(!update_dac){=A0=A0=A0 // where update_dac is set to 1 in the timer i=
nterrupt=20
handler <br>=A0=A0=A0 } <br>=A0=A0=A0 update_ dac =3D 0; <br>=A0=A0=A0 send=
_dac((UINT8) sample);=20
<br>}=A0<br>=A0 <br>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</div><div>=A0</div>=
<div>Can someone see my error or what is going on?</div><div>=A0</div><div>=
Thanks</div>


</div>
</blockquote></div><br></div></div></div><div class=3D"gmail_extra">If freq=
=3D1000 and j=3D1, then sample is 255*sin(pi/4).=A0 sin(pi/4)=3Dsqrt(2)/2, =
so sample should be about 180<br><br></div><div class=3D"gmail_extra">Did y=
ou do the sin on a calculator and have it in degrees mode instead of radian=
s?<br>

<br></div><div class=3D"gmail_extra">Bill<br></div></div>
</blockquote></div><br></div>

--bcaec517234d64407b04eeda5335--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019