PWM : Small Radar

  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
  for(uint16_t i=0;i<780;i++)
  __ASM volatile ("NOP");     //F_CPU=48MHz and every cycle 0.020833us time 
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
extern TIM_HandleTypeDef htim17;
if(flag)
  {
    pulse_width=TIM17 -> CNT ;
    pulse_width=pulse_width/200; // since it reflect
    distance=(3.43*pulse_width); // Distance in cm  
    switch(direction)
    {
        case 1: sprintf(display_direction,"West:%.2fcm   ",distance);
        OLCD_write_string(2,0,display_direction);
        break;
        case 2: sprintf(display_direction,"SW:%.2fcm    ",distance);
        OLCD_write_string(2,0,display_direction);
        break;
        case 3: sprintf(display_direction,"South:%.2fcm  ",distance);
        OLCD_write_string(2,0,display_direction);
        break;
        case 4: sprintf(display_direction,"WE:%.2fcm   ",distance);
        OLCD_write_string(2,0,display_direction);
        break;
        case 0: sprintf(display_direction,"East:%.2fcm   ",distance);
        OLCD_write_string(2,0,display_direction);
        break;
    }
    TIM17->CNT=0;
    flag=0;
  }
else
  {
   HAL_TIM_Base_Start(&htim17);
   flag=1;
   TIM17->CNT=0;
  }
switch(direction)
    {
        case 0: TIM1->CCR1=499;      // West side
	    for(uint16_t j=0;j<8000;j++)
         for(uint16_t i=0;i<4800;i++)   // .8s
          __ASM volatile ("NOP");   // F_CPU=48MHz and every cycle 0.020833us time 
        break;
        case 1: TIM1->CCR1=999;     // South-West side
        break;
        case 2: TIM1->CCR1=1499;    // South side
        break;
        case 3: TIM1->CCR1=1999;    // South-East side
        break;
        case 4: TIM1->CCR1=2499;    // East side
        break;
    }
	for(uint16_t j=0;j<8000;j++)
     for(uint16_t i=0;i<480;i++)   // 80ms
         __ASM volatile ("NOP");   // F_CPU=48MHz and every cycle 0.020833us time 
  direction++;
  if(direction>=5) direction=0;    
  /******             Ultrasonic Sensor Function               ******************/
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
  for(uint16_t i=0;i<780;i++)
  __ASM volatile ("NOP");     //F_CPU=48MHz and every cycle 0.020833us time 
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET); 
  /******************************************************************************/
#include "ssd1306.h"
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); // PWM start for channel 1 TIM1
HAL_TIM_Base_Start_IT(&htim16); // Global interrupt for TIM16
init_OLED();
clear_display();
OLCD_write_string(0,0,"Small Rader");
while(1){
}
HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,uint16_t Length);
HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel);