标题: 基于STC15W单片机平衡车程序+PCB工程设计 [打印本页]

作者: 安安哒多所    时间: 2019-7-28 10:11
标题: 基于STC15W单片机平衡车程序+PCB工程设计
有原理图,PCB板,程序源码
Altium Designer画的PCB图如下:(51hei附件中可下载工程文件)


单片机源程序如下:
  1. /*********************************************************/       
  2. #include "config.h"


  3. /*************        本地函数声明        **************/
  4. //========================================================================
  5. // 函数: void  delay_ms(u8 ms)
  6. // 描述: 延时函数。
  7. // 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
  8. // 返回: none.
  9. // 版本: VER1.0
  10. // 日期:  
  11. // 备注:
  12. //========================================================================
  13. void  delay_ms(u8 ms)
  14. {
  15.      u16   i;
  16.          do{
  17.               i = MAIN_Fosc / 13000;
  18.                   while(--i)        ;   //14T per loop
  19.      }while(--ms);
  20. }


  21. /************************ 定时器配置 ****************************/
  22. void        Timer_config(void)
  23. {
  24.         TIM_InitTypeDef                TIM_InitStructure;                                        //结构定义
  25.         TIM_InitStructure.TIM_Mode      = TIM_16BitAutoReload;        //指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
  26.         TIM_InitStructure.TIM_Polity    = PolityLow;                        //指定中断优先级, PolityHigh,PolityLow
  27.         TIM_InitStructure.TIM_Interrupt = ENABLE;                                //中断是否允许,   ENABLE或DISABLE
  28.         TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T;                        //指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
  29.         TIM_InitStructure.TIM_ClkOut    = DISABLE;                                //是否输出高速脉冲, ENABLE或DISABLE
  30.         TIM_InitStructure.TIM_Value     = 65536UL - (MAIN_Fosc / 480);                //1ms初值,
  31.         TIM_InitStructure.TIM_Run       = ENABLE;                                //是否初始化后启动定时器, ENABLE或DISABLE
  32.         Timer_Inilize(Timer0,&TIM_InitStructure);                                //初始化Timer0          Timer0,Timer1,Timer2,Timer3,Timer4
  33.        
  34.         TIM_InitStructure.TIM_Mode      = TIM_16BitAutoReload;        //指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
  35.         TIM_InitStructure.TIM_Polity    = PolityLow;                        //指定中断优先级, PolityHigh,PolityLow
  36.         TIM_InitStructure.TIM_Interrupt = ENABLE;                                //中断是否允许,   ENABLE或DISABLE
  37.         TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T;                        //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
  38.         TIM_InitStructure.TIM_ClkOut    = DISABLE;                                //是否输出高速脉冲, ENABLE或DISABLE
  39.         TIM_InitStructure.TIM_Value     = 65536UL - (MAIN_Fosc / 1200);                //10ms初值,
  40.         TIM_InitStructure.TIM_Run       = ENABLE;                                //是否初始化后启动定时器, ENABLE或DISABLE
  41.         Timer_Inilize(Timer1,&TIM_InitStructure);                                //初始化Timer1          Timer0,Timer1,Timer2

  42.         TIM_InitStructure.TIM_Interrupt = ENABLE;                                //中断是否允许,   ENABLE或DISABLE. (注意: Timer2固定为16位自动重装, 中断固定为低优先级)
  43.         TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_Ext;                //指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
  44.         TIM_InitStructure.TIM_ClkOut    = DISABLE;                                //是否输出高速脉冲, ENABLE或DISABLE
  45.         TIM_InitStructure.TIM_Value     = 0;                //初值
  46.         TIM_InitStructure.TIM_Run       = ENABLE;                                //是否初始化后启动定时器, ENABLE或DISABLE
  47.         Timer_Inilize(Timer3,&TIM_InitStructure);                                //初始化Timer3          Timer0,Timer1,Timer2,Timer3,Timer4

  48.         TIM_InitStructure.TIM_Interrupt = ENABLE;                                //中断是否允许,   ENABLE或DISABLE. (注意: Timer2固定为16位自动重装, 中断固定为低优先级)
  49.         TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_Ext;                //指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
  50.         TIM_InitStructure.TIM_ClkOut    = DISABLE;                                //是否输出高速脉冲, ENABLE或DISABLE
  51.         TIM_InitStructure.TIM_Value     = 0;                //初值
  52.         TIM_InitStructure.TIM_Run       = ENABLE;                                //是否初始化后启动定时器, ENABLE或DISABLE
  53.         Timer_Inilize(Timer4,&TIM_InitStructure);                                //初始化Timer4          Timer0,Timer1,Timer2,Timer3,Timer4
  54. }

  55. /***************** 串口配置函数 *****************************/
  56. void        UART_config(void)
  57. {
  58.         COMx_InitDefine                COMx_InitStructure;                                        //结构定义
  59.         COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,       UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
  60.         COMx_InitStructure.UART_BRT_Use   = BRT_Timer2;                        //使用波特率,   BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
  61.         COMx_InitStructure.UART_BaudRate  = 9600ul;                                //波特率, 一般 110 ~ 115200
  62.         COMx_InitStructure.UART_RxEnable  = ENABLE;                                //接收允许,   ENABLE或DISABLE
  63.         COMx_InitStructure.UART_Interrupt = ENABLE;                                //中断允许,   ENABLE或DISABLE
  64.         COMx_InitStructure.UART_P_SW      = UART1_SW_P30_P31;        //切换端口,   UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必须使用内部时钟)
  65.         USART_Configuration(USART1, &COMx_InitStructure);                //初始化串口1 USART1,USART2,USART3,USART4

  66. //        COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,       UART_8bit_BRTx,UART_9bit_BRTx
  67. //        COMx_InitStructure.UART_BaudRate  = 9600ul;                        //波特率,     110 ~ 115200
  68. //        COMx_InitStructure.UART_RxEnable  = ENABLE;                                //接收允许,   ENABLE或DISABLE
  69. //        COMx_InitStructure.UART_Interrupt = ENABLE;                                //中断允许,   ENABLE或DISABLE
  70. //        COMx_InitStructure.UART_P_SW      = UART2_SW_P10_P11;        //切换端口,   UART2_SW_P10_P11, UART2_SW_P46_P47
  71. //        USART_Configuration(USART2, &COMx_InitStructure);                //初始化串口2 USART1,USART2,USART3,USART4

  72.         COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,       UART_8bit_BRTx,UART_9bit_BRTx
  73.         COMx_InitStructure.UART_BRT_Use   = BRT_Timer2;                        //使用波特率, BRT_Timer3, BRT_Timer2
  74.         COMx_InitStructure.UART_BaudRate  = 9600ul;                        //波特率,     110 ~ 115200
  75.         COMx_InitStructure.UART_RxEnable  = ENABLE;                                //接收允许,   ENABLE或DISABLE
  76.         COMx_InitStructure.UART_Interrupt = ENABLE;                                //中断允许,   ENABLE或DISABLE
  77.         COMx_InitStructure.UART_P_SW      = UART3_SW_P00_P01;        //切换端口,   UART3_SW_P00_P01, UART3_SW_P50_P51
  78.         USART_Configuration(USART3, &COMx_InitStructure);                //初始化串口2 USART1,USART2,USART3,USART4

  79. //        COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,       UART_8bit_BRTx,UART_9bit_BRTx
  80. //        COMx_InitStructure.UART_BRT_Use   = BRT_Timer4;                        //使用波特率, BRT_Timer4, BRT_Timer2
  81. //        COMx_InitStructure.UART_BaudRate  = 9600ul;                        //波特率,     110 ~ 115200
  82. //        COMx_InitStructure.UART_RxEnable  = ENABLE;                                //接收允许,   ENABLE或DISABLE
  83. //        COMx_InitStructure.UART_Interrupt = ENABLE;                                //中断允许,   ENABLE或DISABLE
  84. //        COMx_InitStructure.UART_P_SW      = UART4_SW_P02_P03;        //切换端口,   UART4_SW_P02_P03, UART4_SW_P52_P53
  85. //        USART_Configuration(USART4, &COMx_InitStructure);                //初始化串口4 USART1,USART2,USART3,USART4

  86. }

  87. /**********************************************/
  88. void main(void)
  89. {
  90.         SYSTEM_CLK_1T();
  91.         P0n_standard(0xff);        //设置为准双向口
  92.         P1n_standard(0xff);        //设置为准双向口
  93.         P2n_standard(0xff);        //设置为准双向口
  94.         P3n_standard(0xff);        //设置为准双向口
  95.         P4n_standard(0xff);        //设置为准双向口
  96.         P5n_standard(0xff);        //设置为准双向口
  97.         UART_config();        //串口初始化
  98.         Timer_config();  //定时器0,1初始化
  99.         PWMInit();
  100.         MPU6050Init();
  101.         CarUpstandInit();       
  102.         EA = 1;
  103.         delay_ms(10);
  104.         PrintString(USART1,"STC15W4K32S4 USART1 Ready!\r\n");
  105.         while (1)
  106.         {
  107.                    UART_handle(); //处理串口和蓝牙接收数据
  108.         }
  109. }
复制代码

所有资料51hei提供下载:
两轮平衡车.zip (346.44 KB, 下载次数: 90)
双面板 AD19.zip (2.03 MB, 下载次数: 67)



作者: xx不放火    时间: 2020-3-11 21:52
电机驱动用的啥?8825?
作者: murenxin0911    时间: 2020-3-13 13:25
终于找到了,很期待的!
作者: like123    时间: 2020-6-13 23:18
我好奇的问问这个电机转速怎么选,最好选多大的




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1