Search code examples
mbeddaclpcxpresso

mbed generate chirp singal


I am using LPCXpresso1549 to generate chirp signal with frequency between 35000 Hz and 45000 Hz. First off, I generate DAC chirp samples on matlab, and store them in const uint16_t chirpData[]. The samples frequency is 96000 Hz, therefore 96001 samples. Then, I set the timer to send samples out one by one every (1/96000) second. However, my signal I got having frequency between 3200 Hz to 44000 Hz. Is that because the timer is slow?

enter code here
const uint16_t chirpData[NUM_SAMPLES] = { 2048, ...., 1728, 2048} //96001 sampels

 #include "mbed.h"
 #include "chirp.h"

 Serial pc(USBTX, USBRX);
 Timer t;
 AnalogOut aout(P0_12); 

 int main()
 {   
     int i = 0;

     while(true) {
         // Write the sample to the analog

         t.start(); //start timer
         if(t.read() >= 0.00001){ // 1/samplef = 0.00001
              aout.write_u16(chirpData[i]);

              i++;
              t.reset(); // reset timer to zero

              if(i > 96000) {
                  i = 0;
              }
          }
     }
 }

Solution

  • In this case I recommend you using Threads executing Tasks by:

    #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
    xTaskHandle taskHandle;
    xTaskCreate(..); //Check Task.h
    

    Then you can set your task cycle by

    void your_task() {
      unsigned task_cycle_ms = 1/freq; //Careful, convert to ms.
      portTickType xLastWakeTime = 0;
      portTickType xFrequency = task_cycle_ms/portTICK_RATE_MS;
      for(;;) {
        vTaskDelayUntil( &xLastWakeTime, xFrequency );
        //your code to execute every cycle here
      }
    }