標(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ī)源程序如下:
  1. #include "PWM.h"
  2. #include "stm32f10x.h"


  3. void TIM4_Init(void)
  4. {
  5.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  6.         GPIO_InitTypeDef GPIO_InitStructure;
  7.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  8.        
  9.         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);
  10.         //GPIO_InitTypeDef GPIO_InitStructure;
  11. //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
  12. //  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. //  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  14. //  GPIO_Init(GPIOB, &GPIO_InitStructure);
  15. //        GPIO_SetBits(GPIOB , GPIO_Pin_6);
  16. //        GPIO_SetBits(GPIOB , GPIO_Pin_7);
  17. //        GPIO_SetBits(GPIOB , GPIO_Pin_8);
  18. //        GPIO_SetBits(GPIOB , GPIO_Pin_9);
  19.        
  20.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//對(duì)應(yīng)BIN2
  21.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  23.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  24.        
  25.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//對(duì)應(yīng)BIN1
  26.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  28.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  29.        
  30.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//對(duì)應(yīng)AIN2
  31.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  32.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  33.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  34.        
  35.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//對(duì)應(yīng)AIN1
  36.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  38.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  39.                  
  40.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
  41.         //TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  42.   TIM_TimeBaseStructure.TIM_Prescaler = 0;   
  43.         TIM_TimeBaseStructure.TIM_Period = 5000 ;
  44.   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;//TIM_CKD_DIV1;          
  45.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  46.   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  47.         //TIM_OCInitTypeDef  TIM_OCInitStructure;
  48.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;        
  49.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  50.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  51.   TIM_OCInitStructure.TIM_Pulse =  2500;     
  52.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  53.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  54.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  55.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  56.   TIM_OC1Init(TIM4 , &TIM_OCInitStructure);
  57.   TIM_OC1PreloadConfig(TIM4 , TIM_OCPreload_Enable);
  58.   TIM_ARRPreloadConfig(TIM4 , ENABLE);   
  59.         TIM_CtrlPWMOutputs(TIM4 , ENABLE);
  60.         TIM_Cmd(TIM4 , ENABLE);       
  61.        
  62.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;        
  63.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  64.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  65.   TIM_OCInitStructure.TIM_Pulse =  0;     
  66.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  67.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  68.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  69.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  70.   TIM_OC2Init(TIM4 , &TIM_OCInitStructure);
  71.   TIM_OC2PreloadConfig(TIM4 , TIM_OCPreload_Enable);
  72.   TIM_ARRPreloadConfig(TIM4 , ENABLE);   
  73.         TIM_CtrlPWMOutputs(TIM4 , ENABLE);
  74.         TIM_Cmd(TIM4 , ENABLE);       
  75.        
  76.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;        
  77.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  78.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  79.   TIM_OCInitStructure.TIM_Pulse =  2500;     
  80.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  81.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  82.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  83.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  84.   TIM_OC3Init(TIM4 , &TIM_OCInitStructure);
  85.   TIM_OC3PreloadConfig(TIM4 , TIM_OCPreload_Enable);
  86.   TIM_ARRPreloadConfig(TIM4 , ENABLE);   
  87.         TIM_CtrlPWMOutputs(TIM4 , ENABLE);
  88.         TIM_Cmd(TIM4 , ENABLE);       
  89.        
  90.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;        
  91.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  92.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  93.   TIM_OCInitStructure.TIM_Pulse =  0;     
  94.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  95.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  96.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  97.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  98.   TIM_OC4Init(TIM4 , &TIM_OCInitStructure);
  99.   TIM_OC4PreloadConfig(TIM4 , TIM_OCPreload_Enable);
  100.   TIM_ARRPreloadConfig(TIM4 , ENABLE);   
  101.         TIM_CtrlPWMOutputs(TIM4 , ENABLE);
  102.         TIM_Cmd(TIM4 , ENABLE);       
  103.         //TIM1->CCR1 = 200;
  104.        
  105. }

復(fù)制代碼

所有資料51hei提供下載:
驅(qū)動(dòng)電機(jī).rar (390.61 KB, 下載次數(shù): 36)




作者: programc3    時(shí)間: 2018-9-8 10:24
很不錯(cuò)




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1