[STM32F1] STM32串口发送问题

[复制链接]
1116|7
 楼主| heishe1989 发表于 2016-7-1 11:45 | 显示全部楼层 |阅读模式
我用printf发送字符串的时候,第一个字符总是收不到,请问哪位大侠知道这是怎么回事?
cowboy2014 发表于 2016-7-1 13:09 | 显示全部楼层
你的程序是怎么写的呢?
 楼主| heishe1989 发表于 2016-7-1 14:02 | 显示全部楼层
本帖最后由 heishe1989 于 2016-7-1 14:04 编辑
cowboy2014 发表于 2016-7-1 13:09
你的程序是怎么写的呢?

#include"stm32f10x.h"
#include"stdio.h"


void COM_config()
{
        USART_InitTypeDef USART_InitStructure;
        RCC_APB2PeriphClockCmd  ( RCC_APB2Periph_USART1,          ENABLE  );

        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_Mode =  USART_Mode_Rx|USART_Mode_Tx;
        USART_InitStructure.USART_HardwareFlowControl =         USART_HardwareFlowControl_None;
        USART_Init  ( USART1,  &USART_InitStructure  );
        USART_Cmd(USART1,ENABLE);
        //USART_ITConfig  ( USART1, USART_IT_RXNE,  ENABLE  );

}
void GPIO_config()
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd  ( RCC_APB2Periph_GPIOA,          ENABLE  );

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed =        GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;               
        GPIO_Init  ( GPIOA, &GPIO_InitStructure );         //配置PA9为Tx

        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 );        //配置PA10为Rx

}
int fputc(int ch, FILE *f)
{
        USART_SendData  (USART1,(unsigned char)ch );
        while(USART_GetFlagStatus  ( USART1,  USART_FLAG_TC  )!=SET);
        return ch;   

}


int main()
{
        GPIO_config();
        COM_config();
        printf(" CLS(1);W8BF(100,100,200,200,2);DS16(0,0,'级联光电传感科技有限公司',4);DS24(0,30,'级联光电传感科技有限公司',3);\r\n");
        while(1)
        {
                        
        }
}
printf里面第一个空格是我不得已加上的,如果不加的话就收不到字母C

cowboy2014 发表于 2016-7-1 20:53 | 显示全部楼层
我觉得这个得看你的是如何移植的了?
 楼主| heishe1989 发表于 2016-7-4 21:54 | 显示全部楼层
cowboy2014 发表于 2016-7-1 20:53
我觉得这个得看你的是如何移植的了?

什么移植?请大虾明示?
zchong 发表于 2016-7-5 07:41 来自手机 | 显示全部楼层
记得有个标志要清一下
xdhwwdz2011@163 发表于 2016-7-5 11:32 | 显示全部楼层
int fpuc(FILE *f, int c)
{
    while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
    USART_SendData(USART1, c);
    return c;
}


这样试试
 楼主| heishe1989 发表于 2016-7-5 16:51 | 显示全部楼层
xdhwwdz2011@163 发表于 2016-7-5 11:32
int fpuc(FILE *f, int c)
{
    while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );

这样可以,谢谢楼主!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

153

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部