STM32F3 DISCOVERY USART 不工作

Posted

技术标签:

【中文标题】STM32F3 DISCOVERY USART 不工作【英文标题】:STM32F3 DISCOVERY USART doesn't work 【发布时间】:2015-07-06 03:55:43 【问题描述】:

我正在尝试将 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】:

你应该使用

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

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

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

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

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

【讨论】:

以上是关于STM32F3 DISCOVERY USART 不工作的主要内容,如果未能解决你的问题,请参考以下文章

stm32f3发现usart没有发送

l3gd20 陀螺仪在 stm32f3discovery 上发回重复字节

我的 EXTI0 中断处理程序没有被覆盖/工作正常(STM32F3Discovery)

stm32f0 uart编程

STM32F3 发现 - 实现 GPIO 中断

STM32F429 定时器触发 USART DMA 传输问题