打印

串口中断通讯求解

[复制链接]
1875|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
a38128895|  楼主 | 2011-10-31 14:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 a38128895 于 2011-10-31 15:12 编辑

我用STM32固件库3.5开发,设置串口波特率是9600,为什么PC上接收需要设定成19200才能正确接收到?
程序:
int main(void)
{      
  /* System Clocks Configuration */
  RCC_Configuration();
      
  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  USART_Configuraton();
  while (1)
  {
  }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{  
  /* Enable GPIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Configure USAR1 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Configure USAR1 Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /*配置LED*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;   //通道设置为串口1中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   //中断占先等级0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;          //中断响应优先级0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             //打开中断
  NVIC_Init(&NVIC_InitStructure);                             //初始化
}
//-----------------------
void USART_Configuraton(void)
{
    USART_InitTypeDef USART_InitStructure;
    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;
   
    /* Configure USARTy */
    USART_Init(USART1, &USART_InitStructure);
    /* Enable USARTy Receive and Transmit interrupts */
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
    /* Enable the USARTy */
    USART_Cmd(USART1, ENABLE);                        
}
沙发
a38128895|  楼主 | 2011-10-31 14:52 | 只看该作者
用的是STM32107VC芯片,晶体25M

使用特权

评论回复
板凳
请如实填写| | 2011-10-31 15:17 | 只看该作者
库函数里面的波特率好像是在外接8MHz的情况下计算的
外接晶振4~16MHz 我记得好像是
你可以查下手册
接其他晶振自己算下就可以了

使用特权

评论回复
地板
shuaibaobao| | 2011-11-27 22:04 | 只看该作者
添加过了,还是有问题啊,呵呵。

使用特权

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

本版积分规则

0

主题

28

帖子

1

粉丝