ST MCU Finder
安装免费手机应用,
寻找理想的ST MCU

[STM32F0] 接收PC端发送的数据

[复制链接]
411|43
| 2019-11-10 16:43 | 显示全部楼层
楼主程序可以公开吗?贴程序看下吧,这么说看不出什么原因

使用特权

评论回复
 楼主 | 2019-11-10 16:47 | 显示全部楼层

#include "main.h"
/** @addtogroup IO_Toggle
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
uint8_t __IO BlinkSpeed = 0;
/* Private function prototypes -----------------------------------------------*/
RCC_ClocksTypeDef RCC_Clocks;
USART_InitTypeDef USART2_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Private functions ---------------------------------------------------------*/
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */

void USART2_IRQHandler (void)//Öжϴ¦Àíº¯Êý
{
        if (USART_GetFlagStatus(USART2,USART_FLAG_RXNE) ==SET)
                {
                 USART_SendData(USART2,USART_ReceiveData(USART2));
                 while (!USART_GetFlagStatus(USART2,USART_FLAG_TC));
                }
        USART_ClearITPendingBit(USART2,USART_IT_RXNE);//Öжϱê־λ
}

int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f030.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */
  
  /* SysTick end of count event each 1ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
  
  /* Initialize LED2 */
  STM_EVAL_LEDInit(LED2);
  
  /* Initialize User_Button on STM32NUCLEO */
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);   
  
  /* Initiate Blink Speed variable */
  BlinkSpeed = 0;
  USART_StructInit(&USART2_InitStruct);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, 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_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);
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
//nvic
  //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPriority= 0x03;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
  NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART2);
