完整源码:
stm32_直流电机.7z
(185.78 KB, 售价: 1 E币)
部分源码:- int main(void)
- {
-
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- Motor_Init();
- while(1)
- {
- Moto_PwmRflash(0,800,0,800);
- }
- }
- //电机
- #define Moto_PwmMax 999
- int16_t MOTO1_PWM = 0;
- int16_t MOTO2_PWM = 0;
- int16_t MOTO3_PWM = 0;
- int16_t MOTO4_PWM = 0;
- void Moto_PwmRflash(int16_t MOTO1_PWM,int16_t MOTO2_PWM,int16_t MOTO3_PWM,int16_t MOTO4_PWM)
- {
- if(MOTO1_PWM>Moto_PwmMax) MOTO1_PWM = Moto_PwmMax;
- if(MOTO2_PWM>Moto_PwmMax) MOTO2_PWM = Moto_PwmMax;
- if(MOTO3_PWM>Moto_PwmMax) MOTO3_PWM = Moto_PwmMax;
- if(MOTO4_PWM>Moto_PwmMax) MOTO4_PWM = Moto_PwmMax;
- if(MOTO1_PWM<0) MOTO1_PWM = 0;
- if(MOTO2_PWM<0) MOTO2_PWM = 0;
- if(MOTO3_PWM<0) MOTO3_PWM = 0;
- if(MOTO4_PWM<0) MOTO4_PWM = 0;
-
- TIM4->CCR3 = MOTO4_PWM;//motor4 L U
- TIM4->CCR4 = MOTO3_PWM;//motor1 R U
- TIM3->CCR3 = MOTO2_PWM;//motor2 R D
- TIM3->CCR4 = MOTO1_PWM;//motor3 L D
- }
- void Time3_Mode_Config()
- {
- uint16_t CCR_Val=800;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
- TIM_OCInitTypeDef TIM_OCInitStruct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- TIM_TimeBaseStruct.TIM_Period=999;
- TIM_TimeBaseStruct.TIM_Prescaler=(uint16_t) (SystemCoreClock / 24000000) - 1;
- TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStruct.TIM_ClockDivision = 0;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
-
-
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStruct.TIM_Pulse=CCR_Val;
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC3Init(TIM3, &TIM_OCInitStruct);
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_OC4Init(TIM3, &TIM_OCInitStruct);
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3,ENABLE);
- TIM_Cmd(TIM3,ENABLE);
-
- }
- void Time4_Mode_Config()
- {
- uint16_t CCR_Val=800;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
- TIM_OCInitTypeDef TIM_OCInitStruct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- TIM_TimeBaseStruct.TIM_Period=999;
- TIM_TimeBaseStruct.TIM_Prescaler=(uint16_t) (SystemCoreClock / 24000000) - 1;
- TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStruct.TIM_ClockDivision = 0;
- TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStruct);
-
-
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStruct.TIM_Pulse=CCR_Val;//配置占空比的值
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC3Init(TIM4, &TIM_OCInitStruct);
- TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC4Init(TIM4, &TIM_OCInitStruct);
- TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM4,ENABLE);
- TIM_Cmd(TIM4,ENABLE);
-
- }
- void Motor_GPIO_Config()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8|GPIO_Pin_9;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP ;
-
- GPIO_Init(GPIOB,&GPIO_InitStruct);
- }
- void Motor_Init(void)
- {
- Motor_GPIO_Config();
- Time3_Mode_Config();
- Time4_Mode_Config();
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|