IIR sine wave help

All technical discussions and projects around startKIT
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

IIR sine wave help

Post by JLS »

Hi all

Is possible help me with simple example how to generate 1bit pwm audio output with this IIR sine wave algo ?

Code: Select all


int main ( void )
{

   long A=0x7e66;
   long buf[3]={0,0x1209,0};
   int val;

    while(1)
    {

            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 128 + (buf[ 0 ] >> 8);

   }
}

Please help me with pwm audio output example.

Many thanks

Kamil


JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

This is my example but not working produce error message - " warning: `c_pwm' not used in two parallel statements "

Code: Select all


#include <platform.h>
#include <xs1.h>
#include "pwm.h"
#include <xscope.h>

#define SAMPLE_RATE 44100

on stdcore[0]: port audio = XS1_PORT_1A;

long A=0x7e66;
long buf[3]={0,0x1209,0};
int val;


void xscope_user_init( void )
{
    xscope_register( 1
        ,XSCOPE_CONTINUOUS ,"signal" ,XSCOPE_INT ,"n");
}

int main ( void )
{

    chan c_pwm;

    while(1)
    {

            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768+buf[ 0 ];

            xscope_int(0, val);

            c_pwm <: val;

            pwm_server(c_pwm, audio, SAMPLE_RATE);

            delay_microseconds (10);

    }

    return 0;
}


Please help me many thanks

Kamil
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

You need to use two threads (or logical cores) to seperate the pwm and sine tasks

Rewrite your main as a function sine and remove the pwn server :

Code: Select all

void sine (chanend pwm){
    while(1)
    {
            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768+buf[ 0 ];

            xscope_int(0, val);

            c_pwm <: val;

            delay_microseconds (10);
    }
}

Then create a new main with a par and your 2 tasks with a common channel between them :

Code: Select all

void main(void) {
  chan pwm;

  par {
    sine(pwm);
    pwm_server(pwm, audio, SAMPLE_RATE);
  }
}
I haven't checked the detail of your code..

regards
Al
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

Testing it and compile without errors but not working !
Attachments
Sine.zip
(39.63 KiB) Downloaded 247 times
Sine.zip
(39.63 KiB) Downloaded 247 times
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Is xscope showing the correct waveform?

regards
Al
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

yes sine algo working on xscope

but pwm not produce sound (this is same pwm like SID projects)
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Maybe try adjusting the relative rates of the tasks your sine rate could be higher than the PWM sample rate.

regards
Al
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

compiling project OK when run this project generate this warning - "xrun: Program received signal ET_ILLEGAL_RESOURCE, Resource exception.
0x00010210 in pwm_server (c_samples=2147549698, p_pwm_output=66048, pwm_frequency=44100) at ../src/pwm.xc:33"
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Can you paste your code please using the code tag

regards
Al
User avatar
infiniteimprobability
XCore Legend
Posts: 1126
Joined: Thu May 27, 2010 10:08 am
Contact:

Post by infiniteimprobability »

Looks like you are sending an int over but pwm is expecting a short..
Post Reply