我无法将 USART_1 与蓝牙 hc-05 连接。微控制器是STM32F103RB。这是代码(我没有使用任何库)
#define APB2_FREQ 72000000
void USART1_Send(char* data)
{
unsigned int length = 0;
while(data[length] != '\0')
++length;
int i = 0;
while(i < length)
{
while((USART1_SR & 0x00000080) == 0); // wait if transmit data register is not empty
USART1_DR = data[i];
++i;
}
}
char USART1_Receive()
{
while((USART1_SR & 0x00000020) == 0); //wait for data to arrive
return USART1_DR;
}
void USART1_Init(int baudrate, short parity, short stop)
{
RCC_APB2ENR |= 0x00004004; // enable Port A, enable usart1 clock
GPIOA_CRH &= 0xFFFFF00F;
GPIOA_CRH |= 0x000004A0; // pin 9 = alternate function push-pull, pin 10 = Floating Input
USART1_CR1 &= 0x00000000; // resetting the parity bit to 0(which is no parity) and "M bit" to 0(which is 8 bit data)
if(parity == 1) // even parity
USART1_CR1 |= 0x00000400;
else if(parity == 2)
USART1_CR1 |= 0x00000600; // odd parity
USART1_CR2 &= 0x00000000; // Reset USART_CR2, 1 stop bit
if(stop == 2)
USART1_CR2 |= 0x00002000; // 2 stop bit
USART1_BRR = APB2_FREQ /(baudrate);
USART1_CR1 |= 0x00002000; // enable UART
USART1_CR1 |= 0x0000000C; // enable Transmiter, receiver
USART1_Send("LINK OK:\r\n");
}
int main(void)
{
USART1_Init(9600, 0, 1); // 9600 baudrate, no parity, 1 stop bit
while(1){
USART1_Send("Hello World\n");
}
return (1);
}
上述程序,配置 USART1 并发送 Hello World。当我在 Keil uvision4 中运行代码时,uart1 窗口重复打印 Hello World。
但是,当我在 STM32F103RB 中烧写代码,并将其与蓝牙连接并将蓝牙与我的手机配对时,它不会在手机的蓝牙终端应用程序上显示任何内容。
这是我连接电线的方法,
- 蓝牙接地 > STM32 接地
- 蓝牙 5v > STM32 5v
- 蓝牙接收 > STM32 PA9
- 蓝牙发送 > STM32 PA10
我用 arduino 试过同样的蓝牙,效果很好。
谢谢!!!