cancel
Showing results for 
Search instead for 
Did you mean: 

I2C conflict with timer output

LFerr.7
Associate II

hello to everyone,

i am using stm32f429zi to generate a pulse for a triac . It all function without a lcd display.

when i write the code to init the display(via I2C) ,,, the display works very fine but i have a problem ... the output of timer1 doesnt show a pulse but a 3.3V and it is not good for my application.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 "i2c-lcd.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c2;
 
TIM_HandleTypeDef htim1;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C2_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define TRIAC_PULSE_WIDTH  100  // Duration of TRIAC trigger pulse in µs
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
	 int alpha =500;
 
   set_alpha( alpha );            // Generate alpha on output
 
  /* 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_I2C2_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  lcd_init ();
 
       lcd_send_cmd (0x80|0x00);
        lcd_send_string(" POWER TECHNOLOGIES");
 
  /* 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};
 
  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
 
  /** 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_DIV1;
  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();
  }
}
 
/**
  * @brief I2C2 Initialization Function
  * @param None
  * @retval None
  */
 
void set_alpha( int alpha )
{
   TIM1->CCR1 = alpha;
   TIM1->ARR = alpha + TRIAC_PULSE_WIDTH;
}
 
 void MX_I2C2_Init(void)
{
 
  /* USER CODE BEGIN I2C2_Init 0 */
 
  /* USER CODE END I2C2_Init 0 */
 
  /* USER CODE BEGIN I2C2_Init 1 */
 
  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.ClockSpeed = 100000;
  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C2_Init 2 */
 
  /* USER CODE END I2C2_Init 2 */
 
}
 
/**
  * @brief TIM1 Initialization Function
  * @param None
  * @retval None
  */
 void MX_TIM1_Init(void)
{
 
  /* USER CODE BEGIN TIM1_Init 0 */
	 {
	    __HAL_RCC_TIM1_CLK_ENABLE(); // Enable Timer 1 clock
	    TIM1->CR1   = 0x0088;        // Auto-reload, One-Pulse-Mode
	    TIM1->CR2   = 0x0000;        // Idle-state configuration
	    TIM1->SMCR  = 0x0066;        // Trigger-Mode, Timer input 2
	    TIM1->DIER  = 0x0000;        // Disable interrupts
	    TIM1->SR    = 0x0000;        // Clear status register
	    TIM1->EGR   = 0x0000;        // Clear event generation register
	    TIM1->CCMR1 = 0x0178;        // OC1: PWM2-Mode, Preload, IC2: IC2 on TI2
	    TIM1->CCMR2 = 0x0000;        // OC3/OC4, IC3/IC4 off
	    TIM1->CCER  = 0x00A1;        // OC1: On, AH; IC2: On, Both edges
	    TIM1->CNT   = 0;             // Clear timer 1 counter
	    TIM1->PSC   = 168-1;         // Set prescaler to 168
	    TIM1->ARR   = 0x0000;        // Initialize Auto-reload register
	    TIM1->RCR   = 0x0000;        // Clear repetition counter
	    TIM1->CCR1  = 0x0000;        // Initialize Capture/Compare register 1
	    TIM1->CCR2  = 0x0000;        // Clear Capture/Compare register 2
	    TIM1->CCR3  = 0x0000;        // Clear Capture/Compare register 3
	    TIM1->CCR4  = 0x0000;        // Clear Capture/Compare register 4
	    TIM1->BDTR  = 0x8000;        // Main output enable
	    TIM1->DCR   = 0x0000;        // Clear DMA configuration (DMA not needed)
	    TIM1->CR1  |= 0x0001;        // Timer 1 Enable
	 }
  /* USER CODE END TIM1_Init 0 */
 
 
  /* USER CODE BEGIN TIM1_Init 2 */
 
  /* USER CODE END TIM1_Init 2 */
 
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
void MX_GPIO_Init(void)
{
 
	{
	   __HAL_RCC_GPIOE_CLK_ENABLE();    // Enable GPIOE clock
	   GPIOE->MODER   = 0x00880000;     // Enable PE9/PE11 alternate function mode
	   GPIOE->AFR[1]  = 0x00001010;     // Select PE9/PE11 alternate function AF1
	   GPIOE->AFR[0]  = 0x00000000;     // Clear AFRL
	   GPIOE->OTYPER  = 0x00000000;     // Set all outputs to push-pull mode (default)
	   GPIOE->OSPEEDR = 0x00000000;     // Set all outputs to low-speed (default)
	   GPIOE->PUPDR   = 0x00000000;     // Disable all pull-ups and pull-downs
	}
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
 
}
 
/* 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 */

2 REPLIES 2
LFerr.7
Associate II

 void MX_TIM1_Init(void)

{

 /* USER CODE BEGIN TIM1_Init 0 */

{

  __HAL_RCC_TIM1_CLK_ENABLE(); // Enable Timer 1 clock

  TIM1->CR1  = 0x0088;    // Auto-reload, One-Pulse-Mode

  TIM1->CR2  = 0x0000;    // Idle-state configuration

  TIM1->SMCR = 0x0066;    // Trigger-Mode, Timer input 2

  TIM1->DIER = 0x0000;    // Disable interrupts

  TIM1->SR  = 0x0000;    // Clear status register

  TIM1->EGR  = 0x0000;    // Clear event generation register

  TIM1->CCMR1 = 0x0178;    // OC1: PWM2-Mode, Preload, IC2: IC2 on TI2

  TIM1->CCMR2 = 0x0000;    // OC3/OC4, IC3/IC4 off

  TIM1->CCER = 0x00A1;    // OC1: On, AH; IC2: On, Both edges

  TIM1->CNT  = 0;       // Clear timer 1 counter

  TIM1->PSC  = 168-1;     // Set prescaler to 168

  TIM1->ARR  = 0x0000;    // Initialize Auto-reload register

  TIM1->RCR  = 0x0000;    // Clear repetition counter

  TIM1->CCR1 = 0x0000;    // Initialize Capture/Compare register 1

  TIM1->CCR2 = 0x0000;    // Clear Capture/Compare register 2

  TIM1->CCR3 = 0x0000;    // Clear Capture/Compare register 3

  TIM1->CCR4 = 0x0000;    // Clear Capture/Compare register 4

  TIM1->BDTR = 0x8000;    // Main output enable

  TIM1->DCR  = 0x0000;    // Clear DMA configuration (DMA not needed)

  TIM1->CR1 |= 0x0001;    // Timer 1 Enable

}

 /* USER CODE END TIM1_Init 0 */

 /* USER CODE BEGIN TIM1_Init 2 */

 /* USER CODE END TIM1_Init 2 */

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

void MX_GPIO_Init(void)

{

{

  __HAL_RCC_GPIOE_CLK_ENABLE();  // Enable GPIOE clock

  GPIOE->MODER  = 0x00880000;   // Enable PE9/PE11 alternate function mode

  GPIOE->AFR[1] = 0x00001010;   // Select PE9/PE11 alternate function AF1

  GPIOE->AFR[0] = 0x00000000;   // Clear AFRL

  GPIOE->OTYPER = 0x00000000;   // Set all outputs to push-pull mode (default)

  GPIOE->OSPEEDR = 0x00000000;   // Set all outputs to low-speed (default)

  GPIOE->PUPDR  = 0x00000000;   // Disable all pull-ups and pull-downs

}

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOF_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOE_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

}

Andrew Neil
Evangelist III

0693W000008xsqBQAQ.png