8 years, 3 months ago.

Draw a Sine wave of 1Khz and sample it with 8KHz

I need to draw a sine on the osciloscope of 1 Khz and sample it with 8hz

Question relating to:

1 Answer

8 years, 3 months ago.

If you want a fixed rate and a fixed pattern it's easy. 8kHz samples an 1kHz sin gives us 8 points per cycle at 45 degrees spacing. If the sample rate was higher or you wanted a flexible frequency then it would make more sense to define an array to store the output values and have the program calculate the values on startup. As it is we only have a single value that isn't completely trivial and that's not exactly complicated to calculate so lets keep it simple and hard code the values and then step through them.

Ticker nextSample;
int sampleCount = 0;
AnalogOut aOut(p18);

// sin 45 = 1/sqrt(2). 
#define sin45 0.7071067812

void onTick(void) {
  float outputValue;
  switch (sampleCount) {
    case 0:
    case 4:
    default:
      outputValue= 0;
      break;
    case 1:
    case 3:
      outputValue= sin45;
      break;
    case 2:
      outputValue= 1;
    break;
    case 5:
    case 7:
      outputValue= -sin45;
      break;
    case 6:
      outputValue= -1;
      break;
    }
  aOut = 0.5+outputValue/2; // sin goes from -1 to +1, analog out goes from 0 to 1 so add scale and offset.
  sampleCount++;
  if (sampleCount>7)
    sampleCount = 0;
}

main () {
  nextSample(onTick, 1.0/8000);
  while (1) {
  }
}

thank you very much

posted by ruben marroig 21 Dec 2015