|
main.c中
#include "stm32f10x.h"
#include "time.h"
/****TIM3的通道2《CH2》(PA7)產(chǎn)生一路PWM輸出:
周期10ms,占空比30%****/
int main(void)
{
TIM3_PWM_Init();
while(1);
}
time.c中
#include "stm32f10x.h"
#include "time.h"
#include<stdio.h>
void TIM3_PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3定時(shí)器時(shí)鐘使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA(PA7)時(shí)鐘
//設(shè)置CH2(PA7)引腳配置--復(fù)用輸出功能
GPIO_InitTypeDef PA7;//定義GPIOx結(jié)構(gòu)體名字
PA7.GPIO_Pin=GPIO_Pin_7;
PA7.GPIO_Mode=GPIO_Mode_AF_PP;//復(fù)用推挽輸出
PA7.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&PA7);//初始化GPIOA
//定時(shí)器TIM3時(shí)基參數(shù)配置
TIM_TimeBaseInitTypeDef time3;//定義基本定時(shí)器結(jié)構(gòu)體名字
time3.TIM_ClockDivision=TIM_CKD_DIV1; //設(shè)置捕獲輸入分頻
time3.TIM_CounterMode=TIM_CounterMode_Up;//設(shè)置計(jì)數(shù)模式:TIM向上計(jì)數(shù)模式
time3.TIM_Prescaler=7200-1;//設(shè)置用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值
time3.TIM_Period=100-1;//設(shè)置重裝載值(周期)
TIM_TimeBaseInit(TIM3,&time3); //根據(jù)指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位
//TIM3時(shí)通道2輸出參數(shù)配置 PWM模式
TIM_OCInitTypeDef time3_oC2;
time3_oC2.TIM_OCMode=TIM_OCMode_PWM1; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式1
time3_oC2.TIM_OutputState=TIM_OutputState_Enable; //比較輸出使能
time3_oC2.TIM_OCPolarity=TIM_OCPolarity_High;//輸出極性:有效電平為高
time3_oC2.TIM_Pulse=30;//比較值
TIM_OC2Init(TIM3,&time3_oC2); //根據(jù)T指定的參數(shù)初始化外設(shè)TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的預(yù)裝載寄存器
TIM_Cmd(TIM3,ENABLE ); //使能指定的TIMx
}
|
評(píng)分
-
查看全部評(píng)分
|