Robot_Bluetooth/App/bluetooth/bt.c

107 lines
3.1 KiB
C
Raw Normal View History

2024-04-17 01:28:33 +08:00
#include "bt.h"
char RxPacket[17];
unsigned char flag;
char open[] = "o";
char close[] = "c";
void uart1_init(u32 bound)
{
//GPIO<49>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹ<><CAB9>UGPIOAʱ<41><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ʹ<><CAB9>USART1ʱ<31><CAB1>
//USART1_TX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB,&GPIO_InitStructure);
//USART1_RX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;//PA10
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB,&GPIO_InitStructure);
//Usart1 NVIC <20><><EFBFBD><EFBFBD>
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //<2F><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //IRQͨ<51><CDA8>ʹ<EFBFBD><CAB9>
NVIC_Init(&NVIC_InitStructure); //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>VIC<49>Ĵ<EFBFBD><C4B4><EFBFBD>
//USART <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_BaudRate=bound;//<2F><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//<2F>ֳ<EFBFBD>Ϊ8λ<38><CEBB><EFBFBD>ݸ<EFBFBD>ʽ
USART_InitStructure.USART_StopBits=USART_StopBits_1;//һ<><D2BB>ֹͣλ
USART_InitStructure.USART_Parity=USART_Parity_No;//<2F><><EFBFBD><EFBFBD>żУ<C5BC><D0A3>λ
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//<2F><>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //<2F>շ<EFBFBD>ģʽ
USART_Init(USART1,&USART_InitStructure); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
USART_Cmd(USART1,ENABLE); //ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>1
}
void BT_Send(char *string)
{
flag=0;
printf("%s",string);
while(flag==0);
}
void BT_Init(void)
{
uart1_init(115200);
BT_Send("AT+BAUD8");
BT_Send("AT");
BT_Send("AT+VERSION");
BT_Send("AT+NAMEJOJO");
BT_Send("AT+PIN1234");
OLED_ShowString(3,1,"OK");
}
int BT_Judge(void)
{
if(strcmp(RxPacket,open))
return 1;
else
return 0;
}
/**************************************************************************
Function: Receive interrupt function
Input : none
Output : none
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**************************************************************************/
void USART1_IRQHandler(void)
{
static unsigned char i=0;
unsigned char clear;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
RxPacket[i++]=USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
else if((USART_GetITStatus(USART1,USART_IT_IDLE)!=RESET))
{
RxPacket[16]=0x00;
OLED_ShowString(2,1,RxPacket);
i=0;
memset(RxPacket,' ',17);
clear=USART1->SR;
clear=USART1->DR;
flag=1;
}
}
int fputc(int ch,FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR=(unsigned char)ch;
return ch;
}