1.定时器1中心对齐输出6路互补PWM
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能PORTA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // 使能PORTA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); // TIM1时钟使能
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8, GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9, GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_TIM1); // GPIOA6复用为定时器1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 1; // 系统主时钟168MHz分频系数 这里为10分频
TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); // 初始化定时器1
TIM_BDTRInitStructure.TIM_DeadTime = 0x94; // 1us死区时间
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 关闭上桥
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
// Phase A
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
// Phase B
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
// Phase C
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM1->CCR1 = 600;
TIM1->CCR2 = 600;
TIM1->CCR3 = 600;
2.3通道输入捕获配置(用于霍尔传感器)
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // 使能PORTC时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); // TIM3时钟使能
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6, GPIO_AF_TIM3); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7, GPIO_AF_TIM3); // GPIOA6复用为定时器1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource8, GPIO_AF_TIM3); // GPIOA6复用为定时器1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //下拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOC,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 167; // 系统主时钟168MHz分频系数 这里为168分频
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); // 初始化定时器3
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; // 双边沿捕获
// 输入通道和捕获通道的映射关系,有直连和非直连两种
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // 映射到TI1上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // 配置输入分频,不分频
TIM_ICInitStructure.TIM_ICFilter = 0x00; // IC1F=0000 配置输入滤波器 不滤波
/* 重点: 需要单独配置每个通道 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // CC1S=01 选择输入端 IC1映射到TI1上
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // CC1S=01 选择输入端 IC1映射到TI2上
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; // CC1S=01 选择输入端 IC1映射到TI3上
TIM_ICInit(TIM3, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级3 (0为最高7为最低)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC1| TIM_IT_CC2|TIM_IT_CC3, ENABLE);
TIM_ClearFlag(TIM3, TIM_IT_CC1| TIM_IT_CC2| TIM_IT_CC3);
// TIM_SelectInputTrigger(TIM3, TIM_TS_TI1F_ED);
TIM_Cmd(TIM3, ENABLE);
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_44864842/article/details/127231279
|
|