資源簡介
利用STM32定時器的同步,可以精準控制PWM脈沖數量(庫函數版)
代碼片段和文件信息
#include?“TIM_Master_Slave.h“
void?TIM_Master_Slave(void)??? //定義TIM2主???TIM4從
{
//定義
TIM_TimebaseInitTypeDef?TIM_TimebaseInitStruct;
TIM_OCInitTypeDef??TIM_OCInitStruct;
GPIO_InitTypeDef??GPIO_InitStruct;
NVIC_InitTypeDef??NVIC_InitStruct;
//時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOAENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOBENABLE);
??// TIM2定時器PWM配置
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);
TIM_TimebaseInitStruct.TIM_ClockDivision?=?TIM_CKD_DIV1?;
TIM_TimebaseInitStruct.TIM_CounterMode?=?TIM_CounterMode_Up?;
TIM_TimebaseInitStruct.TIM_Period?=?999?;
TIM_TimebaseInitStruct.TIM_Prescaler?=?71;
TIM_TimebaseInitStruct.TIM_RepetitionCounter?=?0;
TIM_TimebaseInit(TIM2&TIM_TimebaseInitStruct);
TIM_OCInitStruct.TIM_OCMode?=?TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity?=?TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_Pulse?=?499?;
TIM_OCInitStruct.TIM_OutputState?=?TIM_OutputState_Enable?;
TIM_OC1Init(TIM2?&TIM_OCInitStruct);
//主定時器主模式控制配置
TIM_SelectOutputTrigger(TIM2TIM_TRGOSource_Update);
TIM_SelectMasterSlaveMode(TIM2?TIM_MasterSlaveMode_Enable);
??// TIM4定時器PWM配置
GPIO_InitStruct.
- 上一篇:密度測量PLC程序
- 下一篇:TOPSIS熵值法R代碼.R
評論
共有 條評論