调试完GPIO,就想调USART了,因为在单片机里边,串口一直扮演着很重要的角色。如今到了STM32,依然从USART开始。
从手册中看到,USART1/6这两个是连接到总线APB2上的,所以配置前提前使能总线时钟。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
接下来和stm32s2系列基本一样,配置usart1类型。
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-
- /* Configure USARTx_Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* Configure USARTx_Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
该说说配置IO口了,从数据手册
stm32f407_datasheet.pdf 看到,USART1可以映射到PA9.10口,也可以映射到PB6.7口,这里选用的是PA9.10口上。配置IO口。
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /* Configure USARTx_Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USARTx_Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
接下来用示波器看引脚波形,发现是倒立的一个小尖,每次发送数据只有一个小尖,感觉奇怪,细查也不是IO口配置问题,无奈下将TX映射到PB6上,波形出来了,没问题。
难道是我的discovery板子PA9烧了??后来又想到查原理图。发现原因所在!
原来,discovery板子的PA9连接到了usb otg芯片的out端,并且!有一个4.7uF电容与之并联!!!!!
大家请看原理图34页
果然,这样肯定不会输出波形,串行信号直接被电容旁路了!不知道st为何这样设计!害我白费了这么长时间调串口!
好了,最后将可用的程序贴出来,main.c,用官方库开发的。
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f4_discovery.h"
- /** @addtogroup STM32F4_Discovery_Peripheral_Examples
- * @{
- */
-
- /** @addtogroup IO_Toggle
- * @{
- */
-
- /* Private typedef -----------------------------------------------------------*/
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void Delay(__IO uint32_t nCount);
- /* Private functions ---------------------------------------------------------*/
-
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
-
- void USART1_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-
- /* Configure USARTx_Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* Configure USARTx_Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- 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;
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- }
-
- int main(void)
- {
- USART1_Configuration();
-
- /* GPIOD Periph clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
-
- while (1)
- {
- USART_SendData(USART1,55);
-
- /* PD13 to be toggled */
- GPIO_SetBits(GPIOD, GPIO_Pin_13);
- Delay(0x3FFFf);
- GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
- Delay(0xFFFFF);
-
- }
- }
-
- /**
- * @brief Delay Function.
- * @param nCount:specifies the Delay time length.
- * @retval None
- */
- void Delay(__IO uint32_t nCount)
- {
- while(nCount--)
- {
- }
- }
-
- #ifdef USE_FULL_ASSERT
-
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
-
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
-
- /**
- * @}
- */
-
- /**
- * @}
- */
-
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/