|
我最近暑假得空做一下智能小车,这个蓝牙控制是其中一部分,后面我会再添加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://www.51hei.com/bbs/dpj-210799-1.html
|
评分
-
查看全部评分
|