/************************************************
程序实现3 printf实现
**************************************************/
#include "sys.h"
#include "delay.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(u32 Baudrate);
int fputc(int ch, FILE *f)//重定向,让printf输出到串口
{
USART_SendData(USART1,ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
int main(void)
{
u8 i,data;
RCC_Configuration();
GPIO_Configuration();
USART_Configuration(9600);
data = 'A';
for(i=0;i<30;i++)
{
USART_SendData(USART1, data);
data++;
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
printf("yuanbao");
}
void RCC_Configuration(void)
{
/*---------------使用外部RC晶振----------*/
RCC_DeInit(); //设置时钟为缺省值
RCC_HSEConfig(RCC_HSE_ON); //使能外部高速晶振
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);//等待HSE准备就绪
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能指令预取
FLASH_SetLatency(FLASH_Latency_2); //等待2个周期
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 72MHZ
RCC_PLLCmd(ENABLE); //Enable PLLCLK
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //SYSCLK = PLLCLK
while(RCC_GetSYSCLKSource()!= 0x08); //Wait PLLCLK as system clock
//---------打开相应外设时钟--------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外设的GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA寄存器
}
void USART_Configuration(u32 Baudrate)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = Baudrate;
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);
}
|