OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3559|回复: 2

请教STM32F207ZG串口配置

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
25
金钱
25
注册时间
2013-11-19
在线时间
0 小时
发表于 2013-11-19 18:40:50 | 显示全部楼层 |阅读模式
我在配置207ZG的串口时发现,我配置的波特率115200,但是输出的时候我用串口助手接收的时候波特率变成了38400,设置57600时接收为19200,其他均是乱码,有点头大请指教。下面是我的代码请指教:
void UART_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure; 
  
  /* Enable GPIOs clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
  
  //Enable USART Clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);


 /* GPIOD configuration */
 
   //Uart1 GPIO Config 
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
   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(GPIOB, &GPIO_InitStructure);

   //Uart2 GPIO Config 
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
  
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
   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);
}

void UART_Init(void)
{
    USART_InitTypeDef USART_InitStructure;
//EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
UART_GPIO_Config();
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 | USART_Mode_Tx;
    //Initialize the USART1
USART_Init(USART1,&USART_InitStructure);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
//NVIC_Enable(USART1_IRQn);
/* Enable the USART1 Parity Error Interrupt */
USART_ITConfig(USART1,USART_IT_PE,ENABLE);
    /* Enable the USART1 Framing Error Interrupt */
USART_ITConfig(USART1,USART_IT_ERR,ENABLE);
/* Enable the USART1 Receive Interrupt */
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
    /* Enable the USART1 Transmit Interrupt */
//USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_Cmd (USART1,ENABLE);
//Initialize the USART2
USART_Init(USART2,&USART_InitStructure);
    //Confing USART2_IRQn
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStructure);
    /* Enable the USART2 Parity Error Interrupt */
USART_ITConfig(USART2,USART_IT_PE,ENABLE);
    /* Enable the USART2 Framing Error Interrupt */
USART_ITConfig(USART2,USART_IT_ERR,ENABLE);
/* Enable the USART2 Receive Interrupt */
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
 
USART_Cmd (USART2,ENABLE);
    //interrupt Config 
    //USART_Cmd(USART_TypeDef * USARTx,FunctionalState NewState);
}

/*****************************************************************************
** Function name: SendBytes
**
** Descriptions: 通过串口ch,发送一组数据
**
** parameters: ch:     串口通道号
                        buf:    需要发送的数据Buf
                        length: 需要发送的数据长度
                        timeout:发送超时的时间
** Returned value: 返回已经发送的数据个数
** 
*****************************************************************************/
uint16_t SendBytes(uint8_t ch, uint8_t *buf, uint16_t length, uint16_t timeout)
{
uint32_t  len=0;
uint32_t  i;
volatile uint16_t  timeoutCnt;
     
timeoutCnt = 0;
    for(i=0; i<length; i++)
    {
       timeoutCnt =0;
       //while(!USART_GetFlagStatus(COM_USART[ch],USART_FLAG_TXE))  //串口发送缓冲区空?
       while(!(COM_USART[ch]->SR&USART_FLAG_TXE))
       {
  //等待发送缓冲区空,如果在超时时间内还非空
          if(timeoutCnt>=timeout)  
     {
     timeoutCnt = 0; 
     return i;    //退出发送,返回发送数据的个数
 }
 timeoutCnt++;
       }
       USART_SendData(COM_USART[ch],buf);    //发送缓冲区空,发送下一个字节数据
     }
len = i;
     
return len;
}
能牛则牛,不能不罢休!
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

109

主题

1606

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2222
金钱
2222
注册时间
2011-12-15
在线时间
37 小时
发表于 2013-11-19 21:25:35 | 显示全部楼层
代码一大堆,看这头疼
专业制作STM32 物联网通信模块板,模块交流群:369840039。
回复 支持 反对

使用道具 举报

1

主题

31

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
447
金钱
447
注册时间
2014-4-16
在线时间
82 小时
发表于 2017-2-5 17:21:50 | 显示全部楼层
为什么会这样,是不是时钟没配置正确,今天我也遇到这个问题,一直没有怀疑波特率115200的问题,刚刚改成38400,果真可以,为啥呢,好奇?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-8-24 19:35

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表