1、PWM
PWM配置
配置时基单元 配置输出比较单元 配置输出PWM波的端口
# include "stm32f10x.h"
void PWM_Init ( void )
{
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE) ;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct. GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct. GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct. GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, & GPIO_InitStruct) ;
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM2, ENABLE) ;
TIM_InternalClockConfig ( TIM2) ;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct. TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct. TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct. TIM_Period = 100 - 1 ;
TIM_TimeBaseInitStruct. TIM_Prescaler = 720 - 1 ;
TIM_TimeBaseInitStruct. TIM_RepetitionCounter = 0 ;
TIM_TimeBaseInit ( TIM2, & TIM_TimeBaseInitStruct) ;
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCStructInit ( & TIM_OCInitStruct) ;
TIM_OCInitStruct. TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct. TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct. TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct. TIM_Pulse = 0 ;
TIM_OC1Init ( TIM2, & TIM_OCInitStruct) ;
TIM_Cmd ( TIM2, ENABLE) ;
}
void PWM_SetCompare1 ( uint16_t Compare)
{
TIM_SetCompare1 ( TIM2, Compare) ;
}
2、输入捕获配置
# include "stm32f10x.h"
void IC_Init ( void )
{
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE) ;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct. GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct. GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct. GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, & GPIO_InitStruct) ;
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3, ENABLE) ;
TIM_InternalClockConfig ( TIM3) ;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct. TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct. TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct. TIM_Period = 65536 - 1 ;
TIM_TimeBaseInitStruct. TIM_Prescaler = 72 - 1 ;
TIM_TimeBaseInitStruct. TIM_RepetitionCounter = 0 ;
TIM_TimeBaseInit ( TIM3, & TIM_TimeBaseInitStruct) ;
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct. TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct. TIM_ICFilter = 0x10 ;
TIM_ICInitStruct. TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct. TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct. TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit ( TIM3, & TIM_ICInitStruct) ;
TIM_PWMIConfig ( TIM3, & TIM_ICInitStruct) ;
TIM_SelectInputTrigger ( TIM3, TIM_TS_TI1FP1) ;
TIM_SelectSlaveMode ( TIM3, TIM_SlaveMode_Reset) ;
TIM_Cmd ( TIM3, ENABLE) ;
}
uint32_t IC_GetFreq ( void )
{
return 1000000 / ( ( TIM_GetCapture1 ( TIM3) ) + 1 ) ;
}
uint32_t IC_GetDuty ( void )
{
return ( ( TIM_GetCapture2 ( TIM3) + 1 ) * 100 ) / ( TIM_GetCapture1 ( TIM3) + 1 ) ;
}
3、编码器
# include "stm32f10x.h"
void Encoder2_Init ( void )
{
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE) ;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct. GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct. GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct. GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, & GPIO_InitStruct) ;
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3, ENABLE) ;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct. TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct. TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct. TIM_Period = 65536 - 1 ;
TIM_TimeBaseInitStruct. TIM_Prescaler = 1 - 1 ;
TIM_TimeBaseInitStruct. TIM_RepetitionCounter = 0 ;
TIM_TimeBaseInit ( TIM3, & TIM_TimeBaseInitStruct) ;
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICStructInit ( & TIM_ICInitStruct) ;
TIM_ICInitStruct. TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct. TIM_ICFilter = 0xF ;
TIM_ICInit ( TIM3, & TIM_ICInitStruct) ;
TIM_ICInitStruct. TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct. TIM_ICFilter = 0xF ;
TIM_ICInit ( TIM3, & TIM_ICInitStruct) ;
TIM_EncoderInterfaceConfig ( TIM3,
TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising) ;
TIM_Cmd ( TIM3, ENABLE) ;
}
int16_t Encoder_Get ( void )
{
return TIM_GetCounter ( TIM3) ;
}
int16_t Encoder_GetSpeed ( void )
{
int16_t Temp_Count = TIM_GetCounter ( TIM3) ;
TIM_SetCounter ( TIM3, 0 ) ;
return Temp_Count;
}