您的位置:首页 > 其它

STM32 usart 问题笔记

2017-03-28 16:02 204 查看
1、USART 手册上给的基础配置源码有误,应该为:

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_Tx | USART_Mode_Rx; 
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;  
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;  
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;  
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  

  USART_Init(USART1, &USART_InitStructure); 
USART_ClockInit(USART1 , &USART_ClockInitStructure); 
2、error: #268: declaration may not appear after executablestatement 解决方法:

变量声明必须放在主函数的最前面位置,不可以放在执行语句之后。



USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;//必须放在主函数的最前面。

3、千万不要忘记配置RCC和GPIO的函数库,否则程序会一直卡在while检测的那句话。

void RCC_Configuration(void)
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: