[应用相关] stm32 USART的使用源程序

[复制链接]
651|0
 楼主| 500days 发表于 2016-5-20 21:06 | 显示全部楼层 |阅读模式
  1. #include "stm32f10x_lib.h"
  2. #include"stdio.h"

  3. void RCC_Configuration(void)
  4. {
  5. ErrorStatus HSEStartUpStatus;
  6. /* RCC system reset(for debug purpose) */
  7. RCC_DeInit();          //将外设RCC 寄存器重设为缺省值
  8. /* Enable HSE */
  9. RCC_HSEConfig(RCC_HSE_ON);//使能外部时钟
  10. /* Wait till HSE is ready */
  11. HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部时钟就绪
  12. if(HSEStartUpStatus == SUCCESS)
  13. {
  14.    /* Enable Prefetch Buffer 启用预取缓冲器 */
  15.    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  16.    /* Flash 2 wait state FLASH设置2个等待周期*/
  17.    FLASH_SetLatency(FLASH_Latency_2);

  18.    /* HCLK(AHB时钟) = SYSCLK 设置AHB时钟*/
  19.    RCC_HCLKConfig(RCC_SYSCLK_Div1);//AHB(Advanced High performance Bus)系统总线
  20.                                                     //APB(Advanced Peripheral Bus)外围总线

  21.    /* PCLK2 = HCLK PCLK2时钟=主时钟*/
  22.    RCC_PCLK2Config(RCC_HCLK_Div1);

  23.    /* PCLK1 = HCLK/2 PCLK1时钟为主时钟1/2*/
  24.    RCC_PCLK1Config(RCC_HCLK_Div2);

  25.    /* PLLCLK = 8MHz * 9 = 72 MHz 设置时钟为72M,设置PLL时钟源及倍频系数(<=72M)*/
  26.    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  27.    /* Enable PLL 使能PLL*/
  28.    RCC_PLLCmd(ENABLE);

  29.    /* Wait till PLL is ready */
  30.    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  

  31.    /* Select PLL as system clock source */
  32.    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  33.    /* Wait till PLL is used as system clock source */
  34.    while(RCC_GetSYSCLKSource() != 0x08);
  35. }
  36. }


  37. void USART_configuration(void)
  38. {
  39.    USART_InitTypeDef USART_InitStructure;

  40.    USART_InitStructure.USART_BaudRate = 9600;
  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_Tx | USART_Mode_Rx;


  46.    USART_Init(USART1, &USART_InitStructure);
  47.    USART_Cmd(USART1, ENABLE);


  48. }
  49. void USART_ClockInitconfiguration(void)
  50. {
  51.    USART_ClockInitTypeDef  USART_ClockInitStructuree;
  52.    USART_ClockInitStructuree.USART_Clock = USART_Clock_Disable;
  53.    USART_ClockInitStructuree.USART_CPOL = USART_CPOL_High;
  54.    USART_ClockInitStructuree.USART_CPHA = USART_CPHA_1Edge;
  55.    USART_ClockInitStructuree.USART_LastBit = USART_LastBit_Enable;
  56.    USART_ClockInit(USART1, &USART_ClockInitStructuree);

  57. }

  58. void GPIO_configuration(void)
  59. {
  60.      GPIO_InitTypeDef GPIO_InitStructure;
  61.      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1  , ENABLE);

  62.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  63.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  64.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  65.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  66.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  67.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  68.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  69.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  70. }


  71. int main(void)
  72. {
  73.    unsigned char i,data;

  74.    RCC_Configuration();
  75.    GPIO_configuration();
  76.    USART_ClockInitconfiguration();
  77.    USART_configuration();
  78.    data='A';
  79.    for(i=0;i<30;i++)
  80.    {
  81. USART_SendData(USART1, data);
  82. data++;
  83. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) ==RESET)        ;

  84. }

  85.   return(1);
  86.    }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

120

帖子

3

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