cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help to generate changing frequency sine wave in STM32L073RZT

rutvik1110
Associate II

Hello Community,

I'm working on a project and could use your help. I'm trying to make a sine wave with a changing frequency from 600 Hz to 5 kHz using STM32 microcontroller(STM32L073RZT) with 32 MHz as sysclk.Capture.PNG

 

Capture1.PNG

Capture2.PNG

This are my configurations. Where this r2r pins are DAC bits. 

 

I am not able to generate sine wave more than 280 Hz but i want to generate till 2 KHz. 

Please suggest if I'm doing something wrong or if anyone has a good idea on how to overcome this issue.

 

Code: 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
#include "stm32l0xx.h"
#include <math.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 */
#define SINE_TABLE_SIZE 256
void output_to_dac(uint16_t value);
volatile uint32_t Index = 0;
volatile uint32_t stepSize = 1;
/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
const uint16_t sineLookupTable[] = {
128, 131, 134, 137, 140, 143, 146, 149, 152, 155,
158, 162, 165, 167, 170, 173, 176, 179, 182, 185,
188, 190, 193, 196, 198, 201, 203, 206, 208, 211,
213, 215, 218, 220, 222, 224, 226, 228, 230, 232,
234, 235, 237, 238, 240, 241, 243, 244, 245, 246,
248, 249, 250, 250, 251, 252, 253, 253, 254, 254,
254, 255, 255, 255, 255, 255, 255, 255, 254, 254,
254, 253, 253, 252, 251, 250, 250, 249, 248, 246,
245, 244, 243, 241, 240, 238, 237, 235, 234, 232,
230, 228, 226, 224, 222, 220, 218, 215, 213, 211,
208, 206, 203, 201, 198, 196, 193, 190, 188, 185,
182, 179, 176, 173, 170, 167, 165, 162, 158, 155,
152, 149, 146, 143, 140, 137, 134, 131, 128, 124,
121, 118, 115, 112, 109, 106, 103, 100, 97, 93,
90, 88, 85, 82, 79, 76, 73, 70, 67, 65,
62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
37, 35, 33, 31, 29, 27, 25, 23, 21, 20,
18, 17, 15, 14, 12, 11, 10, 9, 7, 6,
5, 5, 4, 3, 2, 2, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 2,
2, 3, 4, 5, 5, 6, 7, 9, 10, 11,
12, 14, 15, 17, 18, 20, 21, 23, 25, 27,
29, 31, 33, 35, 37, 40, 42, 44, 47, 49,
52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
79, 82, 85, 88, 90, 93, 97, 100, 103, 106,
109, 112, 115, 118, 121, 124};


void setFrequency(uint32_t frequency) {
  printf("1Timer Period for %u Hz\n", frequency);
    uint32_t timer_period = (32000000 / (SINE_TABLE_SIZE * frequency)) - 1;
    printf("Timer Period for %u Hz: %u\n", frequency, timer_period);
    __HAL_TIM_SET_AUTORELOAD(&htim2, timer_period);
     
}