USART_Init(USART2, &USART2_InitStruct);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_ClearFlag (USART2,USART_FLAG_TC);
USART_Cmd(USART2, ENABLE);
  /* Infinite loop */
  while (1)
  {
    /* Test on blink speed */
    if(BlinkSpeed == 0)
    {
      /*LED2 Toggle each 50ms*/
      STM_EVAL_LEDToggle(LED2);
      Delay(50);      
    }      
    else if(BlinkSpeed == 1)
    {
      STM_EVAL_LEDToggle(LED2);
      /*LED2 Toggle each 200ms */
      Delay(200);
    }
  
  //USART_SendData(USART2, 0x5555);
  }
}
/**
* @brief  Inserts a delay time.
* @param  nTime: specifies the delay time length, in 1 ms.
* @retval None
*/
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--;
  }
}
#ifdef  USE_FULL_ASSERT
/**
* @brief  Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  file: pointer to the source file name
* @param  line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

使用特权

评论回复
| 2019-11-10 16:50 | 显示全部楼层
然后呢?你是怎么应对这个的呢

使用特权

评论回复
 楼主 | 2019-11-10 16:53 | 显示全部楼层
其中那个NVIC的设置不知道怎么搞,我杠掉的那个NVIC那里改怎么弄

使用特权

评论回复
| 2019-11-10 16:56 | 显示全部楼层
单独让板子循环发送0x5555呢

使用特权

评论回复
 楼主 | 2019-11-10 16:59 | 显示全部楼层
单独让板子循环发送0x5555是可以的,就是接不到

使用特权

评论回复
| 2019-11-10 17:03 | 显示全部楼层

USART_Init(USART2, &USART2_InitStruct);
这里面的USART2_InitStruct结构体未配置!

使用特权

评论回复
| 2019-11-10 17:06 | 显示全部楼层

然后呢?你是怎么应对这个的呢

使用特权

评论回复
| 2019-11-10 17:09 | 显示全部楼层
最简单的是用STM32 CUBE软件,自动生成。

使用特权

评论回复
| 2019-11-10 17:12 | 显示全部楼层

void connectUsart1ToExt(uint32_t baudrate, uint8_t parity, uint8_t length, uint8_t stopbits)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;  
        
#if defined  (STM32F051) || defined (STM32F042)
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        
        // GPIOµÄ¹«¹²ÌØÐÔ
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

        //COM1
        //USARTx_TX   GPIO_Mode_AF_PP
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_Tx);
        GPIO_Init(GetPortOf(ST_COM1_Tx) , &GPIO_InitStructure);
        SetAF_COM1_Tx;//±ØÐëÑ¡Ôñ¸´Óù¦ÄÜ

        //USARTx_RX   GPIO_Mode_AF_PP & IPU
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_Rx);
        GPIO_Init(GetPortOf(ST_COM1_Rx), &GPIO_InitStructure);
        SetAF_COM1_Rx;//±ØÐëÑ¡Ôñ¸´Óù¦ÄÜ
        
        //RS485-DE
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_DE);
        GPIO_Init(GetPortOf(ST_COM1_DE), &GPIO_InitStructure);
        GPIO_SetBits(RS485_DE);
        
#else
        #warning "Make sure needn't define STM32F051 etc. ?"
#endif

  USART_Cmd(USART1, DISABLE); //ÓÃ×÷ÖØÐÂÅäÖÃUSART1£¬Ïȹش®¿Ú

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  
  USART_InitStructure.USART_BaudRate = baudrate;
  
  switch (parity)
  {
  case 1://Ææ
    USART_InitStructure.USART_Parity = USART_Parity_Odd;
    if (length == 7)
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    else
      USART_InitStructure.USART_WordLength = USART_WordLength_9b;
    break;
  case 2://ż
    USART_InitStructure.USART_Parity = USART_Parity_Even;
    if (length == 7)
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    else
      USART_InitStructure.USART_WordLength = USART_WordLength_9b;
    break;
  default://0ÎÞ8λÊý¾Ýλ
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    break;
  }

  switch (stopbits)
  {
  case 2:
    USART_InitStructure.USART_StopBits = USART_StopBits_2;
    break;
  case 3:
    USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
    break;
#if defined  (STM32F051) || defined (STM32F042)
  //null
#elif defined (STM32F100) || defined (STM32F101) || defined (STM32F103)
  case 0://±£ÁôSTM32F05²»Ö§³Ö,STM32F10Ö§³Ö
        USART_InitStructure.USART_StopBits = USART_StopBits_0_5;
        break;
#else
        #warning "Make sure needn't define STM32F051 etc. ?"
#endif
  default://1
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    break;
  }
  
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

  USART_Cmd(USART1, ENABLE);
}

使用特权

评论回复
| 2019-11-10 17:15 | 显示全部楼层
NVIC是配置中断优先级的。如果要用中断接收,必须配置。

使用特权

评论回复
 楼主 | 2019-11-10 17:18 | 显示全部楼层
串口参数在usart.c里面有,它原先就写在那,应该没事吧,毕竟我的循环发送是可以的,就是这个接收不到

使用特权

评论回复
| 2019-11-10 17:49 | 显示全部楼层
看到了,你用了缺省值 USART_StructInit(&USART2_InitStruct);。  

使用特权

评论回复
| 2019-11-10 18:07 | 显示全部楼层
嗯,那就是中断未使能的问题了NVIC。

使用特权

评论回复
| 2019-11-10 18:09 | 显示全部楼层

刚好在弄STM32F070,跟030差不多,发个我设置的USART初始化函数;
static void xMBPortUsartInit(uint32_t ulBaudRate)
{
        USART_InitTypeDef USART_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        
        USART_DeInit(cCOM1);
        
        /* Enable GPIO clock */
        RCC_AHBPeriphClockCmd(cCOM1_TX_GPIO_CLK | cCOM1_RX_GPIO_CLK, ENABLE);

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

        /* Connect PXx to USARTx_Tx */
        GPIO_PinAFConfig(cCOM1_TX_GPIO_PORT, cCOM1_TX_SOURCE, cCOM1_TX_AF);
        /* Connect PXx to USARTx_Rx */
        GPIO_PinAFConfig(cCOM1_RX_GPIO_PORT, cCOM1_RX_SOURCE, cCOM1_RX_AF);
  
        /* Configure USART Tx as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = cCOM1_TX_PIN;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(cCOM1_TX_GPIO_PORT, &GPIO_InitStructure);
   
        /* Configure USART Rx as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = cCOM1_RX_PIN;
        GPIO_Init(cCOM1_RX_GPIO_PORT, &GPIO_InitStructure);
        
        USART_InitStructure.USART_BaudRate = ulBaudRate;
        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 configuration */
        USART_Init(cCOM1, &USART_InitStructure);
        
        //USART_ClearITPendingBit(cCOM1, USART_IT_RXNE);
        //USART_ClearITPendingBit(cCOM1, USART_IT_TC);
        //USART_ClearITPendingBit(cCOM1, USART_IT_TXE);
        
        /* Enable USART RX Interrupt */
        //USART_ITConfig_Boot(cCOM1, USART_IT_RXNE, ENABLE);
        /* Disable USART TX Interrupt */
        //USART_ITConfig_Boot(cCOM1, USART_IT_TC, DISABLE);
        
        /* Enable USART */
        USART_Cmd(cCOM1, ENABLE);
        
        /* Enable the USART Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

/* ----------------------- Start implementation -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    if( xRxEnable )
    {
                USART_ITConfig(cCOM1, USART_IT_RXNE, ENABLE);
    }
    else
    {
                USART_ITConfig(cCOM1, USART_IT_RXNE, DISABLE);
    }

    if( xTxEnable )
    {
                USART_ITConfig(cCOM1, USART_IT_TXE, ENABLE);
    }
    else
    {
                USART_ITConfig(cCOM1, USART_IT_TXE, DISABLE);
    }
}

使用特权

评论回复
| 2019-11-10 18:12 | 显示全部楼层

NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组任意选

  /* Enable the PPPInterrupts */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
注意:不同版本的库,结构体的成员有变化(依实际为准)

使用特权

评论回复
| 2019-11-10 18:16 | 显示全部楼层


都是CM0的,初始化函数是通用的。

使用特权

评论回复
| 2019-11-10 18:19 | 显示全部楼层
刚开始入手STM32的话,还是建议使用官网的STM32Cube工具自动生成。配置起来很方便,外设(比如USART)的三种通讯方式(轮询、中断、DMA)的驱动都是直接可以用,省去了自己摸索的时间。

使用特权

评论回复
| 2019-11-10 18:24 | 显示全部楼层

我的只有三个成员,那两个preemption和sub我可以不用吗,还是自己在加,但我好像加了错的

使用特权

评论回复
扫描二维码,随时随地手机跟帖
*滑动验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

我要发帖 投诉建议 创建版块 申请版主

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式

论坛热帖

在线客服 快速回复 返回顶部 返回列表