2024-05-24 05:16 AM - edited 2024-05-24 05:32 AM
I am using STM32L496ZGT6P. I am using DAC along with DMA to generate voltage and frequency of my desired range. But I am unable to get hold of the frequency setting. Sysclk I set is 4Mhz. 20 is prescaler value, 4Mhz/20 = 200000.This is further divided by 10 which is time period(ARR) .I am supposed to get 20000 as output in DAC, which I am getting in UART because of the below function, but I am not getting that in CRO. in CRO I am getting 100Hz when I tap DAC output. I have cross checked the output of the DAC using multimeter also. I am getting 100Hz in multimeter. I am unable to figure it out where is the mistake. So any help in this regard is greatly appreciated.
uint32_t Get_Timer2_Frequency(void)
{
pclk1 = HAL_RCC_GetPCLK1Freq();
prescaler = ((RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos);
timer_clock = pclk1;
if (prescaler != 0) {
timer_clock = 2 * pclk1;
}
timer_prescaler = htim2.Instance->PSC;
period = htim2.Instance->ARR;
timer_frequency = timer_clock / ((timer_prescaler + 1)* (period + 1));
return timer_frequency;
}
Below is my Sysclkconfig function.
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
Below is my timer init function:
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 20-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 10-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
below is my main:
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_DAC1_Init();
MX_LPUART1_UART_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim2);
gen_sine(array, OUTPUT_2_5);
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*) array, NP,DAC_ALIGN_12B_R);
data = Get_Timer2_Frequency();
memset((uint8_t*) TxData, 0, 15);
sprintf(TxData, "%u\r\n", data);
HAL_UART_Transmit(&hlpuart1, TxData, 15, HAL_MAX_DELAY);
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
}
2024-05-28 03:16 AM
Hello @vbk22398,
Could you please try this example DAC Signals generation example it contains the system clock configurations and TIM6 configurations, and it works correctly.
If the issue persists after verifying the example, Could you please share the code that was used .Ioc file, to reproduce the issue in order to allow a better analysis of the problem?