你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F030C8的USART2的配置问题 发送不出去

[复制链接]
luoluo1122 提问时间:2017-8-16 11:45 /
第一次用STM32F0系列的MCU,在用STM32F030C8T6的USART2,即PA2和PA3作为串口使用时,始终发送不出去,USART1可以正常使用,USART2的配置与USART1一样,但是USART2不能正常工作,不知道哪里的问题,希望可以得到大神的帮助,谢谢!源码如下:void USART1_INIT(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
  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);
}


void USART2_INIT(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
  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(USART2, &USART_InitStructure);
  USART_Cmd(USART2, ENABLE);
}


void UART2_Send(uint8_t *Buffer, uint32_t Length)
{
        while(Length != 0)
        {
                while(!((USART2->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
                USART2->TDR = *Buffer;
                Buffer++;
                Length--;
        }
}

void UART1_Send(uint8_t *Buffer, uint32_t Length)
{
        while(Length != 0)
        {
                while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
                USART1->TDR = *Buffer;
                Buffer++;
                Length--;
        }
}


uint8_t Tx2_Buffer[] = "good";
uint8_t Tx1_Buffer[] = "nice";
#define countof(a) (sizeof(a) / sizeof(*(a)))

int main(void)
{
        NVIC_Configuration();
        SysTick_Start(5);
        USART1_INIT();
        USART2_INIT();

        while (1)
        {
                if (SysTick->CTRL & 0x10000)// 5ms一次任务
                {
                                UART1_Send(Tx1_Buffer, countof(Tx1_Buffer)-1);       
                                UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1);                               
                }
        }
}

在主函数中每隔5ms发送Tx1_Buffer[]和Tx2_Buffer[],但是在进入UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1);之后就在while(!((USART2->ISR)&(1<<7)));这里死掉了,即出不来这个while,谢谢!


收藏 1 评论6 发布时间:2017-8-16 11:45

举报

6个回答
无薪税绵 回答时间:2017-11-1 08:31:50
外设时钟开启没有?  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);  
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

anywill 回答时间:2017-11-1 09:15:55
建议在cubemx试试

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

wenyangzeng 回答时间:2017-11-1 10:28:14
本帖最后由 wenyangzeng 于 2017-11-1 10:33 编辑

串口2在PA2,PA3
无标题.png
static void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}


评分

参与人数 1ST金币 +4 收起 理由
zero99 + 4

查看全部评分

时光虫子 回答时间:2017-11-1 10:51:29
while(!((USART2->ISR)&(1<<6)));改成这样看看

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

wolfgang 回答时间:2017-11-1 14:34:12
4、NUCLEO-L476RG试验三_LED闪闪亮续(定时、串口)

之前曾写过一个Cubemx配置串口的例子,供您参考。
看看你的工程中
接收时,需要设置的函数
HAL_UART_RxCpltCallback(.....)
HAL_UART_Receive_IT

发送时需要设置的内容:
PUTCHAR_PROTOTYPE
用Printf(....)
看看呢?

串口很简单的。

评分

参与人数 1ST金币 +3 收起 理由
zero99 + 3

查看全部评分

wangbinyan188 回答时间:2017-11-1 15:35:10
需要清除相应的发送中断标志位,才能发送下一BIT

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版