cancel
Showing results for 
Search instead for 
Did you mean: 

Modification/Clarification of Chirp Signal Code

Warzeeeee
Associate

Hello, 
first of all, I hope im at the right section for my question. If not I'm sorry.

I'm relativ new in working with Nucleo Boards and currently working on a Project where i want to generate a chirp signal/sinus sweep from -20kHz up to 20kHz.I made myself the following table in excel to have all the values.

 

/* USER CODE BEGIN 0 */
uint32_t sweep_val[4096] = {4095, 2578, 277, 590, 3051, 4033, 2097, 88, 947, 3413, 3886, 1682, 9, 1293, 3668, 3699, 1347, 4, 1598, 3836, 3511, 1093, 38, 1845, 3939, 3347, 914, 84, 2025, 3996, 3226, 802, 123, 2135, 4023, 3158,
		748, 142, 2175, 4030, 3147, 749, 137, 2146, 4019, 3195, 805, 108, 2047, 3985, 3298, 920, 64, 1878, 3919, 3448, 1102, 20, 1642, 3802, 3630, 1359, 0, 1345, 3615, 3821, 1697, 33, 1004, 3336, 3988, 2115, 155, 646, 2948, 4086,
		2598, 403, 317, 2445, 4063, 3110, 805, 78, 1846, 3868, 3590, 1369, 1, 1201, 3459, 3952, 2069, 160, 595, 2829, 4095, 2832, 603, 151, 2025, 3925, 3530, 1327, 1, 1160, 3393, 3997, 2248, 258, 419, 2532, 4063, 3186, 952, 21,
		1496, 3622, 3886, 1981, 155, 552, 2700, 4083, 3090, 883, 30, 1516, 3615, 3903, 2053, 197, 464, 2543, 4054, 3277, 1102, 1, 1217, 3371, 4030, 2459, 426, 210, 2047, 3883, 3675, 1660, 75, 671, 2784, 4085, 3147, 1005, 4, 1231,
		3346, 4049, 2582, 538, 123, 1778, 3721, 3869, 2069, 247, 341, 2246, 3939, 3631, 1653, 91, 583, 2608, 4045, 3398, 1347, 24, 796, 2860, 4085, 3213, 1146, 3, 947, 3011, 4094, 3101, 1043, 0, 1018, 3070, 4095, 3072, 1028, 0, 1004,
		3040, 4094, 3129, 1102, 3, 905, 2921, 4085, 3268, 1269, 24, 731, 2704, 4045, 3472, 1539, 91, 504, 2379, 3939, 3712, 1919, 247, 263, 1945, 3721, 3939, 2403, 538, 68, 1419, 3346, 4081, 2958, 1005, 1, 853, 2784, 4048, 3511,
		1660, 158, 344, 2047, 3749, 3941, 2459, 615, 32, 1217, 3127, 4093, 3277, 1394, 79, 464, 2213, 3817, 3903, 2403, 600, 30, 1170, 3051, 4083, 3401, 1583, 155, 308, 1908, 3622, 4030, 2801, 952, 4, 731, 2532, 3936, 3804, 2248,
		531, 38, 1160, 2992, 4067, 3530, 1814, 285, 151, 1512, 3292, 4095, 3295, 1522, 160, 263, 1745, 3459, 4085, 3147, 1369, 110, 325, 1846, 3517, 4078, 3110, 1347, 109, 317, 1812, 3479, 4086, 3189, 1454, 155, 241, 1642, 3336,
		4094, 3373, 1697, 275, 123, 1345, 3066, 4061, 3630, 2082, 511, 20, 947, 2640, 3919, 3899, 2598, 920, 18, 507, 2047, 3585, 4078, 3195, 1539, 226, 137, 1324, 2992, 4030, 3753, 2349, 748, 2, 595, 2135, 3615, 4077, 3226, 1616,
		285, 84, 1136, 2767, 3939, 3913, 2710, 1093, 75, 290, 1598, 3183, 4063, 3699, 2320, 775, 9, 485, 1908, 3413, 4093, 3542, 2097, 624, 0, 590, 2041, 3494, 4095, 3496, 2053, 608, 0, 575, 1997, 3446, 4093, 3573, 2186, 723, 9,
		442, 1774, 3257, 4063, 3750, 2496, 996, 75, 235, 1384, 2891, 3939, 3964, 2958, 1465, 285, 45, 868, 2313, 3615, 4093, 3499, 2149, 748, 22, 341, 1537, 2992, 3963, 3957, 2984, 1539, 352, 16, 696, 2047, 3396, 4076, 3756, 2598,
		1173, 175, 87, 947, 2335, 3583, 4094, 3630, 2415, 1028, 123, 123, 1022, 2397, 3607, 4094, 3638, 2452, 1083, 155, 87, 905, 2235, 3479, 4081, 3777, 2706, 1347, 297, 16, 622, 1846, 3155, 3984, 3979, 3147, 1848, 635, 22, 263,
		1252, 2572, 3671, 4095, 3672, 2582, 1275, 285, 13, 564, 1712, 2992, 3893, 4056, 3423, 2248, 996, 158, 58, 731, 1912, 3142, 3951, 4030, 3356, 2186, 967, 155, 53, 693, 1834, 3051, 3897, 4064, 3496, 2403, 1183, 277, 6, 464,
		1483, 2700, 3684, 4093, 3786, 2877, 1682, 615, 41, 153, 908, 2047, 3183, 3936, 4058, 3511, 2481, 1310, 384, 1, 284, 1136, 2280, 3346, 3996, 4026, 3431, 2403, 1265, 373, 3, 263, 1093, 2200, 3268, 3976, 4055, 3530, 2552,
		1428, 464, 13, 210, 947, 1936, 2992, 3760, 4093, 3842, 3090, 1981, 805, 109, 84, 680, 1712, 2737, 3642, 4095, 3913, 3298, 2224, 1043, 181, 18, 469, 1384, 2459, 3401, 3991, 4086, 3712, 2832, 1640, 667, 68, 45, 579, 1410,
		2380, 3292, 3929, 4093, 3875, 3213, 2224, 1083, 233, 9, 308, 887, 1580, 2413, 3195, 3801, 4093, 3997, 3496, 2710, 1733, 769, 137, 22, 419, 1067, 1693, 2413, 3091, 3671, 4093, 4049, 3573, 2860, 1931, 970, 197, 53, 344,
		910, 1454, 2025, 2582, 3080, 3480, 3750, 3886, 3884, 3750, 3481, 3080, 2581, 2026, 1454, 911, 344, 53, 197, 970, 1931, 2860, 3573, 4048, 4095, 3671, 3091, 2413, 1693, 1067, 419, 21, 137, 769, 1733, 2710, 3496, 3997, 4093,
		3801, 3195, 2413, 1580, 887, 308, 9, 233, 1083, 2224, 3213, 3875, 4093, 3929, 3292, 2380, 1410, 579, 45, 68, 667, 1640, 2832, 3712, 4086, 3991, 3401, 2459, 1384, 469, 18, 181, 1043, 2224, 3298, 3913, 4095, 3642, 2737,
		1712, 680, 84, 109, 805, 1981, 3090, 3842, 4093, 3760, 2992, 1936, 947, 210, 13, 464, 1428, 2552, 3530, 4055, 3976, 3268, 2200, 1093, 263, 3, 373, 1265, 2403, 3431, 4026, 3996, 3346, 2280, 1136, 284, 1, 384, 1310, 2481,
		3511, 4058, 3936, 3183, 2047, 908, 153, 41, 615, 1682, 2877, 3786, 4093, 3684, 2700, 1483, 464, 6, 277, 1183, 2403, 3496, 4064, 3897, 3051, 1834, 693, 53, 155, 967, 2186, 3356, 4030, 3951, 3142, 1912, 731, 58, 158, 996,
		2248, 3423, 4056, 3893, 2992, 1712, 564, 13, 285, 1275, 2582, 3672, 4095, 3671, 2572, 1252, 263, 22, 635, 1848, 3147, 3979, 3984, 3155, 1846, 622, 16, 297, 1347, 2706, 3777, 4081, 3479, 2235, 905, 87, 155, 1083, 2452, 3638,
		4094, 3607, 2397, 1022, 123, 123, 1028, 2415, 3630, 4094, 3583, 2335, 947, 87, 175, 1173, 2598, 3756, 4076, 3396, 2047, 696, 16, 352, 1539, 2984, 3957, 3963, 2992, 1537, 341, 22, 748, 2149, 3499, 4093, 3615, 2313, 868,
		45, 285, 1465, 2958, 3964, 3939, 2891, 1384, 235, 75, 996, 2496, 3750, 4063, 3257, 1774, 442, 9, 723, 2186, 3573, 4093, 3446, 1997, 575, 0, 608, 2053, 3496, 4095, 3494, 2041, 590, 0, 624, 2097, 3542, 4093, 3413, 1908,
		485, 9, 775, 2320, 3699, 4063, 3183, 1598, 290, 75, 1093, 2710, 3913, 3939, 2767, 1136, 84, 285, 1327, 3530, 3925, 2025, 151, 603, 2832, 4095, 2829, 595, 160, 2069, 3952, 3459, 1201, 1, 1369, 3590, 3868, 1846, 78, 805,
		3110, 4063, 2445, 317, 403, 2598, 4086, 2948, 646, 155, 2115, 3988, 3336, 1004, 33, 1697, 3821, 3615, 1345, 0, 1359, 3630, 3802, 1642, 20, 1102, 3448, 3919, 1878, 64, 920, 3298, 3985, 2047, 108, 805, 3195, 4019, 2146, 137,
		749, 3147, 4030, 2175, 142, 748, 3158, 4023, 2135, 123, 802, 3226, 3996, 2025, 84, 914, 3347, 3939, 1845, 38, 1093, 3511, 3836, 1598, 4, 1347, 3699, 3668, 1293, 9, 1682, 3886, 3413, 947, 88, 2097, 4033, 3051, 590, 277, 2578, 4095,
		2576, 271, 608, 3090, 4015, 1997, 53, 1102, 3573, 3746, 1356, 9, 1748, 3941, 3257, 731, 211, 2496, 4094, 2555, 235, 701, 3241, 3939, 1712, 4, 1465, 3831, 3421, 868, 156, 2403, 4093, 2572, 225, 748, 3321, 3881, 1537, 1, 1715, 3957,
		3155, 583, 352, 2836, 4055, 2047, 39, 1269, 3756, 3479, 881, 175, 2513, 4093, 2335, 106, 1054, 3630, 3607, 1018, 123, 2403, 4094, 2397, 119, 1043, 3638, 3583, 965, 155, 2513, 4090, 2235, 64, 1234, 3777, 3396, 731, 297, 2836,
		4033, 1846, 2, 1653, 3979, 2992, 382, 635, 3321, 3809, 1252, 71, 2316, 4095, 2313, 68, 1275, 3831, 3266, 564, 465, 3147, 3893, 1384, 46, 2248, 4094, 2309, 58, 1347, 3884, 3142, 442, 615, 3356, 3746, 1081, 155, 2643, 4054, 1834, 0, 1881, 4064, 2576, 123
};

 

  The rest of the code is relative easy:

 

/* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim5);

  HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, sweep_val, 4096, DAC_ALIGN_12B_R);
  HAL_Delay(4700);

  /* USER CODE END 2 */

 

With a Clock Frequency of 100MHz, a Prescaler of 17-1 and a Counter Period of 100-1 i got a pulse length of ~20ms which is exactly what i need. The next step i want to achieve is a 80ms pause between each pulse. No matter what i write into the HAL_delay() the pause inbetween pulses wont change. Even without the HAL_delay it is ~50ms.

Screenshot 2024-08-01 133717.pngScreenshot 2024-08-01 133819.png

 

can anyone explain me what im doing wrong, or even gives me a hint, how to achieve this much easier? 

Like i said in the beginning im new with Nucleos and also couldnt find any helpful information about sweeps in the internet.

Thanks

3 REPLIES 3
TDK
Guru

A few things:

  • DAC array should probably be uint16_t, not uint32_t, and with DMA set up as half-word width. Just wasting space otherwise, if it even works. Make the array const so it stays in FLASH. no need to dedicate RAM for a static array.
  • Are you triggering the DAC with a timer TRGO signal? You don't say or show it, but seems like you are.
  • Is DMA set to circular? Seems like it is, in which case the signal will repeat infinitely. It should be set to normal if you want to pause after each burst. Set the last value in the array to the value you want the DAC at during this delay.

> With a Clock Frequency of 100MHz, a Prescaler of 17-1 and a Counter Period of 100-1 i got a pulse length of ~20ms which is exactly what i need.

Not sure your math works out.

100 MHz / 17 / 100 / 4096 = 14.3 Hz which is a period of 70 ms, not 20 ms.

 

Showing the full code would help.

If you feel a post has answered your question, please click "Accept as Solution".

Hey thanks for your detailed help.


  • DAC array should probably be uint16_t, not uint32_t, and with DMA set up as half-word width. Just wasting space otherwise, if it even works. Make the array const so it stays in FLASH. no need to dedicate RAM for a static array.

I made the changes you suggested in your first point.

  • Are you triggering the DAC with a timer TRGO signal? You don't say or show it, but seems like you are.

Im using the code i already postet in my question for triggering the DAC, i will post you my entire main function:

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_DAC_Init();
  MX_TIM5_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim5);

  HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, sweep_val, 4096, DAC_ALIGN_12B_R);
  HAL_Delay(4700);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

  • Is DMA set to circular? Seems like it is, in which case the signal will repeat infinitely. It should be set to normal if you want to pause after each burst. Set the last value in the array to the value you want the DAC at during this delay.

> With a Clock Frequency of 100MHz, a Prescaler of 17-1 and a Counter Period of 100-1 i got a pulse length of ~20ms which is exactly what i need.

Not sure your math works out.

100 MHz / 17 / 100 / 4096 = 14.3 Hz which is a period of 70 ms, not 20 ms.


Yes its on circular. And if you add the pause between the signals up you have 20ms for signal, 50ms pause adds up to 70ms period. Currently i have no clue where the pause comes from. Which is also the reason why i made this post. I've tried to extend the pause with a delay, but it wont change anything.

 

 

> I've tried to extend the pause with a delay, but it wont change anything.

Because it's in circular mode. It never stops. Don't use circular mode if you want a delay between pulses, as stated in my last post.

Your HAL_Delay function only executes once. HAL_DAC_Start_DMA only gets called once. Is that what you want? Change it to normal mode, move both of these functions to within the main loop.

If you feel a post has answered your question, please click "Accept as Solution".