新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 基于STM32基本定時器3的PWM輸出

基于STM32基本定時器3的PWM輸出

作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
說明:使用標準庫3.4

#include "stm32f10x.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t CCR1_Val = 250;
uint16_t CCR2_Val = 500;
uint16_t CCR3_Val = 750;
uint16_t CCR4_Val = 0;
uint16_t PrescalerValue = 0;

本文引用地址:http://m.butianyuan.cn/article/201611/318477.htm


void RCC_Configuration(void); //時鐘配置
void GPIO_Configuration(void); //管腳配置
void Delay (__IO uint32_t nCount); //延時函數(shù)


int main(void)
{

RCC_Configuration();
GPIO_Configuration();
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 1000; //周期
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //分頻
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //時鐘分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//計數(shù)模
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始TIM3

/* 通道1 /
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //寫比較值(占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //置高
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

/ 通道2 /
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

/* 通道3 */
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

/ 通道4 */
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM3, ENABLE); //
TIM_Cmd(TIM3, ENABLE); //使能計數(shù)

while (1)
{
CCR4_Val++;
if(CCR4_Val==1000)CCR4_Val=0;
TIM_SetCompare4(TIM3,CCR4_Val); //占空比調(diào)節(jié)

Delay(0xFfff);

}
}

/* 時鐘配置 */
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //T3時鐘使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
//管腳時鐘
}
/ 管腳配置 /

void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);

}
/ 延時函數(shù) /

void Delay (__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}



關(guān)鍵詞: STM32基本定時器3PWM輸

評論


技術(shù)專區(qū)

關(guān)閉