cancel
Showing results for 
Search instead for 
Did you mean: 

program dac & dac nucleo

meriarajagukguk
Associate III

i have a code like this :

but i iwant to ask why result signal in my code freq max is 1,5k and minim 200 hz bu while i want to freq max is 200hz and freq minim is 0 hz, and why while value adc under 100, freq in osiloscope lost?

main(void):

//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
int AD_RES[ARRAY_SIZE];
 
//MAPP
//OUTPUT = DAC
//INPUT = ADC
int input_start = 0;
int input_end = 4095;
int output_start =100;
int output_end = 1580;
int angka_test;
int i;
int output;
int y;
int 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
int 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);
      }
 
//MAP
      angka_test = AD_RES[0]; 
      output = output_start + ((output_end - output_start) * (angka_test - input_start)) / (input_end - input_start);
__HAL_TIM_SET_AUTORELOAD(&htim2, output);
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, output);
HAL_Delay(1000);
y = output;
x = angka_test;
  }
 
  /* USER CODE END 3 */
}
1 REPLY 1
Andrew Neil
Evangelist III

Please don't start yet another thread before you've dealt with all the previous ones.

Only an hour ago:

https://community.st.com/t5/stm32cubemx-mcus/sine-wave-dac-in-stm32cubemx/td-p/705324

And please see the posting tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228