找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6964|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

基于STM32的平衡車程序設(shè)計

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:463841 發(fā)表于 2019-1-14 03:42 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
STM32資源使用說明:

1、串口:usart1用于串口調(diào)試。 相關(guān)引腳:PA9、PA10.
         usart3用于藍牙通信。 相關(guān)引腳:PB10、PB11.
2、定時器:timer1用于進中斷控制(采集數(shù)據(jù)、PID參數(shù)調(diào)節(jié))。相關(guān)引腳:
           timer2用于編碼模式測量左電機速度。相關(guān)引腳:PA0、PA1.
           timer3用于產(chǎn)生兩路PWM波驅(qū)動電機。相關(guān)引腳:PB0、PB1.
           timer4用于編碼模式測量右電機速度。相關(guān)引腳:PB6、PB7.
3、模擬IIC: 用于陀螺儀和單片機之間的數(shù)據(jù)通信。相關(guān)引腳:PB8、PB9.
4、模擬SPI:用于oled和單片機之間的數(shù)據(jù)通信。相關(guān)引腳:PB12、PB13、PB14、PB15、PA8.


引腳連接說明:

1、5個VCC,5個GND。
2、PB0、PB1分別接TB6612上的PWMA、PWMB。
3、PA2、PA3分別接TB6612上的BIN1、BIN2。
   PA6、PA5分別接TB6612上的AIN1、AIN2。
   PA4接在TB6612上的STBY。
4、PB4、PB3、PA15、PA12、PA11分別接在5個循跡模塊的D0口。
5、PB12、PB13、PB14、PB15、PA8分別接在oled模塊上的D0、D1、RES、DC、CS。

單片機源程序如下:
  1. #include "stdio.h"
  2. #include "stm32f10x.h"
  3. #include "delay.h"

  4. /**************************用戶頭文件區(qū)******************************/
  5. #include "bsp_usart1.h"
  6. #include "bsp_usart3.h"
  7. #include "bsp_timer1_irq.h"
  8. #include "bsp_timer2_encoder.h"
  9. #include "bsp_timer3_pwm_ch1234.h"
  10. #include "bsp_timer4_encoder.h"
  11. #include "bsp_timer6_irq.h"
  12. #include "bsp_IIC.h"
  13. #include "bsp_NVIC.h"

  14. #include "filter.h"
  15. #include "mpu6050.h"
  16. #include "led.h"
  17. #include "tb6612.h"
  18. #include "line_finding.h"
  19. #include "control.h"
  20. #include "oled_small.h"


  21. /*********************************變量區(qū)***************************/
  22. //extern int Encoder_Left,Encoder_Right;

  23. /*****************************函數(shù)區(qū)******************************/
  24. int  main(void)
  25. {        
  26.   SystemInit();                                                       //配置系統(tǒng)時鐘(72M)
  27.         delay_init();
  28.         bsp_usart1_config(115200);                                          //串口一用作調(diào)試  115200  與模擬IIC配合,只能用115200
  29. //        bsp_usart3_config(9600);                                                //串口三用作藍牙通信 (與藍牙匹配 9600)

  30. #if 0        
  31. /********************循跡部分**********************/
  32.         TIM3_pwm_init(7199,0);                                              //其中兩路通道分別產(chǎn)生兩路PWM波用以驅(qū)動兩個電機
  33.         pid_init();                                                                                                                //PID相關(guān)參數(shù)初始化
  34.   TIM6_irq_Init(49,7199);                                             //5ms進一次中斷服務(wù)進行參數(shù)采集與控制( T= ( (7199+1)/72M * (99+1) ) )
  35.         LED_Init();
  36.         TB6612_Init();                                                                                          //電機驅(qū)動模塊初始化
  37.         line_finding_Init();                                                                  //各循跡模塊初始化
  38.         
  39. #endif
  40.         
  41. /*******************添加平衡功能********************/        
  42. #if 1
  43.                
  44.           IIC_Init();                                                                                                          //模擬IIC實現(xiàn)MPU6050與單片機之間的數(shù)據(jù)通信  
  45.                 MPU6050_initialize();                                                                        //MPU6050角度傳感器資源初始化        
  46.                 DMP_Init();                                             //MPU6050內(nèi)置DMP資源初始化                        
  47.                 TB6612_Init();                                                                                          //電機驅(qū)動模塊初始化
  48.                 pid_init();                                                                                                                //PID相關(guān)參數(shù)初始化
  49.           target_init();                                                                                                //控制對象的目標賦初值
  50.                 TIM1_irq_Init(49,7199);                           //5ms進一次中斷服務(wù)進行參數(shù)采集與控制( T= (  (7199+1)/72M * (99+1) ) )        
  51.                 TIM3_pwm_init(1999,71);                                              //其中兩路通道分別產(chǎn)生兩路PWM ch3\ch4 波用以驅(qū)動兩個電機        
  52.     TIM2_Encoder_Init();                                                                        //用于編碼測速        
  53.     TIM4_Encoder_Init();                                                                                
  54.                 line_finding_Init();                                                                  //各循跡模塊初始化
  55.                 OLED_Init();                                                                                                        //oled顯示屏初始化
  56.                
  57.                 printf("\r\n初始化配置配置完成\r\n");               
  58.                
  59. #endif
  60. /***************************PWM波賦初值***************************/        
  61.                 TIM_SetCompare3(TIM3,initail_pwm);                                //占空比為:x/7200
  62.                 TIM_SetCompare4(TIM3,initail_pwm);
  63.                 go_straight();
  64.                 while(1)
  65.                 {
  66.                                  line_finding_start(); //循跡
  67.                            my_oled_show();
  68.                 }
  69.         
  70. }
復(fù)制代碼

所有資料51hei提供下載:
平衡小車(succed).7z (256.84 KB, 下載次數(shù): 85)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏5 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:260263 發(fā)表于 2019-10-16 11:42 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報

板凳
ID:221003 發(fā)表于 2020-3-27 14:46 | 只看該作者
學習路過
回復(fù)

使用道具 舉報

地板
ID:760164 發(fā)表于 2020-5-24 21:51 | 只看該作者
有電路圖和報告說明書嗎?
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表