void output_to_dac(uint16_t value){
  HAL_GPIO_WritePin(GPIOB,r2r_0_Pin,(value & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_1_Pin,(value & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_2_Pin,(value & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_3_Pin,(value & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_4_Pin,(value & 0x10) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_5_Pin,(value & 0x20) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_6_Pin,(value & 0x40) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_7_Pin,(value & 0x80) ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

int _write(int file, char *ptr, int len) {
    HAL_UART_Transmit(&huart2, (uint8_t*)ptr, len, HAL_MAX_DELAY);
    return len;
}

/* 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_TIM2_Init();
  MX_USART2_UART_Init();
  HAL_TIM_Base_Start_IT(&htim2);
  /* USER CODE BEGIN 2 */
  printf("Start\n");
  setFrequency(290);
  /* USER CODE END 2 */

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

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

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** 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_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief TIM2 Initialization Function
  * @PAram None
  * @retval None
  */
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 = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 0;
  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_RESET;
  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 */

}
// Timer interrupt callback
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    static uint32_t sample_index = 0;
    if (htim->Instance == TIM2) {
    output_to_dac(sineLookupTable[sample_index]);
    sample_index = (sample_index + 1) % SINE_TABLE_SIZE;
    HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_0);
    }
}
/**
  * @brief USART2 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, r2r_0_Pin|r2r_1_Pin|r2r_3_Pin|r2r_2_Pin
                          |r2r_4_Pin|r2r_5_Pin|r2r_6_Pin|r2r_7_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : r2r_0_Pin r2r_1_Pin r2r_3_Pin r2r_2_Pin
                           r2r_4_Pin r2r_5_Pin r2r_6_Pin r2r_7_Pin */
  GPIO_InitStruct.Pin = r2r_0_Pin|r2r_1_Pin|r2r_3_Pin|r2r_2_Pin
                          |r2r_4_Pin|r2r_5_Pin|r2r_6_Pin|r2r_7_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
8 REPLIES 8
Uwe Bonnes
Principal III

HAL is not fast and GPIO write in HAL is even slower. Why don't you use  the built-in DAC?

printf() in the timing critical output routine will also eat up much cycles..

I have a R2R network and these bits generate sine waves. I don't know how to do this using built-in DAC, so if you can explain it, I'd be grateful. I have attached my circuit schematics. And printf() was just to debug the value.

Thanks in advance.

For use DAC read examples STM32CubeL0/Projects/NUCLEO-L073RZ at master · STMicroelectronics/STM32CubeL0 · GitHub

and your code need rewrite 

void output_to_dac(uint16_t value){
  HAL_GPIO_WritePin(GPIOB,r2r_0_Pin,(value & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_1_Pin,(value & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_2_Pin,(value & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_3_Pin,(value & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_4_Pin,(value & 0x10) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_5_Pin,(value & 0x20) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_6_Pin,(value & 0x40) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,r2r_7_Pin,(value & 0x80) ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

to somethink as

void output_to_dac(uint8_t value){
uint16_t temp = GPIOB->ODR & 0xff00;
GPIOB->ODR |= value;
}

Sorry, printf is not in the critical routine. For a start without the need to learn using the DAC, writing all 8 bits at a time will also speed up your code.

void output_to_dac(uint16_t value){
uint16_t pins_to_set = 0;

pins_to_set |= ((value & 0x01) ? r2r_0_Pin : 0);
pins_to_set |= ((value & 0x02) ? r2r_1_Pin : 0);
pins_to_set |= ((value & 0x04) ? r2r_2_Pin : 0);
pins_to_set |= ((value & 0x08) ? r2r_3_Pin : 0);
pins_to_set |= ((value & 0x10) ? r2r_4_Pin : 0);
pins_to_set |= ((value & 0x20) ? r2r_5_Pin : 0);
pins_to_set |= ((value & 0x40) ? r2r_6_Pin : 0);
pins_to_set |= ((value & 0x80) ? r2r_7_Pin : 0);

HAL_GPIO_WritePin(GPIOB, pins_to_set, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOB, ~(pins_to_set) & 0xFF, GPIO_PIN_RESET);
}

 

I changed my code so that the GPIO pins can be set and reset at the same time. The oscilloscope shows a constant output, not a sine wave.

void output_to_dac(uint16_t value){
uint16_t pins_to_set = 0;

pins_to_set |= ((value & 0x01) ? r2r_0_Pin : 0);
pins_to_set |= ((value & 0x02) ? r2r_1_Pin : 0);
pins_to_set |= ((value & 0x04) ? r2r_2_Pin : 0);
pins_to_set |= ((value & 0x08) ? r2r_3_Pin : 0);
pins_to_set |= ((value & 0x10) ? r2r_4_Pin : 0);
pins_to_set |= ((value & 0x20) ? r2r_5_Pin : 0);
pins_to_set |= ((value & 0x40) ? r2r_6_Pin : 0);
pins_to_set |= ((value & 0x80) ? r2r_7_Pin : 0);

HAL_GPIO_WritePin(GPIOB, pins_to_set, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOB, ~(pins_to_set) & 0xFF, GPIO_PIN_RESET);
}

 

I changed my code so that the GPIO pins can be set and reset at the same time. The oscilloscope shows a constant output, not a sine wave.

You dont show schematics, but when not connected same weight bits order on port B change your design ... and learn how to use bare metal registers GPIOB->BRR BSRR ODR ...

Capture3.PNG

Here is the schematics