cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate phase difference between phase shifted sine waves in stm32?

RShre.2
Associate III

I am using stm32-g07 and I managed to generate two waves: one reference and another phase shifted. Now I want to calculate the phase shift between the two. I tried two options:

  1. Try when the DAC channel output values for 1 and 2 get maximum and subtract the values. {this one gave me inaccurate result}
  2. instead of digital values between 0-4095, I converted them to voltage. {haven't tried this for two dacs yet}

However, in both cases, I don't get the index for the highest peak because I am using a phase accumulator and based on the phase increment, the peak value is constantly fluctuating. How to solve this problem?

The code is a bit mess because i was trying different methods. But my sine wave generation is here : void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

[Code in comment below]

1 REPLY 1
RShre.2
Associate III

/* USER CODE BEGIN Header */

/**

 ******************************************************************************

 * @file      : main.c

 * @brief     : Main program body

 ******************************************************************************

 * @attention

 *

 * Copyright (c) 2023 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"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "stdlib.h"

#include "math.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

#define PI 3.14159

#define NS 4096

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

ADC_HandleTypeDef hadc1;

DMA_HandleTypeDef hdma_adc1;

DAC_HandleTypeDef hdac1;

TIM_HandleTypeDef htim16;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

uint32_t Fs = 0;

uint64_t f_exp = 1000; //

uint64_t tuning = 0;

uint32_t idx = 0;

int output_val1 = 0;

int output_val2 = 0;

static int ratio = 0;

int start = 0;

int end = 0;

int phase_diff = 0;

int slow_counter = 0;

int counter = 0;

int n = 0;

float raw = 0;

float adc = 0;

uint32_t phase_diff_t = 0;

uint32_t phase = 0;

uint32_t diff = 0;

int LUT_r[NS];

int LUT_s[NS];

int PSC = 0;

int ARR = 0;

uint64_t Fclk = 64000000;

uint32_t adc_buffer[NS];

int buffer = 0;

/* 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_USART2_UART_Init(void);

static void MX_TIM16_Init(void);

static void MX_DAC1_Init(void);

static void MX_ADC1_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* LUT */

//

void sine_val(int x){

if(x == 1){

for(int i = 0;i < NS;i++){

LUT_r[i] = (sin(i* (2*PI)/NS )+1)*(4095/2);

}

}

else{

for(int i = 0;i < NS;i++){

LUT_s[i] = (sin(i* (2*PI)/NS - PI/2)+1)*(4095/2);

}

}

}

void samplingrate(){

Fs = Fclk/((PSC+1) * (ARR+1));

}

void phase_increment(int16_t freq_out, uint32_t Fs, uint8_t acc_depth) {

  tuning = (round(freq_out * pow(2, acc_depth) / Fs));

}

/* 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_USART2_UART_Init();

 MX_TIM16_Init();

 MX_DAC1_Init();

 MX_ADC1_Init();

 /* USER CODE BEGIN 2 */

 sine_val(1);

 sine_val(2);

 PSC = htim16.Init.Prescaler;

 ARR = htim16.Init.Period;

 samplingrate();

 ratio = Fs/f_exp;

 phase_increment(f_exp, Fs, 32);

 HAL_TIM_Base_Start_IT(&htim16);

 HAL_ADC_Start(&hadc1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

// HAL_ADC_PollForConversion(&hadc1, 0.001);

 //HAL_ADC_GetValue(&hadc1)

raw = output_val1 * 3.3/4096;

if(raw > 3.29){

counter++;

start = idx - start;

}

 }

  /* USER CODE END WHILE */

/* USER CODE BEGIN 4 */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

 // Check which version of the timer triggered this callback and toggle LED

 if (htim == &htim16 )

 {

 phase += tuning;

 idx = (phase >> 20) & 0xFFF;

 output_val1 = LUT_r[idx];

 output_val2 = LUT_s[idx];

 if(output_val1 >= 3900){

  start = idx;

  }

 if(output_val2 >= 4000){

 end = idx;

 }

 HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, output_val1);

 HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, output_val2);

//

//

 HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

  HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);

 }

}

 Unfortunately i couldn't post whole code but whatever i wrote for myself is here.