cancel
Showing results for 
Search instead for 
Did you mean: 

I am reading i2c data from temperature sensor. I need to convert i2c data that I got from sensor to PWM signal. I have generated PWM signal.

NKaur.1
Associate II

Right now, I am able to get I2C data from sensor. I have generated PWM signal separately with TIM_CH2. But I have no idea how to generate pwm from I2C signal. So, basically I need to control amplitude of pwm from i2c signal. I am using NUCLEO-L011K4 microcontroller development board which is STM32L011K4T6

Here my main part of code:

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  uint8_t receiveBuffer[1];

  write_register(0x0A, 0X00);

  read_register(0X0A, receiveBuffer );

  write_register(0X14, 0XC1);

  read_register(0X14, receiveBuffer);

  HAL_Delay(20);

  buffer[0]=0x08;

  HAL_I2C_Master_Transmit(&hi2c1,0x50<<1,buffer,1,100);

  HAL_Delay(20);

  HAL_I2C_Master_Receive(&hi2c1,0x50<<1,buffer,2,100);

 // buffer[0] : MSB data

 // buffer[1] : LSB data

  rawT = buffer[0]<<8 | buffer[1];

  Temperature = rawT * 0.005;

  HAL_Delay(50);

  int32_t counter = 0;

 HAL_TIM_Base_Start_IT(&htim2);

  HAL_TIM_Base_Start(&htim2);

  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);

  while(counter < 4096)

    {

     TIM2->CCR2 = counter;

     counter += 1;

    }

 HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);

  HAL_Delay(50);

  }

CubeMX selection of pins are attached here.

Thanks

2 REPLIES 2

TIM2->CCR2 would control the PULSE WIDTH, in timer ticks, of the signal.

You'd need to come up with the 0-100% duty as it relates to the temperature range you want to represent, probably with some min/max duty limits.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
NKaur.1
Associate II

Thanks for reply. Can you give any example for how to map TIM2->CCR2 to any signal.