打印

使用printf时,第一条printf的第一字节打出一个中文乱码,求解

[复制链接]
3230|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
can123dao|  楼主 | 2013-8-12 11:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
芯片STM32F103ZET6,应该是自己代码的问题。
main.c
#include "stm32f10x.h"
#include "netconfig.h"
#include "SysTick.h"
#include "sys_config.h"
#include "cc.h"
#include "lwip/dns.h"
#include "stdio.h"

__IO uint32_t LocalTime = 0;
struct ip_addr result;

static void found(const char *name, struct ip_addr *ipaddr, void *arg){
       
        uint8_t ip[4];
        result = *ipaddr;

        ip[0] = result.addr>>24;         
        ip[1] = result.addr>>16;
        ip[2] = result.addr>>8;   
        ip[3] = result.addr;
       
        printf("%d.%d.%d.%d",ip[3], ip[2], ip[1], ip[0]);       
}

int main(void)
{
        const char *hostname = "www.baidu.com";
        struct ip_addr *addr;
       
           sys_config();
           SysTick_Init();       
           LwIP_Init();

           printf("LWIP INIT DONE\n");

           dns_gethostbyname(hostname, addr, found, NULL);
         
    while(1)
    {        
                    LwIP_Periodic_Handle(LocalTime);       
    }
}
sysconfig.c

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



void NVIC_Config(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        __set_PRIMASK(1);
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        #ifdef VECT_TAB_RAM
                NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else  //VEC_TAB_FLASH
                NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif

        NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;                                 //外部中断2
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;       
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                          
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                               
        NVIC_Init(&NVIC_InitStructure);       
         
   SysTick_Config(72000);
   __set_PRIMASK(0);
}

void GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |  RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOF | RCC_APB2Periph_AFIO, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                                                  //USART1 TX
        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;                                          //USART1 RX
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                            //复用开漏输入
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;          //sck-->PA5  SI-->PA7  SO-->PA6
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                          //复用功能
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                                  //enc_ncs
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        GPIO_SetBits(GPIOA,GPIO_Pin_4);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                                  //PC4,FLASH片选
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
        GPIO_SetBits(GPIOC,GPIO_Pin_4);

   
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                                        //enc_nint
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                   
        GPIO_Init(GPIOC, &GPIO_InitStructure);
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource2);


        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;                                  //led
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
        GPIO_SetBits(GPIOF,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);

//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                       
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                   
//        GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART1_Config(void)
{
        USART_InitTypeDef USART_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        USART_InitStructure.USART_BaudRate = 115200;                                       
        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_Rx | USART_Mode_Tx;
        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1, ENABLE);
}

void SPI1_Config(void)
{
        SPI_InitTypeDef SPI_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;
        SPI_Init(SPI1, &SPI_InitStructure);

        SPI_Cmd(SPI1, ENABLE);
}

void EXTI_Config(void)
{
        EXTI_InitTypeDef EXTI_InitStructure;

        EXTI_InitStructure.EXTI_Line = EXTI_Line2;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;         //比上升沿触发EXTI_Trigger_Rising抖动少
        EXTI_InitStructure.EXTI_LineCmd        = ENABLE;
        EXTI_Init(&EXTI_InitStructure);
}

void sys_config()
{
        NVIC_Config();
        GPIO_Config();
        USART1_Config();
        EXTI_Config();
        SPI1_Config();
}

//加入以下代码,支持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(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET){
        }
        USART_SendData(USART1, (uint8_t)ch);
        return ch;
}
#endif

沙发
can123dao|  楼主 | 2013-8-12 16:05 | 只看该作者
解决,波特率设高了,低点就行,为什么呢。

使用特权

评论回复
板凳
chuangpu| | 2013-8-12 19:29 | 只看该作者
问题解决了就好   有可能是船速速度快   导致有丢包现象  我是这样理解的  

使用特权

评论回复
地板
can123dao|  楼主 | 2013-8-12 21:09 | 只看该作者
chuangpu 发表于 2013-8-12 19:29
问题解决了就好   有可能是船速速度快   导致有丢包现象  我是这样理解的

也许吧,可是只丢第一字节,有点怪。

使用特权

评论回复
5
xiaosi204| | 2014-3-26 16:10 | 只看该作者
LZ你好,你现在DNS可以用吗?为什么我调用dns_gethostbyname的时候,就卡在这里了
if ((dns_pcb == NULL) || (addr == NULL) ||
      (!hostname) || (!hostname[0]) ||
      (strlen(hostname) >= DNS_MAX_NAME_LENGTH)) {
    return ERR_ARG;
  }
直接返回ERR_ARG

能分享下你的代码吗?ye_zi204@163.com

使用特权

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

本版积分规则

36

主题

114

帖子

2

粉丝