单片机学习!
目录
前言
一、输出比较单元配置函数
1.1 TIM_OC1Init
1.2 TIM_OC2Init
1.3 TIM_OC3Init
1.4 TIM_OC4Init
1.5 TIM_OCStructInit
二、高级定时器输出PWM
2. TIM_CtrlPWMOutputs
三、强制输出模式配置
3.1 TIM_ForcedOC1Config
3.2 TIM_ForcedOC2Config
3.3 TIM_ForcedOC3Config
3.4 TIM_ForcedOC4Config
四、配置CCR寄存器的预装功能
4.1 TIM_OC1PreloadConfig
4.2 TIM_OC2PreloadConfig
4.3 TIM_OC3PreloadConfig
4.4 TIM_OC4PreloadConfig
五、配置快速使能
5.1 TIM_OC1FastConfig
5.2 TIM_OC2FastConfig
5.3 TIM_OC3FastConfig
5.4 TIM_OC4FastConfig
六、外部事件时清除REF信号
6.4 TIM_ClearOC1Ref
6.2 TIM_ClearOC2Ref
6.3 TIM_ClearOC3Ref
6.4 TIM_ClearOC4Ref
七、单独设置输出比较的极性函数
7.1 TIM_OC1PolarityConfig
7.2 TIM_OC1NPolarityConfig
7.3 TIM_OC2PolarityConfig
7.4 TIM_OC2NPolarityConfig
7.5 TIM_OC3PolarityConfig
7.6 TIM_OC3NPolarityConfig
7.7 TIM_OC4PolarityConfig
八、单独修改输出使能参数
8.1 TIM_CCxCmd
8.2 TIM_CCxNCmd
九、选择输出比较模式
9. TIM_SelectOCxM
十、单独更改CCR寄存器值的函数
10.1 TIM_SetCompare1
10.2 TIM_SetCompare2
10.3 TIM_SetCompare3
10.4 TIM_SetCompare4
总结
本文主要分类归纳了一些常用的PWM驱动常用库函数。
- 函数用结构体来初始化输出比较单元。
- 四个输出比较单元对应以下四个函数:TIM_OC1Init、TIM_OC2Init、TIM_OC3Init、TIM_OC4Init.
- 参数TIMx:选择定时器
- 参数TIM_OCInitStruct:结构体,配置输出比较参数
TIM_OCInitTypeDef structure 结构体
typedef struct
{
uint16_t TIM_OCMode;
uint16_t TIM_OutputState;
uint16_t TIM_OutputNState;
uint16_t TIM_Pulse;
uint16_t TIM_OCPolarity;
uint16_t TIM_OCNPolarity;
uint16_t TIM_OCIdleState;
uint16_t TIM_OCNIdleState;
} TIM_OCInitTypeDef;
TIM_OCMode选择定时器模式。
TIM_OutputState选择输出比较状态。
TIM_OutputNState选择互补输出比较状态。
TIM_Pulse设置了待装入捕获比较寄存器的脉冲值。它的取值必须在0x0000和0xFFFF之间。
TIM_OCPolarity输出极性。
TIM_OCNPolarity互补输出极性。
TIM_OCIdleState选择空闲状态下的非工作状态。
TIM_OCNIdleState选择空闲状态下的非工作状态。
代码示例:
/* Configures the TIM1 Channel1 in PWM Mode */
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 0x7FF;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
函数用来给输出比较结构体赋一个默认值
void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
{
/* Set the default configuration */
TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct->TIM_Pulse = 0x0000;
TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
}
代码示例:
TIM_OCStructInit(&TIM_OCInitStructture);//TIM_OCStructInit函数需要把结构体变量的地址传进去,才能给结构体赋初始值。
函数仅高级定时器使用,在使用高级定时器输出PWM时需要调用这个函数,使能主输出,否则PWM将不能正常输出。
函数可用于运行中需要暂停输出波形并且强制输出高电平或低电平,(强制输出高电平和设置100%占空比效果一样,制输出低电平和设置0%占空比效果一样,所以一般不常使用)
预装功能就是影子寄存器,其作用是写入值不会立刻生效,而是在更新事件时才会生效。
函数带N的就是高级定时器里互补通道的配置,OC4没有互补通道,所以就没有OC4N的函数。这个函数设置极性和上文结构体初始化那个函数里设置极性的作用是一样的,只不过结构体是一起初始化的,这是一个单独的函数进行修改的。一般来说,结构体里的参数,都有一个单独的函数可以进行更改。这里函数就是用来单独更改输出极性的。
函数用来使能或者失能 TIM 捕获比较通道 x。
/**
* @brief Enables or disables the TIM Capture Compare Channel x.
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param TIM_Channel: specifies the TIM Channel
* This parameter can be one of the following values:
* @arg TIM_Channel_1: TIM Channel 1
* @arg TIM_Channel_2: TIM Channel 2
* @arg TIM_Channel_3: TIM Channel 3
* @arg TIM_Channel_4: TIM Channel 4
* @param TIM_CCx: specifies the TIM Channel CCxE bit new state.
* This parameter can be: TIM_CCx_Enable or TIM_CCx_Disable.
* @retval None
*/
void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx)
函数用来使能或者失能 TIM 捕获比较通道 xN
/**
* @brief Enables or disables the TIM Capture Compare Channel xN.
* @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the TIM peripheral.
* @param TIM_Channel: specifies the TIM Channel
* This parameter can be one of the following values:
* @arg TIM_Channel_1: TIM Channel 1
* @arg TIM_Channel_2: TIM Channel 2
* @arg TIM_Channel_3: TIM Channel 3
* @param TIM_CCxN: specifies the TIM Channel CCxNE bit new state.
* This parameter can be: TIM_CCxN_Enable or TIM_CCxN_Disable.
* @retval None
*/
void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN)
函数用来单独更改输出比较模式。
- 选择 TIM 输出比较模式。
- 本函数在改变输出比较模式前失能选中的通道。
- 用户必须使用函数 TIM1_CCxCmd 和 TIM1_CCxNCmd 来使能这个通道。
/**
* @brief Selects the TIM Output Compare Mode.
* @note This function disables the selected channel before changing the Output
* Compare Mode.
* User has to enable this channel using TIM_CCxCmd and TIM_CCxNCmd functions.
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param TIM_Channel: specifies the TIM Channel
* This parameter can be one of the following values:
* @arg TIM_Channel_1: TIM Channel 1
* @arg TIM_Channel_2: TIM Channel 2
* @arg TIM_Channel_3: TIM Channel 3
* @arg TIM_Channel_4: TIM Channel 4
* @param TIM_OCMode: specifies the TIM Output Compare Mode.
* This parameter can be one of the following values:
* @arg TIM_OCMode_Timing
* @arg TIM_OCMode_Active
* @arg TIM_OCMode_Toggle
* @arg TIM_OCMode_PWM1
* @arg TIM_OCMode_PWM2
* @arg TIM_ForcedAction_Active
* @arg TIM_ForcedAction_InActive
* @retval None
*/
void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode)
以下四个函数比较重要,函数用于运行时更改占空比。
以上就是今天要讲的内容,本文仅仅简单介绍了PWM驱动常用库函数。