打印
[应用相关]

分享:stm32 USART的使用源程序

[复制链接]
861|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
huangfeng33|  楼主 | 2014-10-31 11:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x_lib.h"
#include"stdio.h"

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

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

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

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

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

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

   /* Enable PLL 使能PLL*/
   RCC_PLLCmd(ENABLE);

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

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

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


void USART_configuration(void)
{
   USART_InitTypeDef USART_InitStructure;

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


   USART_Init(USART1, &USART_InitStructure);
   USART_Cmd(USART1, ENABLE);


}
void USART_ClockInitconfiguration(void)
{
   USART_ClockInitTypeDef  USART_ClockInitStructuree;
   USART_ClockInitStructuree.USART_Clock = USART_Clock_Disable;
   USART_ClockInitStructuree.USART_CPOL = USART_CPOL_High;
   USART_ClockInitStructuree.USART_CPHA = USART_CPHA_1Edge;
   USART_ClockInitStructuree.USART_LastBit = USART_LastBit_Enable;
   USART_ClockInit(USART1, &USART_ClockInitStructuree);

}

void GPIO_configuration(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1  , ENABLE);

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

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}


int main(void)
{
   unsigned char i,data;

   RCC_Configuration();
   GPIO_configuration();
   USART_ClockInitconfiguration();
   USART_configuration();
   data='A';
   for(i=0;i<30;i++)
   {
USART_SendData(USART1, data);
data++;
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) ==RESET)        ;

}

  return(1);
   }


沙发
mmuuss586| | 2014-10-31 12:28 | 只看该作者

支持楼主分享;

使用特权

评论回复
板凳
zh113214| | 2014-11-1 12:59 | 只看该作者
表示赞一个!!!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

506

主题

2446

帖子

8

粉丝