[求助] STM32F107串口1和串口2通信

merlong   2012-7-4 13:09 楼主
STM32F107串口1和串口2通信 串口2发送数据,串口1接收数据,为什么串口1接收到的数据只有第一个字节???
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Constants used by Serial Command Line Mode */
#define CMD_STRING_SIZE       128
USART_InitTypeDef USART_InitStructure;
void GetInputString (uint8_t * buffP);
uint8_t GetKey(void);
uint32_t SerialKeyPressed(uint8_t *key);
void SerialPutChar(uint8_t c); 
void USART_Configuration(void);
void Put_String(u8 *p);
int main(void)
{
  uint8_t inputstr[CMD_STRING_SIZE]; 
 
u8 str[]={"1234abcd1234"};
USART_Configuration();

 // Put_String(inputstr); 
  while (1)
  {  Put_String(str); 
     GetInputString(inputstr);
  }
}

void USART_Configuration(void)
{   USART_InitStructure.USART_BaudRate = 115200;  //串口2
  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_Tx;

  /* 完成串口时钟配置、GPIO配置、根据上述参数初始化并使能串口 */
  STM_EVAL_COMInit(COM1, &USART_InitStructure);  //串口1
  USART_InitStructure.USART_BaudRate = 115200;
  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;
  STM_EVAL_COMInit(COM2, &USART_InitStructure);
}
void GetInputString (uint8_t * buffP)
{
  u32 bytes_read = 0;
  u8 c = 0;
  
  do
  {
    c = GetKey();
    if (bytes_read >= (CMD_STRING_SIZE))
    {
      bytes_read = 0;
     // continue;
break;
    }
    if (c >= 0x20 && c <= 0x7E)
    {
      buffP[bytes_read++] = c;   
    }
  }
  while (c!='\0');
  buffP[bytes_read] = '\0';

/**************************************************/ 
void Put_String(u8 *p) //USART2发送字符串
while(*p) 
    USART_SendData(USART2, *p++); 
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) 
    {
}

u32 SerialKeyPressed(u8 *key)  //USART1接收数据
{


  if ( USART_GetFlagStatus(EVAL_COM2, USART_FLAG_RXNE) != RESET)
  {
    *key = (u8)EVAL_COM2->DR & 0xFF;
    return 1;
  }
  else
  {
    return 0;
  }
}

/**
  * @brief  Wait untill Get a key from the HyperTerminal
  * @param  None
  * @retval The Key Pressed
  */
u8 GetKey(void)   //接收数据
{
  u8 key = 0;

  /* Waiting for user input */
  while (1)
  {
    if (SerialKeyPressed((u8*)&key)) break;
  }
  return key;
}
#ifdef  USE_FULL_ASSERT
void assert_failed(u8* file, u32 line)
  while (1)
  {
  }
}
#endif

回复评论 (1)

我的串口都发不了。。。
点赞  2012-7-15 15:02
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复