OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3578|回复: 4

一个关于把printf定向到串口的程序,运行以后就停止不动

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
9
金钱
6
注册时间
2016-7-20
在线时间
0 小时
发表于 2016-7-20 14:35:57 | 显示全部楼层 |阅读模式
3金钱
一个关于把printf定向到串口的程序,运行以后就停止不动,把int fputc(u8 ch,FILE *f)去掉以后又可以正常运行,这是设呢么情况,求大神指导,下面是代码:
/*Include---------------------------*/
#include"stm32f10x_lib.h"  //°üº¬ËùÓеÄÍ·Îļþ
#include<stdio.h>
//----------------函数申明--------------------
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
int fputc(u8 ch,FILE *f)
{
//ch发送给USART1
USART_SendData(USART1,ch);
//等待发送完毕
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
//返回ch
return(ch);
}
int main (void)
{
u8 i, data;

USART_Configuration();
RCC_Configuration();
GPIO_Configuration();

data='A';
for(i=0;i<30;i++)
{
  USART_SendData(USART1,data);
  data++;
  while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
printf("abcbd");
}
/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
//----------&Ecirc;&sup1;&Oacute;&Atilde;&Iacute;&acirc;&sup2;&iquest;RC&frac34;§&Otilde;&ntilde;-----------
RCC_DeInit();   //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Icirc;&ordf;&Egrave;±&Ecirc;&iexcl;&Ouml;&micro;
RCC_HSEConfig(RCC_HSE_ON); //&Ecirc;&sup1;&Auml;&Uuml;&Iacute;&acirc;&sup2;&iquest;&micro;&Auml;&cedil;&szlig;&Euml;&Ugrave;&Ecirc;±&Ouml;&Oacute;
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //&micro;&Egrave;&acute;&yacute;&Iacute;&acirc;&sup2;&iquest;&cedil;&szlig;&Euml;&Ugrave;&Ecirc;±&Ouml;&Oacute;&Ecirc;&sup1;&Auml;&Uuml;&frac34;&Iacute;&ETH;÷

//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
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

//---------&acute;ò&iquest;&ordf;&Iuml;à&Oacute;&brvbar;&Iacute;&acirc;&Eacute;è&Ecirc;±&Ouml;&Oacute;--------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //&Ecirc;&sup1;&Auml;&Uuml;APB2&Iacute;&acirc;&Eacute;è&micro;&Auml;GPIOA&micro;&Auml;&Ecirc;±&Ouml;&Oacute;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //&Ecirc;&sup1;&Auml;&Uuml;APB2&Iacute;&acirc;&Eacute;è&micro;&Auml;GPIOC&micro;&Auml;&Ecirc;±&Ouml;&Oacute;  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : &sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;GPIO&Iacute;&acirc;&Eacute;è
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;  //&Eacute;ù&Atilde;÷&Ograve;&raquo;&cedil;&ouml;&frac12;á&sup1;&sup1;&Igrave;&aring;±&auml;&Aacute;&iquest;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //&sup1;&Uuml;&frac12;&Aring;&AElig;&micro;&Acirc;&Ecirc;&Icirc;&ordf;50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);     //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;GPIOA&frac14;&Auml;&acute;&aelig;&AElig;÷
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);     //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;GPIOA&frac14;&Auml;&acute;&aelig;&AElig;÷
}
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);
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

9

主题

103

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2016-7-4
在线时间
47 小时
发表于 2016-7-20 15:00:27 | 显示全部楼层
你确定串口操作能直接写文件指针
回复

使用道具 举报

22

主题

213

帖子

0

精华

高级会员

Rank: 4

积分
682
金钱
682
注册时间
2015-2-13
在线时间
74 小时
发表于 2016-7-20 17:40:14 | 显示全部楼层
改成原子哥的示例程序试下:
[mw_shl_code=c,true]//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
    int handle;
};

FILE __stdout;

//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
    x = x;
}

//重定义fputc函数
int fputc(int ch, FILE *f)
{
    while((USART1->SR & 0X40) == 0); //循环发送,直到发送完毕
    USART1->DR = (u8) ch;
    return ch;
}
#endif[/mw_shl_code]
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165536
金钱
165536
注册时间
2010-12-1
在线时间
2117 小时
发表于 2016-7-20 20:33:50 | 显示全部楼层
用我们最新的代码吧。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165536
金钱
165536
注册时间
2010-12-1
在线时间
2117 小时
发表于 2016-7-20 20:34:00 | 显示全部楼层
你这个好老了。。。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-9 22:21

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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