標(biāo)題:
基于stm32f103c8板子的小車的電機(jī)驅(qū)動(dòng)源碼
[打印本頁(yè)]
作者:
POTATO
時(shí)間:
2018-7-7 10:36
標(biāo)題:
基于stm32f103c8板子的小車的電機(jī)驅(qū)動(dòng)源碼
這是基于stm32f103c8板子的小車的電機(jī)驅(qū)動(dòng)的keil工程源代碼,這里是直流電機(jī),由B6,B7,B8,B9四個(gè)管口輸出PWM波來(lái)控制,前兩個(gè)管口與B電機(jī)相關(guān),后兩個(gè)與A電機(jī)相關(guān),兩個(gè)口的PWM波占空比不同,產(chǎn)生壓差,使電機(jī)轉(zhuǎn)動(dòng)
單片機(jī)源程序如下:
#include "PWM.h"
#include "stm32f10x.h"
void TIM4_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);
//GPIO_InitTypeDef GPIO_InitStructure;
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_SetBits(GPIOB , GPIO_Pin_6);
// GPIO_SetBits(GPIOB , GPIO_Pin_7);
// GPIO_SetBits(GPIOB , GPIO_Pin_8);
// GPIO_SetBits(GPIOB , GPIO_Pin_9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//對(duì)應(yīng)BIN2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//對(duì)應(yīng)BIN1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//對(duì)應(yīng)AIN2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//對(duì)應(yīng)AIN1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 5000 ;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;//TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM4 , &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4 , TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4 , ENABLE);
TIM_CtrlPWMOutputs(TIM4 , ENABLE);
TIM_Cmd(TIM4 , ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM4 , &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4 , TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4 , ENABLE);
TIM_CtrlPWMOutputs(TIM4 , ENABLE);
TIM_Cmd(TIM4 , ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC3Init(TIM4 , &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4 , TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4 , ENABLE);
TIM_CtrlPWMOutputs(TIM4 , ENABLE);
TIM_Cmd(TIM4 , ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC4Init(TIM4 , &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4 , TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4 , ENABLE);
TIM_CtrlPWMOutputs(TIM4 , ENABLE);
TIM_Cmd(TIM4 , ENABLE);
//TIM1->CCR1 = 200;
}
復(fù)制代碼
所有資料51hei提供下載:
驅(qū)動(dòng)電機(jī).rar
(390.61 KB, 下載次數(shù): 36)
2018-7-7 17:35 上傳
點(diǎn)擊文件名下載附件
驅(qū)動(dòng)電機(jī)
下載積分: 黑幣 -5
作者:
programc3
時(shí)間:
2018-9-8 10:24
很不錯(cuò)
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1