Created
October 22, 2019 20:46
-
-
Save d-boz-wtwh/deaf13b78607d86be3161c28f2ad3b13 to your computer and use it in GitHub Desktop.
Microcontroller Projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************** | |
*Property off = www.microcontroller-project.com * | |
*Author = Usman Ali Butt * | |
*Date = 10 April 2017 * | |
* * | |
*****************************************************/ | |
#include "main.h" | |
#include "stm32f1xx_hal.h" | |
/* Private variables ---------------------------------------------------------*/ | |
TIM_HandleTypeDef htim4; | |
/* Private function prototypes -----------------------------------------------*/ | |
void SystemClock_Config(void); | |
void Error_Handler(void); | |
static void MX_GPIO_Init(void); | |
static void MX_TIM4_Init(void); | |
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); | |
int main(void) | |
{ | |
/* 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_TIM4_Init(); | |
/* USER CODE BEGIN 2 */ | |
HAL_TIM_Base_Start(&htim4); | |
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1); //Start Pwm signal on PB-6 Pin | |
/* USER CODE END 2 */ | |
int pwm=0; | |
while (1) | |
{ | |
for (pwm=0;pwm<=3906;pwm=pwm+1) //darkest to brightest: 0-100% duty cycle | |
{ | |
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm); //update pwm value | |
HAL_Delay(5); //Wait for 5ms | |
} | |
HAL_Delay(500); //hold for 0.5s | |
for (pwm=3906;pwm>=0;pwm=pwm-1) //brightest to darkest: 100-0% duty cycle | |
{ | |
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm); | |
HAL_Delay(5); //Wait for 5ms | |
} | |
HAL_Delay(500); //hold for 0.5s | |
} | |
} | |
/** System Clock Configuration | |
*/ | |
void SystemClock_Config(void) | |
{ | |
RCC_OscInitTypeDef RCC_OscInitStruct; | |
RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
/**Initializes the CPU, AHB and APB busses clocks | |
*/ | |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
RCC_OscInitStruct.HSICalibrationValue = 16; | |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; | |
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
/**Initializes the CPU, AHB and APB busses 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_DIV16; | |
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16; | |
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV16; | |
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
/**Configure the Systick interrupt time | |
*/ | |
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); | |
/**Configure the Systick | |
*/ | |
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); | |
/* SysTick_IRQn interrupt configuration */ | |
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); | |
} | |
/* TIM4 init function */ | |
static void MX_TIM4_Init(void) | |
{ | |
TIM_ClockConfigTypeDef sClockSourceConfig; | |
TIM_MasterConfigTypeDef sMasterConfig; | |
TIM_OC_InitTypeDef sConfigOC; | |
htim4.Instance = TIM4; | |
htim4.Init.Prescaler = 15; | |
htim4.Init.CounterMode = TIM_COUNTERMODE_UP; | |
htim4.Init.Period = 3906; | |
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; | |
if (HAL_TIM_Base_Init(&htim4) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; | |
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; | |
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; | |
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
sConfigOC.OCMode = TIM_OCMODE_PWM1; | |
sConfigOC.Pulse = 1953; | |
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; | |
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; | |
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
HAL_TIM_MspPostInit(&htim4); | |
} | |
/** Pinout Configuration | |
*/ | |
static void MX_GPIO_Init(void) | |
{ | |
/* GPIO Ports Clock Enable */ | |
__HAL_RCC_GPIOB_CLK_ENABLE(); | |
} | |
void Error_Handler(void) | |
{ | |
while(1) | |
{ | |
} | |
/* USER CODE END Error_Handler */ | |
} | |
#ifdef USE_FULL_ASSERT | |
void assert_failed(uint8_t* file, uint32_t line) | |
{ | |
} | |
#endif | |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment