gpt4 book ai didi

stm32 - 无法让STM32F103RB usart1与hc-05通信

转载 作者:行者123 更新时间:2023-12-04 12:18:49 25 4
gpt4 key购买 nike

我无法将 USART_1 与蓝牙 hc-05 连接。微 Controller 是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。

enter image description here

但是,当我在STM32F103RB中烧录代码,并连接蓝牙并将蓝牙与我的手机配对时,它在手机的蓝牙终端应用程序上没有显示任何内容。

这是我连接电线的方式,
  • 蓝牙接地 > STM32 接地
  • 蓝牙 5v > STM32 5v
  • 蓝牙接收 > STM32 PA9
  • 蓝牙发送 > STM32 PA10

  • 我已经用 arduino 尝试了相同的蓝牙,它工作正常。

    谢谢!!!

    最佳答案

    知道了...
    微 Controller 以 8 MHz HSI 作为系统时钟运行。

    所以,有几个解决方案,

    解决方法之一是将波特率计算中的时钟频率改为8000000,

    USART1_BRR = 8000000 /(baudrate);



    另一种解决方案是将系统时钟增加到 72MHz。一种方法是将 PLL 配置为将 HSI 或 HSE 的时钟频率倍增至 72 MHz,然后将 PLL 用作系统时钟。

    希望这可以帮助某人。

    关于stm32 - 无法让STM32F103RB usart1与hc-05通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863124/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com