Note: The original , verified , casual copying, no copyright claims ~~
PWM tutorial dig four or five days, advanced PWM timer has been unable to open, the heart almost cold.
Playing for a long time the king of glory, then refer to wildfires brother routines, combined with STM library functions, a little broken up, with register operations, and finally a little prospect of a solution.
The following code, in the order in STM library functions code, output has been verified .
Detours experience:
1_ order of the code, there are a few lines has required careful to not come out correctly
2_ three important concepts: Update (drain interrupt and wide) \ shadow register (aging effect) \ alignment (different motors)
3_ code is easy to understand write packet
// GPIO 初始化部分
// ***********************************************
RCC->AHB1ENR |= 0x1 << 0; // 使能时钟 PA
RCC->AHB1ENR |= 0x1 << 2; // 使能时钟 PC
GPIOC->MODER |= 0x2 << 2*6; // 主信号输出引脚 PC6 TIM8_CH1_ 复用,开漏, 无效(速度,上下拉,ODR)
GPIOA->MODER |= 0x2 << 2*5; // 互补输出引脚 PA5 TIM8_CH1_ 复用,开漏, 无效(速度,上下拉,ODR)
GPIOA->MODER |= 0x2 << 2*6; // 断路保护 PA6 TIM8_CH1_ 复用,开漏, 无效(速度,上下拉,ODR)
GPIOC->AFR[0] |= 0x3 << 4*6; // 引脚复用 0x3 = GPIO_AF_TIM8
GPIOA->AFR[0] |= 0x3 << 4*5; // 引脚复用 0x3 = GPIO_AF_TIM8
GPIOA->AFR[0] |= 0x3 << 4*6; // 引脚复用 0x3 = GPIO_AF_TIM8
// TIM 初始化部分
// ************************************************
RCC->APB2ENR |= 1<<1; // 使能时钟 TIM8
// 时基配置
TIM8->CR1 = 0;
TIM8->CR1 = 0 << 4; // 方向,递增
TIM8->CR1 = 0 << 5; // 对齐,边沿
TIM8->CR1 = 0 << 8; // 分频,不分频
TIM8->ARR = 1000-1;
TIM8->PSC = 1800-1;
TIM8->RCR = 0; // 重复计数器
TIM8->EGR = 1;
TIM8->CCR1 = 900-1 ;
// 通道x配置
TIM8->CCER = 0; // 关闭通道1
// CR2 输出模式:闲时极性 输入模式:DMA等
TIM8->CR2 = 0;
TIM8->CR2 |= 1<<8; // [ ; 8] 通道1 闲时极性 1:高电平
TIM8->CR2 |= 0<<9; // [ : 9] 通道1 互补闲时极性 0:低电平
// CCMR1 通道x 模式配置
TIM8->CCMR1 =0;
TIM8->CCMR1 |= 0x6<<4; // [ 6: 4] 通道1 输出模式 110:PWM1_CNT<ARR_有效
// CCER 通道 闲时极性 \ 使能
TIM8->CCER |= 0 << 1; // [ : 1] 通道1 输出极性 0:高电平有效
TIM8->CCER |= 1 << 0; // [ : 0] 通道1 输出使能
TIM8->CCER |= 0 << 3; // [ : 3] 通道1 互补输出极性 0:高电平有效
TIM8->CCER |= 1 << 2 ; // [ : 2] 通道1 互补输出使能 1:低电平有效
// BDTR 断路\死区控制
TIM8->BDTR = 11 << 0; // [ 7: 0] 死区持续时间
TIM8->BDTR |= 0 << 8; // [ 9: 8] 锁定配置
TIM8->BDTR |= 1 << 10; // [ :10] MOE=0的关闭状态
TIM8->BDTR |= 1 << 11; // [ :11] MOE=1的关闭状态
TIM8->BDTR |= 0 << 12; // [ :12] 断路使能
TIM8->BDTR |= 0 << 13; // [ :13] 断路极性 00:低电平有效
TIM8->BDTR |= 1 << 14; // [ :14] 自动输出使能 00:只能软件置1 01:软件\更新置1
// 使能 开始工作
// *****************************************************
TIM8->BDTR |= 1 << 15; // 主动输出使能 , 高级定时器必须置些位
TIM8->CR1 |= 1; // 使能定时器