|
我最近暑假得空做一下智能小車,這個藍牙控制是其中一部分,后面我會再添加oled,wifi和語音等模塊
帖子里面包括主函數和藍牙的部分代碼,整個工程詳細代碼我放在附件里面,你們有需要可以下載。里面代碼親試有效,有疑問歡迎來我帖子下面留言,我會一一回復的。
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "pwm.h"
- #include "Motor.h"
- #include "key.h"
- #include "BlueTooth.h"
- #include "Engine.h"
- #include "ultrasonic.h"
- int main(void)
- {
- Motor_Init(); //電機初始化
- delay_init();
- time3_initer(65535,7); //pwm初始化
- BlueBooth_Init(); //藍牙初始化
- sg90_Init(); //舵機初始化
- Wave_IO_Init(); //超聲波初始化
- while(1)
- {
復制代碼 藍牙初始化代碼
- #include "BlueTooth.h"
- #include "stm32f10x.h"
- #include "motor.h"
- #include "pwm.h"
- #include "Engine.h"
- #include "ultrasonic.h"
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- u8 flag,i;
- void BlueBooth_Init(void) //藍牙初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
-
- // USART_DeInit(USART1);
- // USART1_TX PA.9 //串口發送
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //gpio初始化
-
- //USART1_RX PA.10 //串口接收
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //gpio初始化
- USART_InitStructure.USART_BaudRate = 9600; //串口波特率
-
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長為8位數據格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //一位停止位
- USART_InitStructure.USART_Parity = USART_Parity_No; //無奇偶校驗和
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無硬件數據流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發模式
- USART_Init(USART1, &USART_InitStructure); //串口1初始化
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //開始串口接收中斷
- //Usart1 NVIC
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //分組0
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//3位搶占優先級
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //3位子優先級
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ中斷使能
- NVIC_Init(&NVIC_InitStructure); //NVIC初始化
-
- USART_Cmd(USART1, ENABLE); //串口初始化
復制代碼
下面這個是我上一篇帖子,里面是超聲波加舵機的測距代碼,里面有詳細工程,你們也可以看一下
http://m.zg4o1577.cn/bbs/dpj-210799-1.html
|
評分
-
查看全部評分
|