skip to Main Content

I want to create a flexible signal shape that can be viewed using an oscilloscope by making a program. I am using an array to form the signal, such as {2048, 4026, 2048, 0}, where 2048 represents the flat line I want to adjust, 4026 represents the peak, and 0 represents the valley. I want to adjust the value of 2048 flexibly; for example, if I input x=3, it should output {2048, 4026, 2048, 2048, 2048, 0}. However, I am confused about how to use the for loop or if statements because I want to place the value in the middle. I need help with my code; please provide suggestions or possibly an example code.

the result i want
That is the description of what I want to create, and here is my code.

The code like 2048 is the part that I want to make flexible.

#define NS 4
uint32_t Wave_LUT[NS] = {
2048,4026, 2048,0
};
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, Wave_LUT, 4, DAC_ALIGN_12B_R);
HAL_TIM_Base_Start(&htim4);

Please help me, I really can’t analogize this anymore.

1

Answers


  1. You did not show how NS and x are related. In this example I will assume that NS is always large enough to hold the resulting array.

    From your examples I get a few fixed values.
    Your array layout is always like this:

    • First value is 2048
    • Second value is 4026
    • x values are 2048
    • Last value is 0

    That means, you have in total x+3 elements in your array and all but 2 have the same value.

    Let’s fill the values in:

    #include <stdio.h>  // for perror
    #include <stdlib.h> // for exit
    
    #define NS 1000
    uint32_t Wave_LUT[NS];
    
    void Init_DAC_Array(size_t x)
    {
      if (x+3 > NS)
      {
        perror("x exceeds array size!");
        exit(1);
      }
    
      // For simplicity we just fill all the array up with 2048u.
      for (size_t i = 0u; i < x+3u; i++)
      {
        Wave_LUT[i] = 2048u;
      }
      Wave_LUT[1] = 4026u;
      Wave_LUT[x+2u] = 0u;
    }
    

    If you really want to save every nanosecond you could modify the filling part like this:

      Wave_LUT[0] = 2048u;
      Wave_LUT[1] = 4026u;
      for (size_t i = 2u; i < x+2u; i++)
      {
        Wave_LUT[i] = 2048u;
      }
      Wave_LUT[x+2u] = 0u;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search