gpt4 book ai didi

bluetooth - STM32F3 DISCOVERY USART 不工作

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

我正在尝试将 HC-05 与我的 STM32F3 DISCOVERY 结合使用,但我无法让 USART 正常工作。

无论我手动从 HC-05 读取数据还是使用中断,它都不起作用。

我尝试在 arduino 上运行这个蓝牙模块,它在第一次尝试时运行正常,所以 HC-05 正在运行。

这是我的代码,如果你们能看一下并找出任何错误,我将不胜感激。

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f30x_usart.h"
#include "stm32f30x_rcc.h"


/** @addtogroup STM32F3_Discovery_Peripheral_Examples
* @{
*/

/** @addtogroup GPIO_IOToggle
* @{
*/

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BSRR_VAL 0xC000
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
static __IO uint32_t TimingDelay;
volatile uint16_t usart_buffer = 0;



volatile char usartMessage[] = "message";

/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nTime);
void USART1_IRQHandler(void);

/* Private functions ---------------------------------------------------------*/
void USART_print (USART_TypeDef* USARTx, volatile char *buffer)
{
/* transmit till NULL character is encountered */
while(*buffer)
{
USART_SendData(USARTx, *buffer++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);

}
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);


/* Configure USART1 pins: --------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);

USART_Cmd(USART1, ENABLE);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the priority group of the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART2 interrupts are globally enabled
NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff

// finally this enables the complete USART1 peripheral
USART_Cmd(USART1, ENABLE);


if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}

STM_EVAL_LEDInit(LED5);



while (1)
{
int i = USART_ReceiveData(USART1);
if(i == '1'){
USART_print(USART1, "messsage");
}
/*for(i=0; usartMessage[i] != 0; i++){

USART_print(USART1, &usartMessage[i]);
}*/
Delay(200);

}
}




void USART1_IRQHandler(void){
Delay(100);
USART_print(USART1, "message INTERRUPT!");
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
usart_buffer = USART_ReceiveData(USART1);

}
}

void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;

while(TimingDelay != 0);
}

/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}

最佳答案

你应该使用

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

(好吧,STMicro 新 header 有不同的命名方案,但我认为很容易找到匹配项)而不是

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

使引脚连接到外围设备 (USART),而不是直接 IO。

除此之外,您的代码还有一些看起来很奇怪的功能,例如在 irq 处理程序中使用非 volatile TimeDecrement 或 Delay(除非正确设置 irq 的优先级,否则它们不会工作),但我希望您知道自己在做什么。

关于bluetooth - STM32F3 DISCOVERY USART 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900105/

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