STM32F3 DISCOVERY的USART无法工作

3
我正在尝试将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--;
  }
}
1个回答

1

你应该使用

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

(好的,STMicro新的头文件有不同的命名规则,但我认为很容易找到匹配项) 而不是
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

使引脚连接到外设(USART),而不是直接IO。
除此之外,你的代码还有一些看起来奇怪的特性,比如在irq处理程序中使用非易失性TimeDecrement或Delay(除非irq的优先级设置正确,否则这将无效),但我希望你知道自己在做什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接