Go Down

Topic: Resampling (Read 62 times) previous topic - next topic

suraj_150890

Hello,

I need your help guys, I am sending a number on serial monitor and it will generate that amount of frequency on Digital Oscilloscope. I am referring this link of Arduino Due Waveform Generator
https://www.arduino.cc/en/Tutorial/DueSimpleWaveformGenerator

I have to modify this code using two method
1) Resampling, Linear Interpolation
2)Timer Interrupt

I have made certain modification, I guess my Code is not right.
Please find the attachment of my code.

Code: [Select]

/*Simple Waveform generator with Arduino Due

  * connect two push buttons to the digital pins 2 and 3
    with a 10 kilohm pulldown resistor to choose the waveform
    to send to the DAC0 and DAC1 channels
  * connect a 10 kilohm potentiometer to A0 to control the
    signal frequency

 Code Impelementation with AttachInterrupt Function
 */

#include "Waveforms.h"

#define oneHzSample 1000000/maxSamplesNum  // sample for the 1Hz signal expressed in microseconds
#define SAMPLE_RATE 44100.0
// Pin Connection
const int button0 = 2, button1 = 3;
volatile boolean state=LOW;
float number;

//Initialization of Wave
volatile int wave0 = 0, wave1 = 0;
int i = 0;
int sample;


void TC4_Handler() //Interrupt at 44.1KHz rate (every 22.6us)
{
 
  TC_GetStatus(TC1, 1); //Clear status to fire again the interrupt.
 
  analogWrite(DAC0, waveformsTable[wave0][i]);
 
  analogWrite(DAC1, waveformsTable[wave1][i]);

     
 
}


// Variables will change:

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int buttonState1;             // the current reading from the input pin
int lastButtonState1 = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 5;    // the debounce time; increase if the output flickers

long lastDebounceTime1 = 0;  // the last time the output pin was toggled
long debounceDelay1 = 5;    // the debounce time; increase if the output flickers

void setup() {
  analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
  analogReadResolution(12);   // set the analog input resolution to 12 bit
  pinMode(button0,INPUT);
  pinMode(button1,INPUT);
  pinMode(DAC0,OUTPUT);
  pinMode(DAC1,OUTPUT);
 


 //turn on the timer clock in the power management controller
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk(ID_TC4);
 
  //we want wavesel 01 with RC
  TC_Configure(TC1, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2);
  TC_SetRC(TC1, 1, 238); // sets <> 44.1 Khz interrupt rate
 
  TC_Start(TC1, 1);
 
  // enable timer interrupts on the timer
  TC1->TC_CHANNEL[1].TC_IER=TC_IER_CPCS;
  TC1->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;
 
  //Enable the interrupt in the nested vector interrupt controller
  //TC4_IRQn where 4 is the timer number * timer channels (3) + the channel
  //number (=(1*3)+1) for timer1 channel1
  NVIC_EnableIRQ(TC4_IRQn);
analogWrite(DAC0,0);
analogWrite(DAC1,0);
Serial.begin(9600);
}

void loop() {
  // Read the the potentiometer and map the value  between the maximum and the minimum sample available
  // 1 Hz is the minimum freq for the complete wave
  // 170 Hz is the maximum freq for the complete wave. Measured considering the loop and the analogRead() time

  sample = map(analogRead(A0), 0, 4095, 0, oneHzSample);
  sample = constrain(sample, 0, oneHzSample);


Serial.println("Enter a number: ");
while(Serial.available()==0){} 
number=Serial.parseFloat();
Serial.println(number);

//Mathematical calculation
 float j= number* 367.5; // 367.5 = 44100/120
 Serial.println(j);

 float k=j/120;
 Serial.println(k);

 
float l=k-int(k);
Serial.println(l);

  i=l*120;
 Serial.println(i);
 
  wave0Select();
  wave1Select();

 
  i++;
  if (i == maxSamplesNum)  // Reset the counter to repeat the wave
    i = 0;

 

//  delayMicroseconds(number);  // Hold the sample value for the sample time
}

// function hooked to the interrupt on digital pin 2
void wave0Select() {
   int reading = digitalRead(button0);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // Function call Wave0Select
      if (buttonState == HIGH) {
       wave0++;
       
       
      }
    }
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
 
  if(wave0 == 4)
    wave0 = 0;
}

// function hooked to the interrupt on digital pin 3
void wave1Select() {
  int reading1 = digitalRead(button1);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
  }

  if ((millis() - lastDebounceTime1) > debounceDelay1) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading1 != buttonState1) {
      buttonState1 = reading1;

      // Function call Wave0Select
      if (buttonState1 == HIGH) {
       wave1++;
      }
    }
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState1 = reading1;
  if(wave1 == 4)
    wave1 = 0;
}

 



Thanks in advance!!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy