请教STM32F207ZG串口配置

2019-10-15 04:52发布

我在配置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; }
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。