Printf 函数
回帖(1)
2021-12-2 14:00:28
#include "stm32f10x.h"
#include "stdio.h"
void RCC_Configuration(void);//首先设置系统时钟为8MHZvoid GPIO_Configuration(void);void USART_Configuration(void);/*Function Name : fputcDescription : 重定向这个C库中的(stdio) printf函数,文件流到串口流USART1Input : ch,*fOutput : NoneReturn : None*/int fputc(int ch,FILE *f){ //ch送给USART1 USART_SendData(USART1, ch); //发送数据 //等待发送完毕 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET); //返回ch return(ch);}void delay1ms(u16 t){ u16 i,j; for(i=0;i0;j++); } int main(void){ u8 i,data='A'; RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); for(i=0;i<30;i++){ USART_SendData(USART1, data); //发送数据 data++; //Status = USART_GetFlagStatus(USART1, USART_FLAG_TC); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);//USART_SR的TC置一,表示发送数据完成;否则没有完成; } printf("hello"); return 0; }void RCC_Configuration(void)//系统时钟设置,应用库函数编程过程{ //----------使用外部RC晶振----------- RCC_DeInit(); //初始化为缺省值 RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速时钟 while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪 //FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer //FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state 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 = 8MHZ * 9 =72MHZ,系统给外设的时钟fpclkx RCC_PLLCmd(ENABLE); //Enable PLLCLK while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLLCLK is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock while(RCC_GetSYSCLKSource()!=0x08); //Wait till PLL is used as system clock source //---------打开相应外设时钟-------------------- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外设的GPIOA的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能APB2外设的GPIOC的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE); }void GPIO_Configuration(void)//复用引脚设置{ GPIO_InitTypeDef GPIO_InitStructure; /* Configure USARTx_Tx as alternate function push-pull */ 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); /* Configure USARTx_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);}void USART_Configuration(void)//复用引脚设置{ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600;//波特率 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_Tx | USART_Mode_Rx;//发送使能,接收使能 USART_Init(USART1, &USART_InitStructure);//初始化 USART_Cmd(USART1, ENABLE); //UE使能}
#include "stm32f10x.h"
#include "stdio.h"
void RCC_Configuration(void);//首先设置系统时钟为8MHZvoid GPIO_Configuration(void);void USART_Configuration(void);/*Function Name : fputcDescription : 重定向这个C库中的(stdio) printf函数,文件流到串口流USART1Input : ch,*fOutput : NoneReturn : None*/int fputc(int ch,FILE *f){ //ch送给USART1 USART_SendData(USART1, ch); //发送数据 //等待发送完毕 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET); //返回ch return(ch);}void delay1ms(u16 t){ u16 i,j; for(i=0;i0;j++); } int main(void){ u8 i,data='A'; RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); for(i=0;i<30;i++){ USART_SendData(USART1, data); //发送数据 data++; //Status = USART_GetFlagStatus(USART1, USART_FLAG_TC); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);//USART_SR的TC置一,表示发送数据完成;否则没有完成; } printf("hello"); return 0; }void RCC_Configuration(void)//系统时钟设置,应用库函数编程过程{ //----------使用外部RC晶振----------- RCC_DeInit(); //初始化为缺省值 RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速时钟 while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪 //FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer //FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state 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 = 8MHZ * 9 =72MHZ,系统给外设的时钟fpclkx RCC_PLLCmd(ENABLE); //Enable PLLCLK while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLLCLK is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock while(RCC_GetSYSCLKSource()!=0x08); //Wait till PLL is used as system clock source //---------打开相应外设时钟-------------------- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外设的GPIOA的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能APB2外设的GPIOC的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE); }void GPIO_Configuration(void)//复用引脚设置{ GPIO_InitTypeDef GPIO_InitStructure; /* Configure USARTx_Tx as alternate function push-pull */ 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); /* Configure USARTx_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);}void USART_Configuration(void)//复用引脚设置{ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600;//波特率 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_Tx | USART_Mode_Rx;//发送使能,接收使能 USART_Init(USART1, &USART_InitStructure);//初始化 USART_Cmd(USART1, ENABLE); //UE使能}
举报
更多回帖