Meria Rajagukguk
Meria Rajagukguk

Reputation: 11

How to create a for loop program that can change middle values flexibly?

I am a beginner. Just yesterday, I asked about how to create a flexible signal output. I found an answer and received a lot of input as well. My solution was very simple, using just one for loop, which I found to be very helpful. However, now I’m facing a problem—my program is NOT FLEXIBLE.

Here is my program:

#define size 300
uint16_t Value[size] = 
{
2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,

2100, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355,
3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076,
4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730,
3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500,
2400, 2300, 2199, 2098,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // i = 124; i < 124; i++  length 95
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //i = 124; i < 140; i++ length  115
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // i = 124; i < 200; length 131
    
1997, 1896, 1795, 1695, 1595, 1497,1305, 1212, 1120, 1031, 944, 860, 779, 701, 627,
556, 488, 424, 365, 309, 258, 211, 168, 130, 97, 69, 45, 26, 13,4, 0, 1, 8, 19, 35, 56, 82, 
113, 149, 200, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258, 
1353, 1449, 1546, 1645, 1745, 1845, 1947, 1965, 1990, 2010, 2022, 2047
};

  for (uint32_t i = 124; i < 200; i++)
    {
        Value[i] = newArray;
    }   
  HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)Value, 131, DAC_ALIGN_12B_R); //115
  HAL_TIM_Base_Start(&htim4);

 while (1)
  {             
    for(i=0; i<4; i++)
    {
     //ADC
      ADC_CH_Cfg.Channel = ADC_Channels[i];       
      HAL_ADC_ConfigChannel(&hadc1, &ADC_CH_Cfg); 
      HAL_ADC_Start(&hadc1);
      if(HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK) 
                {
          AD_RES[i] = HAL_ADC_GetValue(&hadc1);  
        }
         HAL_ADC_Stop(&hadc1); 
    }
}
    

I’m using ADC to DAC, but you don't need to look at the ADC part—just focus on the DAC. With that many arrays, I want it so that when I change one parameter, maybe from the number of i or from the length in HAL_Start_DMA or whatever it is, the shape of the signal produced changes. Because when I use the loop above, what happens is that I have to change the size of i, change the length in HAL, and add many '0' values to produce the signal I want. But that’s not what I want—it's just like inputting each value one by one, even though I’m using a for loop to simplify the process. Do you have any suggestions for a loop that simple so that I can understand? Also, if you have examples using if or while, that’s okay too. I’ve been tinkering with this all day, and it's really frustrating me.

If I use the program above, I have to set the x value in the image manually, which is very unsatisfactory.

enter image description here

Upvotes: 0

Views: 36

Answers (1)

Sid Pethe
Sid Pethe

Reputation: 59

Why don't you try thinking about the problem a different way? (I'm going to assume you're a student trying to learn embedded systems) What you're really after is a way to insert an extendible hold time between two DAC outputs (Think of it as a coincidence that they are mirrors of each other).

  • Two arrays for the two halves of the waveform and a delay in between that stops the DAC. Look into HAL_Delay() or the HAL_GetTicks() functions for easy to implement options.
  • Or you could just declare a very large array and change what parts of it you write to

If however, you're set on doing it using a flexible array. This is what you're after.

uint16_t value[10000] = {}
uint16_t firstHalf[] ={2048, 2048,2048}; // Put the first half here
uint16_t secondHalf[] ={2048, 2048,2048}; // Put the second half here
unsigned int hold_samples = 100; // this is your x value
int totalSize = sizeof(firstHalf) + hold_samples + sizeof(secondHalf);

int index = 0;
int n_firstHalf = (sizeof(firstHalf)/sizeof(uint16_t));
for(int i=0; i<n_firstHalf; i++){
    value[index] = firstHalf[i];
    index++;
}

for(int i=0; i<hold_samples; i++){
    value[index] = 0;
    index++;
}

int n_secondHalf = (sizeof(secondHalf)/sizeof(uint16_t));
for(int i=0; i<n_secondHalf; i++){
    value[index] = secondHalf[i];
    index++;
}

Upvotes: 0

Related Questions