stm32f4-discovery USART1 (PA9\PA10不可用)

调试完GPIO,就想调USART了,因为在单片机里边,串口一直扮演着很重要的角色。如今到了STM32,依然从USART开始。
 
从手册中看到,USART1/6这两个是连接到总线APB2上的,所以配置前提前使能总线时钟。
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
接下来和stm32s2系列基本一样,配置usart1类型。
  1. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  2.   /* Configure USARTx_Tx as alternate function push-pull */
  3.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  4.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  5.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  6.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  7.   /* Configure USARTx_Rx as input floating */
  8.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  9.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  10.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  11.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
  12.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
该说说配置IO口了,从数据手册  stm32f407_datasheet.pdf   看到,USART1可以映射到PA9.10口,也可以映射到PB6.7口,这里选用的是PA9.10口上。配置IO口。
  1. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  2.   /* Configure USARTx_Tx as alternate function push-pull */
  3.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  4.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  5.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  6.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  7.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  8.   /* Configure USARTx_Rx as input floating */
  9.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  10.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  11.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  12.   GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
  13.   GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

接下来用示波器看引脚波形,发现是倒立的一个小尖,每次发送数据只有一个小尖,感觉奇怪,细查也不是IO口配置问题,无奈下将TX映射到PB6上,波形出来了,没问题。

难道是我的discovery板子PA9烧了??后来又想到查原理图。发现原因所在!

原来,discovery板子的PA9连接到了usb otg芯片的out端,并且!有一个4.7uF电容与之并联!!!!!

大家请看原理图34页

 discovery原理图.pdf  

果然,这样肯定不会输出波形,串行信号直接被电容旁路了!不知道st为何这样设计!害我白费了这么长时间调串口!

好了,最后将可用的程序贴出来,main.c,用官方库开发的。

 

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f4_discovery.h"
  3. /** @addtogroup STM32F4_Discovery_Peripheral_Examples
  4.   * @{
  5.   */

  6. /** @addtogroup IO_Toggle
  7.   * @{
  8.   */ 

  9. /* Private typedef -----------------------------------------------------------*/
  10. GPIO_InitTypeDef GPIO_InitStructure;

  11. /* Private define ------------------------------------------------------------*/
  12. /* Private macro -------------------------------------------------------------*/
  13. /* Private variables ---------------------------------------------------------*/
  14. /* Private function prototypes -----------------------------------------------*/
  15. void Delay(__IO uint32_t nCount);
  16. /* Private functions ---------------------------------------------------------*/

  17. /**
  18.   * @brief Main program
  19.   * @param None
  20.   * @retval None
  21.   */

  22. void USART1_Configuration(void)
  23. {
  24.   GPIO_InitTypeDef GPIO_InitStructure;
  25.   USART_InitTypeDef USART_InitStructure;
  26.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  27.   /* Configure USARTx_Tx as alternate function push-pull */
  28.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  29.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  30.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  31.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  32.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  33.   /* Configure USARTx_Rx as input floating */
  34.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  35.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  36.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  37.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
  38.   GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
  39.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  40.   USART_InitStructure.USART_BaudRate = 115200;
  41.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  42.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  43.   USART_InitStructure.USART_Parity = USART_Parity_No;
  44.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  45.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  46.   USART_Init(USART1, &USART_InitStructure);
  47.   USART_Cmd(USART1, ENABLE); 
  48. }

  49. int main(void)
  50. {
  51.   USART1_Configuration();

  52.   /* GPIOD Periph clock enable */
  53.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  54.   /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  55.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  56.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  57.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  58.   GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
  59.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  60.   GPIO_Init(GPIOD, &GPIO_InitStructure);


  61.   while (1)
  62.   {
  63.     USART_SendData(USART1,55);

  64.     /* PD13 to be toggled */
  65.     GPIO_SetBits(GPIOD, GPIO_Pin_13);
  66.     Delay(0x3FFFf); 
  67.     GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  68.     Delay(0xFFFFF);

  69.   }
  70. }

  71. /**
  72.   * @brief Delay Function.
  73.   * @param nCount:specifies the Delay time length.
  74.   * @retval None
  75.   */
  76. void Delay(__IO uint32_t nCount)
  77. {
  78.   while(nCount--)
  79.   {
  80.   }
  81. }

  82. #ifdef USE_FULL_ASSERT

  83. /**
  84.   * @brief Reports the name of the source file and the source line number
  85.   * where the assert_param error has occurred.
  86.   * @param file: pointer to the source file name
  87.   * @param line: assert_param error line source number
  88.   * @retval None
  89.   */
  90. void assert_failed(uint8_t* file, uint32_t line)
  91. { 
  92.   /* User can add his own implementation to report the file name and line number,
  93.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  94.   /* Infinite loop */
  95.   while (1)
  96.   {
  97.   }
  98. }
  99. #endif

  100. /**
  101.   * @}
  102.   */ 

  103. /**
  104.   * @}
  105.   */ 

  106. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值