找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4676|回复: 1
打印 上一主题 下一主题
收起左侧

stm32 USART的使用源程序

[复制链接]
跳转到指定楼层
楼主
ID:60266 发表于 2014-8-18 21:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

#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);
   }


评分

参与人数 1黑币 +1 收起 理由
yihong1 + 1

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:111454 发表于 2016-3-30 17:17 | 只看该作者
很不错哦,学习一下
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表