首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >STM32F0 UART不工作

STM32F0 UART不工作
EN

Stack Overflow用户
提问于 2014-10-19 17:51:05
回答 2查看 4.5K关注 0票数 1

我是新来的。我试图使用UART,所以我编写了这段代码来初始化UART并发送"Hello“。我正在使用STM32F0板。

代码语言:javascript
复制
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_misc.h"
#include "stm32f0xx_usart.h"

#define UINT8               uint8_t
#define UINT32              uint32_t


#define MAX_BUFFER_SIZE     256

typedef struct Buffer_st
{
   UINT8 size;
   UINT8 data[MAX_BUFFER_SIZE];
}Buffer_st;

Buffer_st receivedDataUART1;

void initUART1(UINT32 baudRate)
{
   NVIC_InitTypeDef NVIC_InitStructure;
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;

   /* Enable the USART1 Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);

   //UART init
   USART_InitStructure.USART_BaudRate = baudRate;
   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;

   /* Enable GPIO clock */
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

   /* Enable USART clock */
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

   /* Connect PXx to USARTx_Tx */
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

   /* Connect PXx to USARTx_Rx */
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

   /* Configure USART Tx, Rx as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* USART configuration */
   USART_Init(USART1, &USART_InitStructure);

   /* Enable USART */
   USART_Cmd(USART1, ENABLE);
   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

   resetReceivedDataBufferUART1();
}

void resetReceivedDataBufferUART1(void)
{
   receivedDataUART1.size = 0;
   memset(receivedDataUART1.data, MAX_BUFFER_SIZE, 0);
}

UINT8 sendDataUART1(Buffer_st buffer)
{
  UINT8 cpt;

  for (cpt = 0; cpt < buffer.size; cpt++)
  {
     USART_SendData(USART1, buffer.data[cpt]);

     //Loop until the end of transmission
     while (USART_GetFlagStatus(USART1, USART_IT_TXE) == RESET);
  }
}

void USART1_IRQHandler(void)
{
   if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
   {
       receivedDataUART1.data[receivedDataUART1.size++] = USART_ReceiveData(USART1);
   }
}

int main(void)
{
   Buffer_st buffer = {6,"Hello"};

   initUART1(9600);
   sendDataUART1(buffer);

   while(1);
   return 0;
}

我正在使用UART/USB连接器。问题是我在minicom没有收到任何东西。调试时,我发现方案进入无限循环。

代码语言:javascript
复制
 //Loop until the end of transmission
while (USART_GetFlagStatus(USART1, USART_IT_TXE) == RESET);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-22 08:47:21

您将中断状态标志与用于同步轮询的标志混在一起。要发送数据,必须首先轮询传输数据寄存器空状态的USART_FLAG_TXE标志,然后才能发送字节。USART_IT_TXE标志仅供中断使用。

因此,要发送一个字节,请执行如下操作:

代码语言:javascript
复制
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=SET);
USART_SendData(USART1,myByte);

有关更多信息,请参阅USARTx_ISR寄存器在RM0091中的文档。

票数 3
EN

Stack Overflow用户

发布于 2014-10-19 19:13:41

您使用这样的函数

代码语言:javascript
复制
void USART_write(unsigned char *string){

    while(*string){
        // wait until data register is empty
        while( !(USART1->SR & 0x00000040) );
        USART_SendData(USART1,*string);
        *string++;
    }
}

此代码在STM3VLDiscovery上进行了测试。您必须检查您的芯片的注册状态,如:时间(!(USART1 1->SR& 0x00000040) );

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26453539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档