|
pwm.c
motor.c内容
单片机源程序如下:
- #include "pwm.h"
- static void PWM_GPIO_Config(void)
- {
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- static void PWM_TIM_Config(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
-
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = (1000-1);
- TIM_TimeBaseInitStructure.TIM_Prescaler = (72-1);
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse = 500;
- TIM_OC1Init(TIM2,&TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
-
- TIM_Cmd(TIM2, ENABLE);
-
- }
- void TIM_Init(void)
- {
- PWM_GPIO_Config();
- PWM_TIM_Config();
- }
复制代码
所有资料51hei提供下载:
pwm独立通道.7z
(172.89 KB, 下载次数: 103)
|
评分
-
查看全部评分
|