有原理图,PCB板,程序源码
Altium Designer画的PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- /*********************************************************/
- #include "config.h"
- /************* 本地函数声明 **************/
- //========================================================================
- // 函数: void delay_ms(u8 ms)
- // 描述: 延时函数。
- // 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
- // 返回: none.
- // 版本: VER1.0
- // 日期:
- // 备注:
- //========================================================================
- void delay_ms(u8 ms)
- {
- u16 i;
- do{
- i = MAIN_Fosc / 13000;
- while(--i) ; //14T per loop
- }while(--ms);
- }
- /************************ 定时器配置 ****************************/
- void Timer_config(void)
- {
- TIM_InitTypeDef TIM_InitStructure; //结构定义
- TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- TIM_InitStructure.TIM_Polity = PolityLow; //指定中断优先级, PolityHigh,PolityLow
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 480); //1ms初值,
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
-
- TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- TIM_InitStructure.TIM_Polity = PolityLow; //指定中断优先级, PolityHigh,PolityLow
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1200); //10ms初值,
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer1,&TIM_InitStructure); //初始化Timer1 Timer0,Timer1,Timer2
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE. (注意: Timer2固定为16位自动重装, 中断固定为低优先级)
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_Ext; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 0; //初值
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer3,&TIM_InitStructure); //初始化Timer3 Timer0,Timer1,Timer2,Timer3,Timer4
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE. (注意: Timer2固定为16位自动重装, 中断固定为低优先级)
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_Ext; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 0; //初值
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer4,&TIM_InitStructure); //初始化Timer4 Timer0,Timer1,Timer2,Timer3,Timer4
- }
- /***************** 串口配置函数 *****************************/
- void UART_config(void)
- {
- COMx_InitDefine COMx_InitStructure; //结构定义
- COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
- COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
- COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 一般 110 ~ 115200
- COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
- COMx_InitStructure.UART_P_SW = UART1_SW_P30_P31; //切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必须使用内部时钟)
- USART_Configuration(USART1, &COMx_InitStructure); //初始化串口1 USART1,USART2,USART3,USART4
- // COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_8bit_BRTx,UART_9bit_BRTx
- // COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 110 ~ 115200
- // COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- // COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
- // COMx_InitStructure.UART_P_SW = UART2_SW_P10_P11; //切换端口, UART2_SW_P10_P11, UART2_SW_P46_P47
- // USART_Configuration(USART2, &COMx_InitStructure); //初始化串口2 USART1,USART2,USART3,USART4
- COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_8bit_BRTx,UART_9bit_BRTx
- COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //使用波特率, BRT_Timer3, BRT_Timer2
- COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 110 ~ 115200
- COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
- COMx_InitStructure.UART_P_SW = UART3_SW_P00_P01; //切换端口, UART3_SW_P00_P01, UART3_SW_P50_P51
- USART_Configuration(USART3, &COMx_InitStructure); //初始化串口2 USART1,USART2,USART3,USART4
- // COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_8bit_BRTx,UART_9bit_BRTx
- // COMx_InitStructure.UART_BRT_Use = BRT_Timer4; //使用波特率, BRT_Timer4, BRT_Timer2
- // COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 110 ~ 115200
- // COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- // COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
- // COMx_InitStructure.UART_P_SW = UART4_SW_P02_P03; //切换端口, UART4_SW_P02_P03, UART4_SW_P52_P53
- // USART_Configuration(USART4, &COMx_InitStructure); //初始化串口4 USART1,USART2,USART3,USART4
- }
- /**********************************************/
- void main(void)
- {
- SYSTEM_CLK_1T();
- P0n_standard(0xff); //设置为准双向口
- P1n_standard(0xff); //设置为准双向口
- P2n_standard(0xff); //设置为准双向口
- P3n_standard(0xff); //设置为准双向口
- P4n_standard(0xff); //设置为准双向口
- P5n_standard(0xff); //设置为准双向口
- UART_config(); //串口初始化
- Timer_config(); //定时器0,1初始化
- PWMInit();
- MPU6050Init();
- CarUpstandInit();
- EA = 1;
- delay_ms(10);
- PrintString(USART1,"STC15W4K32S4 USART1 Ready!\r\n");
- while (1)
- {
- UART_handle(); //处理串口和蓝牙接收数据
- }
- }
复制代码
所有资料51hei提供下载:
两轮平衡车.zip
(346.44 KB, 下载次数: 90)
|