打印

请问串口接收发送问题程序出现哪里错误了!

[复制链接]
1848|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
无敌小青年|  楼主 | 2013-5-8 10:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
程序目的是串口1接收GPS发送的数据(4800-8-1-1),由串口2发送给电脑串口助手显示
main文件:
#include "stm32f10x.h"
#include "usart1.h"
#include <stdio.h>
int main(void)
{
   USART2_Config();
   USART1_Config();
   printf("\r\n李xydlut\r\n");
   while(1)
   {
   
   }
}

USART文件:
#include "USART1.H"
#include <stdio.h>
/*USART2 GPIO配置,工作模式配置 115200 8-N-1*/
void USART2_Config(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
   /*USART2 Tx复用推挽输出*/
   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);

  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(USART2,&USART_InitStructure);
   USART_Cmd(USART2,ENABLE);
}
void USART1_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
     USART_InitTypeDef USART_InitStructure;
   
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);

     /*A10 RX浮空输入 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*配置USART1的工作模式*/
USART_InitStructure.USART_BaudRate = 4800;                 //波特率4800         
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8bit         
USART_InitStructure.USART_StopBits = USART_StopBits_1;    //停止位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);   //初始化外设 USART1   
     USART_Cmd(USART1, ENABLE);     //使能USART外设
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能接收中断
}
/*重定向c库函数printf到USART1(由printf调用)*/
int fputc(int ch,FILE *f)
{
   /*将printf内容发往串口*/
   USART_SendData(USART2,(unsigned char) ch);
   while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
   return(ch);
}
void USART1_IRQHandler(void)
{
   u16 str1;
      
   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
   {
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
      str1=USART_ReceiveData(USART1);
      USART_SendData(USART2, str1);
      while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
     USART_ITConfig( USART1,USART_IT_RXNE, ENABLE);
   }
}

型号stm32f103zet6  劳烦大家帮忙看看问题出在哪里。之前我串口1接收和发送都行,换成串口1接收,串口2发送就不行了,程序有点繁琐,感谢大家了!
沙发
IJK| | 2013-5-8 12:33 | 只看该作者
有点点小建议:LZ自己调一下程序,就容易知道问题在哪里了。
有个简单的办法,就是自发自收

使用特权

评论回复
板凳
无敌小青年|  楼主 | 2013-5-8 12:38 | 只看该作者
IJK 发表于 2013-5-8 12:33
有点点小建议:LZ自己调一下程序,就容易知道问题在哪里了。
有个简单的办法,就是自发自收 ...

之前用串口1接收串口助手的数据然后用串口1返回发送给串口助手显示,也就是自发自收已经实现了的。但是现在想串口1接收数据,比如GPS信号,在由串口2发送给电脑串口助手,无奈实验不成功,希望给予更多指点

使用特权

评论回复
地板
IJK| | 2013-5-8 13:25 | 只看该作者
之前用串口1接收串口助手的数据然后用串口1返回发送给串口助手显示,也就是自发自收已经实现了的。

可以:对串口2也这么做1遍

使用特权

评论回复
5
youdaping777| | 2013-5-8 15:14 | 只看该作者
先把串口1相关代码注释掉,单独测试一下串口2,看收发怎么样。

使用特权

评论回复
6
无敌小青年|  楼主 | 2013-5-8 16:30 | 只看该作者
youdaping777 发表于 2013-5-8 15:14
先把串口1相关代码注释掉,单独测试一下串口2,看收发怎么样。

恩,单独串口1接收发送以及单独串口2接收发送都测试过,行的。但是串口1接收在串口2发送不行,目前难受中。。。哎。。。

使用特权

评论回复
7
hawksabre| | 2013-5-8 18:31 | 只看该作者
单步调试   慢慢查一查   耐下心来   应该很快就可以解决这个问题   相信自己

使用特权

评论回复
8
jomosiron| | 2013-5-9 10:40 | 只看该作者
你把串口1接收到的数据放到数组里,然后在主程序中再用串口2发送数组内容就可以了

使用特权

评论回复
9
jomosiron| | 2013-5-9 10:40 | 只看该作者
你把串口1接收到的数据放到数组里,然后在主程序中再用串口2发送数组内容就可以了

使用特权

评论回复
10
youdaping777| | 2013-5-10 11:24 | 只看该作者
你参考一下。。。。。。

