**Simple LD2 blinking with 2 periods - for delay variiable debuging set optimisation to none ** int main(void) { /* USER CODE BEGIN 1 */ uint32_t delay; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if (HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)) delay = 100; else delay = 500; HAL_Delay(delay); HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin); } /* USER CODE END 3 */ } /** System Clock Configuration */ ** End Of Example ** **PWM using delays example:** int main(void) { /* USER CODE BEGIN 1 */ uint32_t delay,period,pwm; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ period = 20; // in miliseconds pwm = 50; // in percents /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if (HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)) { pwm = 25; HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,1); delay = period*pwm; delay = delay/100; HAL_Delay(delay); HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,0); delay = period - delay; HAL_Delay(delay); } else { delay = 500; HAL_Delay(delay); HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin); } } /* USER CODE END 3 */ } /** System Clock Configuration */ ** End Of Example ** **PWM using delays example no 2:** int main(void) { /* USER CODE BEGIN 1 */ uint32_t delay,period,pwm,ton,toff; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ period = 20; // in miliseconds pwm = 25; // in percents /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ delay = period*pwm; ton = delay/100; toff= period - ton; if (HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)) { pwm = 25; } else { if (pwm==99) pwm = 1; else pwm++; } HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,1); HAL_Delay(ton); HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,0); HAL_Delay(toff); } /* USER CODE END 3 */ } /** System Clock Configuration */ ** End Of Example ** **PWM using timer TIM2 example:** 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(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ uint32_t delay=5; uint16_t pwm=0; HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ for (pwm=0;pwm<500;pwm=pwm+10) { HAL_Delay(delay); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, pwm); } for (pwm=0;pwm<500;pwm=pwm+5) { HAL_Delay(delay); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 500-pwm); } } /* USER CODE END 3 */ } /** System Clock Configuration ** End Of Example ** ** For system clock = 80MHz //Timer PPM TIM2_CH1 (PA5 - LD2) prescaler=79, Counter period=20000 + Timer HCsr04 TIM3_CH1 prescaler=799, Counter period=10000 (PA6 - 10us co 100ms) + SWO printf debug + RS232 printf debug ** int main(void) { /* USER CODE BEGIN 1 */ uint32_t delay,period,pwm,ton,toff; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM2_Init(); MX_USART2_UART_Init(); MX_TIM3_Init(); /* USER CODE BEGIN 2 */ period = 20; // in miliseconds pwm = 25; // in percents delay = 20; HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ for (pwm=500;pwm<=2500;pwm=pwm+10) { HAL_Delay(delay); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, pwm); printf("PPM value = %i,us\n\r",pwm); } for (pwm=2500;pwm>=500;pwm=pwm-10) { HAL_Delay(delay); __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, pwm); } } /* USER CODE END 3 */ } /** System Clock Configuration * * * /* USER CODE BEGIN 4 */ /*//SW0 int _write(int file, char *ptr, int len) { int DataIdx; for(DataIdx=0; DataIdx