stm32f207zg uart1配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21496027/article/details/52755287
**
  ******************************************************************************
  * @file    Project/STM32F2xx_StdPeriph_Template/main.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    13-April-2012
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include<stdio.h>
#include "stm32f2xx.h"
#include "stm322xg_eval.h"
/** @addtogroup Template_Project
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MESSAGE1   "     STM32F2xx      "
#define MESSAGE2   " Device running on  "
#define MESSAGE3   "   STM322xG-EVAL    "
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
 #ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
 USART_GetFlagStatus(USART1, USART_FLAG_TC);
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}
void RCC_Configuration(void)
{  
  RCC_DeInit();              //RCC寄存器初始化
  RCC_HSEConfig(RCC_HSE_ON);  //使用外部时钟
 if(RCC_WaitForHSEStartUp() == SUCCESS) //等待外部时钟启动
  {
    RCC_PLLCmd(DISABLE);                    //配置PLL前应先关闭主PLL
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择PLL时钟为系统时钟     RCC_HCLKConfig(RCC_SYSCLK_Div1);     //HCLK(AHB)时钟为系统时钟1分频
    RCC_PCLK1Config(RCC_HCLK_Div4);     //PCLK1(APB1)时钟为HCLK时钟8分频,则TIM2时钟为HCLK时钟4分频
    RCC_PCLK2Config(RCC_HCLK_Div2);     //PCLK2(APB2)时钟为HCLK时钟2分频
    RCC_PLLConfig(RCC_PLLSource_HSE, 25, 336, 2, 7); //PLL时钟配置,公式详见‘system_stm43f4xx.c’ Line149
     RCC_PLLCmd(ENABLE);        //PLL时钟开启
     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //等待PLL时钟准备好
   }
}
 void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE  );
   /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
//  RCC_APB2PeriphClockCmd( USART1, ENABLE  );
   /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;     // 选中管脚9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);     // 选择A端口
   
  /* Configure USART1 Rx (PA.10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 //IO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;/
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART1_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  USART_DeInit(USART1);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE  );
  USART_InitStructure.USART_BaudRate = 115200;        // 波特率为:115200
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;     // 8位数据
  USART_InitStructure.USART_StopBits = USART_StopBits_1;      // 在帧结尾传输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;    // 发送使能+接收使能
  /* Configure USART1 basic and asynchronous paramters */
  USART_Init(USART1, &USART_InitStructure);
 //USART1->BRR=0x0139; //选择A端口
  USART_Cmd(USART1, ENABLE);       //USART1总开关:开启
}


int main(void)
{
 RCC_Configuration();
  SystemCoreClockUpdate();
 GPIO_Configuration();
USART1_Configuration();
 while(1)
 {
printf("风萧萧兮易水寒,壮士一去兮不复还\r\n");
printf("屈原:离骚\r\n");
 }
 }

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in 10 ms.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
  while(TimingDelay != 0);
}
/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}
#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 STMicroelectronics *****END OF FILE****/

在此芯片上串口1和gpio不在同一个总线上,配置时注意,此程序在意法公司stm32f2xx库之上进行配置,函数简单明确;

猜你喜欢

转载自blog.csdn.net/qq_21496027/article/details/52755287
今日推荐