CH1, CH3
CH1 CH1N
Std 라이브러리
/* TIM Disable */
TIM1->CR1 &= (uint16_t)~TIM_CR1_CEN;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Time base configuration */
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3;//TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;//72Mhz
TIM_TimeBaseStructure.TIM_Period = a_nPeriod - 1; //(72MHz/72)/a_nPeriod = x Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Init TIM Status */
TIM1->CNT = 0;
TIM1->SR = 0;
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;//TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 10;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;//TIM_Break_Disable;//TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//TIM_AutomaticOutput_Disable
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
/* Defines the output pin state during timer idle state (HIGH/LOW) */
/* PWM1 Mode configuration: Channel 1 */
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = a_nPeriod - a_nDuty;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = a_nDuty;//a_nDuty;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
Hal 라이브러리
int main(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0, };
TIM_MasterConfigTypeDef sMasterConfig = {0, };
TIM_SlaveConfigTypeDef sSlaveConfig = {0, };
TIM_OC_InitTypeDef sConfigOC = {0, };
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0, };
if(HAL_Init()!= HAL_OK){
/* Start Conversation Error */
Error_Handler();
}
/* Configure the system clock to 84 MHz */
SystemClock_Config();
/* Configure LED2 */
BSP_LED_Init(LED2);
/*##-1- Configure the TIM peripheral #######################################*/
/* Set TIMx instance */
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = 0;//48-1;//uhPrescalerValue;
TimHandle.Init.Period = 1000;//138;//uwPeriodValue;//PERIOD_VALUE;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;//TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK){
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig) != HAL_OK){
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &sMasterConfig);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;//TIM_OSSR_DISABLE;//TIM_OSSR_ENABLE
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;//TIM_OSSI_DISABLE;//TIM_OSSI_ENABLE
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 50;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;//TIM_BREAK_ENABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
// sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;//TIM_AUTOMATICOUTPUT_DISABLE;//TIM_AUTOMATICOUTPUT_ENABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sBreakDeadTimeConfig) != HAL_OK){
Error_Handler();
}
/* Common configuration for all channels */
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;//TIM_OCNIDLESTATE_SET;//TIM_OCNIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
sConfigOC.Pulse = 0;//69;
#if 0
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_1) != HAL_OK){
Error_Handler();
}
#else
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_1) != HAL_OK){
Error_Handler();
}
#endif
sConfigOC.OCMode = TIM_OCMODE_PWM2;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;//TIM_OCFAST_ENABLE
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
/* Set the pulse value for channel 2 */
sConfigOC.Pulse = 0;
#if 0
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_2) != HAL_OK){
Error_Handler();
}
#else
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_2) != HAL_OK){
Error_Handler();
}
#endif
/* Set the pulse value for channel 3 */
// sConfigOC.Pulse = 500;
#if 0
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_3) != HAL_OK){
Error_Handler();
}
#else
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_3) != HAL_OK){
Error_Handler();
}
#endif
/* Set the pulse value for channel 4 */
// sConfigOC.Pulse = 500;
#if 0
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_4) != HAL_OK){
Error_Handler();
}
#else
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_4) != HAL_OK){
Error_Handler();
}
#endif
__HAL_TIM_DISABLE_OCxPRELOAD(&TimHandle, TIM_CHANNEL_1);
__HAL_TIM_DISABLE_OCxPRELOAD(&TimHandle, TIM_CHANNEL_2);
__HAL_TIM_DISABLE_OCxPRELOAD(&TimHandle, TIM_CHANNEL_3);
__HAL_TIM_DISABLE_OCxPRELOAD(&TimHandle, TIM_CHANNEL_4);
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK){
Error_Handler();
}
#if 1
/* Start channel 2 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK){
Error_Handler();
}
/* Start channel 3 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK){
Error_Handler();
}
/* Start channel 4 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK){
Error_Handler();
}
#endif
if (HAL_TIMEx_PWMN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK){
Error_Handler();
}
__HAL_TIM_SetCompare(&TimHandle, TIM_CHANNEL_1, 400);
// __HAL_TIM_SetCompare(&TimHandle, TIM_CHANNEL_2, 400);
__HAL_TIM_SetCompare(&TimHandle, TIM_CHANNEL_3, 600);
/* Infinite loop */
while (1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
/* Insert delay 100 ms */
HAL_Delay(100);
}
}
'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글
No section matches selector - no section to be FIRST/LAST. 에러 (2) | 2022.02.12 |
---|---|
STM32F 자이로 가속센서 MPU6050 (0) | 2022.02.08 |
STM32F DAC, Digital POT (0) | 2022.02.03 |
HMAC-SHA256(KISA) (0) | 2021.12.14 |
STM32F103 - PB10, PB11 PWM REMAP (0) | 2021.12.14 |