#include "pbdata.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
int main(void)
{
RCC_Configuration();//系统时钟初始化
GPIO_Configuration();//端口初始化
TIM3_Configuration();//定时器和PWM配置
TIM_SetCompare2(TIM3,400);//最大行程
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)==SET);//校验完成
TIM_SetCompare2(TIM3,200);//给一定占空比,电机开始转动
delay_ms(1000);
delay_ms(1000);
TIM_SetCompare2(TIM3,210);//改变占空比,转速改变
while(1);
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED1
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//KEY
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_1);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//端口映射
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
//定时器设置
TIM_TimeBaseStructure.TIM_Period = 3999; //初值
TIM_TimeBaseStructure.TIM_Prescaler = 359; //预分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//PWM设置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//PWM1模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性
TIM_OC2Init(TIM3,&TIM_OCInitStructure);//定时器和通道设置函数
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能ccr预装载器
TIM_Cmd(TIM3,ENABLE);//定时器使能
}
http://www.openedv.com/thread-65127-1-1.html
(出处: OpenEdv-开源电子网)
网友评论