STM32F3发现USART无法正常工作

时间:2015-04-27 15:37:29

标签: bluetooth interrupt stm32 usart

我试图在我的STM32F3 DISCOVERY中使用HC-05,但我在使用USART时遇到问题。

无论我是手动从HC-05读取数据还是使用interupts,它都无法正常工作。

我试图在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 个答案:

答案 0 :(得分:0)

你应该使用

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

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

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

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

除此之外,你的代码有一些看起来很奇怪的功能,比如在irq处理程序中使用非易失性TimeDecrement或Delay(除非正确设置irq的优先级,否则无效),但我希望你知道你在做什么。

相关问题