Phase Accumulator DDS

#include "mbed.h"

 

DigitalOut myled(LED1);

 

DigitalIn pushButton(p23);

DigitalIn rotaryA(p21);

DigitalIn rotaryB(p22);

 

AnalogIn pressure(p20);

AnalogIn pos1(p17);

AnalogIn pos2(p16);

 

AnalogOut signal(p18);

 

Ticker sampleClock;

// 16 entry sinewave table.

char sineWave[]={127,175,216,244,255,244,216,175,127,78,37,9,0,9,36,78};

 

char sawWave[]={0,16,31,47,64,79,95,111,127,143,159,175,191,207,223,255};

 

unsigned int counter;

unsigned  int counterSum;

unsigned int counterLimit = 2147483647; //31 bits  2^31 - 1

 

char temp;

 

float byteToFloat(char in){

float i = in;

return (i/255.0);

}

 

void sample2(){

 

 

 

}

 

void sample(){

 

//useing a 31 bit counter.

counter = counter + counterSum;

if(counter > counterLimit){

//wrap the counting.

counter = counter - counterLimit;

}

 

//Fout = CounterSum*Fclk/MaxCount

 

temp = counter>>27;  //shift the top 4 bits down.

//float x = temp;

//signal.write(x/16.0);

 

//write the output out

signal.write(byteToFloat(sineWave[temp]));

}

 

int read_encoder()

{

int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};

static char old_AB = 0;

/**/

old_AB <<= 2;

char x  = rotaryA.read();

x<<=1;

x = x+rotaryB.read();                 //remember previous state

old_AB |= ( x & 0x03 );  //add current state

return ( enc_states[( old_AB & 0x0f )]);

}

 

int main() {

 

//setup variables.

counter = 0;

counterSum = 302365697;

counterSum = 18897856;

//counterSum = 1181116;

//18897856.1

 

//hook up sample() to the ticker.  run the counter at 50kHz

sampleClock.attach_us(sample,20);

 

while(1){

myled=1;

wait(0.5);

myled.write(0);

wait(0.5);

}

}


0 comments

You need to log in to post a comment