打印
[STM32]

USART口无法用printf函数输出,target里面勾选了USE MicroLib

[复制链接]
2781|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一鸟人_|  楼主 | 2014-8-14 09:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
具体程序如下:
main函数:
#include "stm32f10x.h"
#include "stm32f10x_sdio.h"
#include "GPIO_config.h"
#include "USART_Config.h"
#include <stdio.h>
#define CMD_STRING_SIZE       128

int fputc(int ch,FILE *f)
{
        //USART_ClearFlag(USART2,USART_FLAG_TC);
        USART_SendData(USART2,ch);
        while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
        return(ch);
}
//uint32_t SerialKeyPressed(uint8_t *key)
//{
//        if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)!=RESET)
//        {
//                *key = (uint8_t)USART2->DR;
//                return 1;
//        }
//        else
//        {
//                return 0;
//        }
//}
//
//uint8_t GetKey(void)
//{
//        uint8_t key = 0;
//        while(1)
//        {
//                if(SerialKeyPressed((uint8_t*)&key)) break;
//       
//        }
//        return key;
//}
//void GetInputString(uint8_t *buffp)
//{
//         uint32_t bytes_read = 0;
//        uint8_t c = 0;
//        do
//        {
//                c=GetKey();
//                if(c== '\r')
//                break;
//
//                if(bytes_read >= (CMD_STRING_SIZE))
//                {
//                        printf("Command string size overflow\r\n");
//                }
//                if(c >= 0x20 &&c<=0x7E)
//                {
//                       
//                        buffp[bytes_read++] = c;
//
//                        USART_ClearFlag(USART1,USART_FLAG_TC);
//                        USART_SendData(USART1,c);
//                        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
//                }
//        }
//         while(1);
//         if(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=RESET)
//         {
//                 USART_ClearFlag(USART1,USART_FLAG_TC);
//                 USART_SendData(USART1,"\n\r");
//                 buffp[bytes_read]='\0';
//                 //不确定是否要加结束符
//         }
//}

int main(void)
{
        uint8_t inputstr[CMD_STRING_SIZE];
        SystemInit();
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        USART_Config( );
        GPIO_Config( );
        printf("请输入:");
        GPIOA->BRR = GPIO_Pin_8;
        GPIOA->BRR = GPIO_Pin_11;
        GPIOA->BRR = GPIO_Pin_12;
        printf("请输入:");
       
        while(1) ;
//        {
//       
//                GetInputString(inputstr);
//        }
}
USART配置
#include "stm32f10x.h"
void USART_Config(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(USART2, &USART_InitStructure);
                USART_Cmd(USART2, ENABLE);
gpio配置
#include"stm32f10x.h"
void GPIO_Config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        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);

        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

           GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_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);

}

相关帖子

沙发
airwill| | 2014-8-14 10:11 | 只看该作者
先验证一下, usart 发送数据能正常吗?

使用特权

评论回复
板凳
一鸟人_|  楼主 | 2014-8-14 10:51 | 只看该作者
airwill 发表于 2014-8-14 10:11
先验证一下, usart 发送数据能正常吗?

我这已经是在验证usart是否能正常发送数据了,其他程序都注释起了呀~

使用特权

评论回复
地板
摩天轮1111| | 2015-7-26 23:53 | 只看该作者
这里我也遇到了,勾选了使用微库就可以了,就是不理解微库在这里起到什么作用,,,

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

7

帖子

0

粉丝