[STM32F0] STM32F042 USART 初始化无法写入

[复制链接]
1418|3
 楼主| e58136782000 发表于 2017-6-7 16:03 | 显示全部楼层 |阅读模式
各位大大好 小弟我最近用STM32F042K6T7
其上面的USART1和USART2各有两组
USART1:
         1            2
TX:  PA9        PB6
RX:  PA10       PB7

USART2:
        1             2
TX:  PA2       PA14
RX:  PA3       PA15

我使用USART1的第2組
但是程序执行到       
USART_Init(USART1, &USART_InitStructure);
这一行时我的register CR1 內的RE和TE
都没有启动,而且进入library程序内看发现有执行这一行
USARTx->CR1 = tmpreg;
tmpreg內是有值的,但是怎么执行USARTx->CR1一直等于0,一直写不进去
这问题已经2.3天一直无法解决,所以来这里求助

盼有没有高手大大指点一下迷津,程序如下

  1.         GPIO_InitTypeDef        GPIO_InitStructure;       
  2.         USART_InitTypeDef         USART_InitStructure;
  3.   
  4.         /* Enable USART clock */
  5.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
  6.         RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  7.         /* Connect PXx to USARTx_Tx and USARTx_Rx */
  8.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
  9.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);

  10.         //--**--// AF - Tx
  11.         /* Configure USART Tx as alternate function push-pull */
  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  13.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  14.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  15.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  16.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  17.         GPIO_Init(GPIOB, &GPIO_InitStructure);       

  18.         //--**--// AF - Rx
  19.         /* Configure USART Rx as alternate function push-pull */
  20.         //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  21.         //GPIO_Init(GPIOA, &GPIO_InitStructure);
  22.         USART_InitStructure.USART_BaudRate = 9600;//115200;
  23.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  24.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  25.         USART_InitStructure.USART_Parity = USART_Parity_No;//USART_Parity_Odd;
  26.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  27.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;       
  28.        
  29.         //USART_Configuration(&USART_InitStructure);
  30.        
  31.         /* USART configuration */
  32.         USART_Init(USART1, &USART_InitStructure);
  33.        
  34.         /* Enable USART */
  35.         USART_Cmd(USART1, ENABLE);
lxyppc 发表于 2017-6-7 17:08 来自手机 | 显示全部楼层
你的代码这句话
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
APB1开了APB2的时钟?
 楼主| e58136782000 发表于 2017-6-7 17:40 | 显示全部楼层
@lxyppc
感谢大大的指点,原来问题在这,我还有再问一个小问题,因为我执行了这行程序
USART_Cmd(USART1, ENABLE);
启动了CR1的register UE 但也同时启动了ISR内的FE
我看原文的手册看的不是很懂,他是说有Bus上有错误资料吗 还是我设定哪里有错误??
 楼主| e58136782000 发表于 2017-6-7 18:16 | 显示全部楼层
怎么结帖阿??
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部