cancel
Showing results for 
Search instead for 
Did you mean: 

SINE WAVE DAC IN STM32CUBEMX

meriarajagukguk
Associate III

I have a DAC signal from an ADC (potentiometer). I have set the frequency from 0-200 Hz but when I run it when I am at 200 and the sign should still be there, the signal on the oscilloscope disappears (just a straight line). does anyone know how to fix it?

1 ACCEPTED SOLUTION

Accepted Solutions

Simply your code set this ...

 hasil = (output_end - output_start) / (input_end - input_start) * (input - input_start) + output_start;
__HAL_TIM_SET_AUTORELOAD(&htim2, hasil);	

Back to your idea 0-200Hz is impossible. 0 is stopped system. First lowest freq is based on timer range ...

Then better say 2-200Hz and try understand what you do. You dont show full code then hard to help.

View solution in original post

10 REPLIES 10
Andrew Neil
Evangelist III

You've posted this so many times!

 


@meriarajagukguk wrote:

I have set the frequency from 0-200 Hz 


What is this "frequency", exactly? Frequency of what ?

How, exactly, have you set it?

What does a potentiometer have to do with frequency?

 


@meriarajagukguk wrote:

 when I run it when I am at 200 and the sign should still be there


What on earth does that mean?

 


@meriarajagukguk wrote:

 the signal on the oscilloscope disappears (just a straight line). 


Show some example oscilloscope traces.

Use your scope's screenshot facility - not photographs of the screen!

 


@meriarajagukguk wrote:

does anyone know how to fix it?


From this description, no!

Perhaps provide a diagram of your system, showing what you expect it to do, and how you expect it to perform?

 

You still haven't provided a clear description of your goal here - what you're actually trying to achieve.

Forget about STM32, DACs, ADCs, etc. Just consider your system as a "Black Box". Tell us what you expect to be going into that system, and what you need to come out of that system in response.

In other words, describe the requirement - not your attempt at implementation.

https://sketchplanations.com/the-xy-problem

https://xyproblem.info/

http://www.catb.org/~esr/faqs/smart-questions.html#goal


@Andrew Neil wrote:

Forget about STM32, DACs, ADCs, etc. Just consider your system as a "Black Box". Tell us what you expect to be going into that system, and what you need to come out of that system in response.


Something like this:

 


                      +------------+
What                  |            |
        ------------->+            +------------->
Goes                  |    Your    |                 What
        ------------->+            +------------->
In                    |   System   |                 Comes
        ------------->+            +------------->
                      |            |                 Out
                      +------------+

 

meriarajagukguk
Associate III

i post in my new question

Please don't keep doing that - you already have so many threads on this!

You really need to step back, and think about what you're actually trying to achieve:

 

@Andrew Neil wrote:

Forget about STM32, DACs, ADCs, etc. Just consider your system as a "Black Box". Tell us what you expect to be going into that system, and what you need to come out of that system in response.


meriarajagukguk
Associate III

hello I'm a beginner, sorry if I always ask.
If I have code like the one below with mapping the goal is to produce a 200hz signal on the oscilloscope. But why is the value of 'hasil' always the same as the value of uint16_t output_end = 2000;
and if I have got the frequency I want, the signal on the oscilloscope will sometimes disappear when I turn the potentiometer. is there a loop or wrong code in my code? please help. and I've read about ARR, well ARR produces a value that corresponds to the 'hasil' in the watch window, can anyone explain simply what it is. I've studied but I can't conclude it.

Spoiler
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */


//DAC
#define BUFFER_SIZE 225;
uint32_t Wave_LUT[225] = {
		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,
		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, 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, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 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, 189,
    234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258,
    1353, 1449, 1546, 1645, 1745, 1845, 1946, 1950, 1965, 1975, 1990, 1999, 2010,2022, 2047
};
#define ARRAY_SIZE 4
uint32_t AD_RES[ARRAY_SIZE];

//MAPP
//OUTPUT = DAC
//INPUT = ADC
uint16_t input_start = 0;
uint16_t input_end = 4095;
uint16_t output_start = 100;
uint16_t output_end = 2000;
uint16_t input ;
uint16_t i;
uint16_t hasil;
uint16_t y;
uint16_t x;

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

DAC_HandleTypeDef hdac1;
DMA_HandleTypeDef hdma_dac1_ch1;

TIM_HandleTypeDef htim2;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

//ADC
uint16_t i = 0;
ADC_ChannelConfTypeDef ADC_CH_Cfg = {0};
uint32_t ADC_Channels[4] = {ADC_CHANNEL_6, ADC_CHANNEL_7, ADC_CHANNEL_8, ADC_CHANNEL_9};
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_DAC1_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_ADC1_Init();
  MX_TIM2_Init();
  MX_USART2_UART_Init();
  MX_DAC1_Init();
  /* USER CODE BEGIN 2 */

		
	//ADC
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
	ADC_ChannelConfTypeDef ADC_CH_Cfg = {0};
	ADC_CH_Cfg.Rank =  ADC_REGULAR_RANK_1;
  ADC_CH_Cfg.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
	
	//DAC
	HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*)Wave_LUT, 225, DAC_ALIGN_12B_R);
	HAL_TIM_Base_Start(&htim2);

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
		
    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);
    }
			input = AD_RES[0]; 
			//output = output_start + ((output_end - output_start) * (input - input_start)) / (input_end - input_start);	
			 hasil = (output_end - output_start) / (input_end - input_start) * (input - input_start) + output_start;
			__HAL_TIM_SET_AUTORELOAD(&htim2, hasil);	
			HAL_Delay(500);
			y = hasil;
			x = input;
	}

  /* USER CODE END 3 */
		
}

@meriarajagukguk wrote:

hello I'm a beginner, sorry if I always ask.


So don't keep doing it!

Please follow the advice already given - don't just ignore it and start yet another post which still makes no sense.

Again:


@Andrew Neil wrote:

You really need to step back, and think about what you're actually trying to achieve:

@Andrew Neil wrote:

Forget about STM32, DACs, ADCs, etc. Just consider your system as a "Black Box". Tell us what you expect to be going into that system, and what you need to come out of that system in response.



 

I’ve already tried to explain it, and I’ve found the answer to the previous question, so please don’t be upset. Now, I want to ask about the relationship between CNT and the frequency on the oscilloscope. Why does the CNT value on the timer reach millions when the oscilloscope output is a straight line, but when it outputs a sine wave, the CNT value is below 15? What influences the CNT?

Would you be willing to explain this gladly?

Simply your code set this ...

 hasil = (output_end - output_start) / (input_end - input_start) * (input - input_start) + output_start;
__HAL_TIM_SET_AUTORELOAD(&htim2, hasil);	

Back to your idea 0-200Hz is impossible. 0 is stopped system. First lowest freq is based on timer range ...

Then better say 2-200Hz and try understand what you do. You dont show full code then hard to help.