STM32复习笔记(十八) —— 高级定时器(输出比较)
1.配置步骤
1)选择计数器时钟 (内部,外部,预分频器) 2)将相应的数据写入TIMx_ARR and TIMx_CCRx寄存器中 3)可设置中断,TIMx_DIER 4)选择输出模式,TIMx_CCMRx and TIMx_CCER 5)启动计数器,TIMx_CR1
2.本例程软件设计思路
使用 TIM1 输出比较,从 TIM1_CH1 通道输出占空比为30% PWM 波
3.代码编写
于 main.c 中编写代码
#include "stm32f10x.h"
#include "dr_usart.h"
void TIM1_CH1_GPIO_Config(void)
{
RCC->APB2ENR |= ((uint16_t)0x01 << 2);
RCC->APB2ENR |= ((uint16_t)0x01 << 0);
GPIOA->CRH &= ~((uint32_t)0x0F << 0);
GPIOA->CRH |= ((uint32_t)0x09 << 0);
AFIO->MAPR &= ~((uint32_t)0x03 << 6);
AFIO->MAPR |= ((uint32_t)0x01 << 6);
}
void TIM1_Config(void)
{
RCC->APB2ENR |= ((uint32_t)0x01 << 11);
TIM1->CR1 &= ~((uint16_t)0x07 << 4);
TIM1->CR1 &= ~((uint16_t)0x03 << 8);
TIM1->ARR = ((uint16_t)9999);
TIM1->PSC = ((uint16_t)71);
TIM1->RCR = ((uint16_t)0);
TIM1->CCER &= ~((uint16_t)0x01 << 0);
TIM1->CCMR1 &= ~((uint16_t)0xFF << 0);
TIM1->CCMR1 |= ((uint16_t)0x06 << 4);
TIM1->CCER &= ~((uint16_t)0x01 << 1);
TIM1->CCER |= ((uint16_t)0x01 << 0);
TIM1->CCR1 = ((uint16_t)3000);
TIM1->BDTR |= ((uint16_t)0x01 << 15);
TIM1->CR1 |= ((uint16_t)0x01 << 0);
}
int main(void)
{
User_Init();
delay_(1);
TIM1_CH1_GPIO_Config();
TIM1_Config();
while(1);
}
点击编译,如无错误可进入仿真界面 打开逻辑分析仪 (添加 PA8) 点击运行 经验证,程序运行结果与预想一致
|