/**********************************************************
说明:
     本程序能实现USART1和USART2直接通信,发送和接收的
数据正确.

实验条件:
     直接把 USART1 的 TXD,RXD 和 USART2 的 RXD,TXD
对应连接即可.
**********************************************************/
#include "stm32f10x_lib.h"
#include "stm32f10x_conf.h"
#include "ili9320.h"
/******************************/
vu8 TxBuffer1 = 129; //USART1发送的数据
vu8 TxBuffer2 = 56; //USART2发送的数据
vu8 RxBuffer1;   //USART1接收数据
vu8 RxBuffer2;   //USART2接收数据
/********** 函数声明 *********/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void Sys_Init(void);
/********** 系统初始化 ******************/
void Sys_Init(void)
{
#ifdef DEBUG
     debug();
#endif
//----- 模块初始化配置 -------
     RCC_Configuration();
     NVIC_Configuration();
     GPIO_Configuration();
     USART_Configuration();
}
/******* 主函数 **********/
int main(void)
{
     Sys_Init();
     while(1);
}
/****** 系统时钟配置函数 ********/
void RCC_Configuration(void)
{
     ErrorStatus  HSEStartUpStatus;

     RCC_DeInit();         //RCC system reset(for debug purpose)
     RCC_HSEConfig(RCC_HSE_ON);      //Enable HSE
     HSEStartUpStatus = RCC_WaitForHSEStartUp();  //Wait till HSE is ready
     if(HSEStartUpStatus == SUCCESS)
     {
       FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
       FLASH_SetLatency(FLASH_Latency_2);      //Set 2 Latency cycles
       RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB clock  = SYSCLK
       RCC_PCLK2Config(RCC_HCLK_Div1);  //APB2 clock = HCLK
       RCC_PCLK1Config(RCC_HCLK_Div2);  //APB1 clock = HCLK/2
       RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
       RCC_PLLCmd(ENABLE);          //Enable PLL
       while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  //Wait till PLL is ready  
       RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock source
       while(RCC_GetSYSCLKSource() != 0x08);  //Wait till PLL is used as system clock source
       //-----------------------------------------------------
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1
             | RCC_APB2Periph_AFIO, ENABLE); //GPIOA and USART1 clock ENABLE
       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //USART2  clock ENABLE
     }
}
/****** 中断配置函数 ********/
void NVIC_Configuration(void)
{
     NVIC_InitTypeDef NVIC_InitStructure;
     //--- 在内存中进行调试 -----   
#ifdef VECT_TAB_RAM
     NIVC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
     //--- 在FLASH中进行调试 ----
#else
     NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
     //---- 配置USART1 和 USART2中断,并使能 -----
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      
     NVIC_Init(&NVIC_InitStructure);
     //------------------------------------
     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
     NVIC_Init(&NVIC_InitStructure);
}

/********** 通用I/O配置函数 ***************/
void GPIO_Configuration(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;

     /* Configure USART1 and USART2 Rx as input floating */
     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3 | GPIO_Pin_10;
     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
     //------------------
     /* Configure USART1 and USART2 Tx as alternate function push-pull */
     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2 | GPIO_Pin_9;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*****************************************************
函数名称:USART_Configuration(void)
功能说明:USART通信模块配置,配置USART工作模式;
输入参数:无
返回值  :无
*****************************************************/
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_Rx | USART_Mode_Tx;
   /* Configure USART1 */
   USART_Init(USART1, &USART_InitStructure);
   /* Configure USART2 */
   USART_Init(USART2, &USART_InitStructure);
  
   /* Enable USART1 Receive and Transmit interrupts */
   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   /* Enable USART2 Receive and Transmit interrupts */
   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
   USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
   /* Enable the USART1 */
   USART_Cmd(USART1, ENABLE);
   /* Enable the USART2 */
   USART_Cmd(USART2, ENABLE);
}
/*************************************************/
#ifdef DEBUG
void assert_failed(u8 *file, u32 line)
{
   while(1)
  {}
}
#endif



/***************************************************/
/***************************************************/

#include "stm32f10x_it.h"
/***********************************/
extern vu8   TxBuffer1;
extern vu8   TxBuffer2;
extern vu8   RxBuffer1;
extern vu8   RxBuffer2;
extern vu16  USART_IDx;
/*********** USART1 中断服务程序 *************/
void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    RxBuffer1 = USART_ReceiveData(USART1);
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);

    USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
  }
  
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
    /* Write one byte to the transmit data register */
    USART_SendData(USART1, TxBuffer1);
    USART_ClearITPendingBit(USART1, USART_IT_TC);                  
    USART_ITConfig(USART1, USART_IT_TXE, DISABLE);   
  }
}
/*********** USART2 中断服务程序 *************/
void USART2_IRQHandler(void)
{
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    RxBuffer2 = USART_ReceiveData(USART2);
    USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  }
  
  if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
  {   
    /* Write one byte to the transmit data register */
    USART_SendData(USART2, TxBuffer2);
    USART_ClearITPendingBit(USART2, USART_IT_TC);                  
    USART_ITConfig(USART2, USART_IT_TXE, DISABLE);   
  }  
}

使用特权

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

本版积分规则

1

主题

3

帖子

0

粉丝