Meria Rajagukguk
Meria Rajagukguk

Reputation: 11

DAC freq LOST and signal in osciloscop is unstable, used STM32 NUCLEOF334R8

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.

`#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 WaveLUT [] = {
        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 */
        
}`

I have tried and it produces an unstable signal and the value of 'hasil' follows the start output.

Upvotes: 0

Views: 76

Answers (0)

Related Questions