打印
[STM32F0]

接收PC端发送的数据

[复制链接]
2209|47
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kangzj|  楼主 | 2021-11-13 18:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用的NUCLEO-F030R8的板子,我现在能做的都改了,板子就是接收不到数据

使用特权

评论回复
沙发
zhaoxqi| | 2021-11-13 18:51 | 只看该作者
楼主程序可以公开吗?贴程序看下吧,这么说看不出什么原因

使用特权

评论回复
板凳
kangzj|  楼主 | 2021-11-13 18:53 | 只看该作者

#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

使用特权

评论回复
地板
jlyuan| | 2021-11-13 18:55 | 只看该作者
然后呢?你是怎么应对这个的呢

使用特权

评论回复
5
kangzj|  楼主 | 2021-11-13 18:57 | 只看该作者
其中那个NVIC的设置不知道怎么搞,我杠掉的那个NVIC那里改怎么弄

使用特权

评论回复
6
heweibig| | 2021-11-13 18:59 | 只看该作者
单独让板子循环发送0x5555呢

使用特权

评论回复
7
kangzj|  楼主 | 2021-11-13 19:01 | 只看该作者
单独让板子循环发送0x5555是可以的,就是接不到

使用特权

评论回复
8
wuhany| | 2021-11-13 19:03 | 只看该作者

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

使用特权

评论回复
9
tian111| | 2021-11-13 19:05 | 只看该作者
然后呢?你是怎么应对这个的呢

使用特权

评论回复
10
lizye| | 2021-11-13 19:10 | 只看该作者
最简单的是用STM32 CUBE软件,自动生成。

使用特权

评论回复
11
gongche| | 2021-11-13 19:11 | 只看该作者

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);
}

使用特权

评论回复
12
jlyuan| | 2021-11-13 19:13 | 只看该作者
NVIC是配置中断优先级的。如果要用中断接收,必须配置。

使用特权

评论回复
13
kangzj|  楼主 | 2021-11-13 19:16 | 只看该作者
串口参数在usart.c里面有,它原先就写在那,应该没事吧,毕竟我的循环发送是可以的,就是这个接收不到

使用特权

评论回复
14
supernan| | 2021-11-13 19:18 | 只看该作者
看到了,你用了缺省值 USART_StructInit(&USART2_InitStruct);。  

使用特权

评论回复
15
shimx| | 2021-11-13 19:20 | 只看该作者
嗯,那就是中断未使能的问题了NVIC。

使用特权

评论回复
16
午夜粪车| | 2021-11-13 19:23 | 只看该作者

刚好在弄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);
    }
}

使用特权

评论回复
17
zhaoxqi| | 2021-11-13 19:26 | 只看该作者

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);
注意:不同版本的库,结构体的成员有变化(依实际为准)

使用特权

评论回复
18
stly| | 2021-11-13 19:28 | 只看该作者
都是CM0的,初始化函数是通用的。

使用特权

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

使用特权

评论回复
20
shimx| | 2021-11-13 19:37 | 只看该作者
我的只有三个成员,那两个preemption和sub我可以不用吗,还是自己在加,但我好像加了错的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

672

主题

7007

帖子

2

粉丝