STM32F0(5)定時(shí)器PWM
// 經(jīng)過(guò)下面的設(shè)置之后,該設(shè)置的引腳就會(huì)產(chǎn)生PWM波
void TIM1_PWM_Init(void)
{
// 配置引腳的復(fù)用功能
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
GPIOA->AFR[1] |= 0x00000002;
GPIOA->MODER |= GPIO_MODER_MODER8_1;
GPIOA->MODER &= ~GPIO_MODER_MODER8_0;
GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR8;
GPIOA->OTYPER |= GPIO_OTYPER_OT_8;
// 配置PWM波的相關(guān)屬性
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // 開(kāi)啟該定時(shí)器的時(shí)鐘開(kāi)關(guān)
// OC1M -- 控制PWM波模式
TIM1->CCMR1 |= (TIM_CCMR1_OC1PE | TIM_CCMR1_OC1FE | TIM_CCMR1_OC1M);
TIM1->PSC = 0; // 預(yù)分頻寄存器
TIM1->ARR = 256; // 自動(dòng)重裝載值 == 頻率值
TIM1->CCR1 = 0; // 占空比的值,控制占空比的數(shù)值不同從而控制輸出波形
// 開(kāi)啟 1 ,2 ,3 , 4哪一個(gè)通道的開(kāi)關(guān),以及輸出引腳有效電平
TIM1->CCER |= TIM_CCER_CC1E;
TIM1->BDTR |= TIM_BDTR_MOE; // PWM波輸出使能
TIM1->CR1 |= TIM_CR1_CEN; // 定時(shí)器使能
}