include<stm32f10x.h>
#include<stdio.h>
void RCC_Configuration(void); //被调用函数声明
void GPIO_Configuration(void);
void USART_InitConfiguration(void); //串口初始化
int fputc(int ch,FILE* f); //输出重定向函数
//.........输出重定向函数.........
int fputc(int ch,FILE *f)
(
/*ch送给USART1 */
USART_SendData(USART1,(uint8_t) ch); //发送一个数据
/*等待发送结束*/
while( USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
//返回ch
return (ch);
)
int main()
{
uint8_t i,data;
RCC_Configuration();
GPIO_Configuration();
USART_InitConfiguration(); //进行USART1串口初始化,
//fputc(int ch,FILE*f);
//下面进行发送数据
data='A';
for(i=0;i<8;i++)
{
USART_SendData(USART1, data); //发送一个数据
//FlagStatus Status;
data++;
while( USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); //等待一个数据发送完成
}
printf("handsome man");
}
//...........................................................................
void RCC_Configuration(void)
{
;
}
void GPIO_Configuration(void)
{
;
}
//串口初始化函数
void USART_InitConfiguration(void)
{
USART_InitTypeDef USART_InitStructure; // 注意库里已经定义了这个结构体,声明一个结构体变量 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_InitStructure.USART_Clock = USART_Clock_Disable; //同步模式下需要配置下面四项,异步模式不需要
// USART_InitStructure.USART_CPOL = USART_CPOL_High;
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure); //调用库中的串口初始化函数,初始化这个结构体
//进行 USART_CR1中 UE的设定,UE位控制整个USART的使能与否,调用此库函数作用就是使能串口1
USART_Cmd(USART1, ENABLE);
}
为什么加上 int fputc(int ch,FILE *f)函数,编译就出错了呢
compiling main.c...
User\main.c(53): error: #757: function "USART_SendData" is not a type name
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(53): error: #79: expected a type specifier
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(53): error: #90: function returning function is not allowed
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(53): error: #18: expected a ")"
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(53): error: #79: expected a type specifier
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(53): error: #90: function returning function is not allowed
User\main.c: USART_SendData(USART1,(uint8_t) ch); //发送一个数据
User\main.c: ^
User\main.c(55): error: #169: expected a declaration
User\main.c: while( USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
User\main.c: ^
User\main.c(57): error: #169: expected a declaration
User\main.c: return (ch);
User\main.c: ^
User\main.c(58): error: #169: expected a declaration
User\main.c: )
User\main.c: ^
User\main.c(112): warning: #1-D: last line of file ends without a newline
User\main.c: }