野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9970|回复: 5

请学习和开发的高手们围观、、、串口2接收GPS数据,串口1打印出来,为乱码

[复制链接]
发表于 2014-4-18 10:58:42 | 显示全部楼层 |阅读模式
这是我的源代码,,单独用USART1接收然后打印到电脑上结果的乱码也一样,,波特率确认没问题,串口确认没问题,也确认GPS能够发过来数据,跪求大神解决啊
#include "stm32f10x.h"
#include <stdio.h>
#include "stm32f10x_it.h"
#include "stm32f10x_flash.h"
ErrorStatus HSEStartUpStatus;
/**
  * @brief  主函数
  * @param  无
  * @retval 无
  */


void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);

int main(void)
{   
u16 i=0;
u16 m=0;
u8 flag=0;
char buff[500]="";
   
   
#ifdef DEBUG
  debug();
#endif
  RCC_Configuration();
  NVIC_Configuration();
  GPIO_Configuration();
  USART_Configuration();

while(1 )
{
}
}


void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();
  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);
  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);
    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);
    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);   //设置代码延时值(参数里的是指2延时周期)
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);   //使能或者失能预取指缓存(参数里是使能)
    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
   
  /* Enable USART1 and GPIOA clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  
  //查手册
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE);
}

/// 配置USART2接收中断
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;


#ifdef  VECT_TAB_RAM  
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif

/* Configure the NVIC Preemption Priority Bits */  
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;  
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* Configure USART1 Rx (PA.10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART2 TX (PA.02) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART2 Rx (PA.03) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}



void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  USART_InitStructure.USART_BaudRate = 4800;
  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_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

  USART_Init(USART2, &USART_InitStructure);
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);  //接收中断使能
  USART_Cmd(USART2, ENABLE);
  /* Enable USART1 */
  USART_Init(USART1, &USART_InitStructure);
   //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //接收中断使能
  USART_Cmd(USART1, ENABLE);
}



int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (u8) ch);
  /* Loop until the end of transmission */
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }
  return (ch);
}




这里是中断函数:
void USART2_IRQHandler(void)
{
uint8_t ch=0;

if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{  
  ch = USART_ReceiveData(USART2);

  printf("%c",ch);

}
  
}
回复

使用道具 举报

发表于 2014-4-18 11:05:01 | 显示全部楼层

int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (u8) ch);
  /* Loop until the end of transmission */
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }
  return (ch);
}

函数里的   USART_FLAG_TC  改成USART_FLAG_TXE来看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-18 11:25:17 | 显示全部楼层
晋奇 发表于 2014-4-18 11:05

int fputc(int ch, FILE *f)
{

多谢你的回答,但是还是和以上的一样,还是乱码、、、、、乱码里很多都是????但是把GPS模块直接连接到串口调试助手上面的时候,打印的数据就没错
回复 支持 反对

使用道具 举报

发表于 2014-4-18 13:36:02 | 显示全部楼层
不接GPS模块,单纯让COM1上报数据,看结果如何.
用串口软件发送数据至COM2,看COM1&COM2的返回如果;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-18 14:43:05 | 显示全部楼层
刀_口 发表于 2014-4-18 13:36
不接GPS模块,单纯让COM1上报数据,看结果如何.
用串口软件发送数据至COM2,看COM1&COM2的返回如果;

谢谢您非常专业的回答,很高兴能与您共同讨论这个话题!您的回答很对,开发板上面1口是通过跳帽连接CP2102的,1口能够给1口发数据并且能通过串口调试助手显示在电脑上,2口连接到CP2102也是能够显示数据的。2口通过一个USB转串口的工具就不能通过串口调试工具显示数据到电脑上面了,也就是说这一步已经是乱码了,所以就是这上面出现的问题,但是就是不知道怎么解决。。。。。是开发板的问题???串口2直接给串口1发也是乱码呀。、、、、
回复 支持 反对

使用道具 举报

发表于 2016-1-21 20:56:51 | 显示全部楼层
我是新手,我和楼主在做相似的事情,我是用串口1从电脑上发数据给板子,串口2接受数据并发给外设(温度传感器),并将数据返回给板子,板子再从串口1发回命令给电脑。现在关于单独的两个串口与电脑的通信我已经搞好了,就是不清楚怎么将这两个组合在一起,冒昧的请问楼主现在能给我指导一下吗?
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-5 15:57 , Processed in 0.028062 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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