打印

使用STM32作串口实现时接收到数据不正常

[复制链接]
2297|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
joypark159|  楼主 | 2013-7-17 14:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用串口助手通过SP485发送数据,但在USART3的数据寄存器中得到的数据不是发送的数据


#include <stm32f10x.h>

#include "usart1.h"
#include <stdarg.h>


void USART3_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
          
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                                 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                //ÍÆÍìÊä³ö-TX
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë-RX
  GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//RTX
  GPIO_Init(GPIOB, &GPIO_InitStructure);
         
          
        /* USART3 ¹¤×÷ģʽÅäÖà */
        USART_InitStructure.USART_BaudRate = 9600;        //²¨ÌØÂÊÉèÖãº9600
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //Êý¾ÝλÊýÉèÖãº8λ
        USART_InitStructure.USART_StopBits = USART_StopBits_1;         //ֹͣλÉèÖãº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_ClearFlag(USART3,USART_FLAG_OR);
        USART_Init(USART3, &USART_InitStructure);  //³õʼ»¯USART3
        USART_Cmd(USART3, ENABLE);// USART3ʹÄÜ


}

/*·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý*/
void UART3SendByte(unsigned char SendData)
{                          
                                GPIO_SetBits(GPIOB,GPIO_Pin_14);
        USART_SendData(USART3,SendData);
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);            
}  

/*½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý*/
unsigned char UART3GetByte(unsigned char* GetData)

                                {  
                                        GPIO_ResetBits(GPIOB,GPIO_Pin_14);
        if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
        {  return 0;//ûÓÐÊÕµ½Êý¾Ý
                }
        *GetData = USART_ReceiveData(USART3);
        return 1;//ÊÕµ½Êý¾Ý
}
/*½ÓÊÕÒ»¸öÊý¾Ý£¬ÂíÉÏ·µ»Ø½ÓÊÕµ½µÄÕâ¸öÊý¾Ý*/
void UART3Test(void)
{
       unsigned char i = 0;

       while(1)
       {   
                 while(UART3GetByte(&i))
        {
         UART3SendByte(i);
        }      
       }     
}


沙发
cjhk| | 2013-7-17 18:35 | 只看该作者
不是很了解这一块    楼主  尝试着将代码重新查查错  看看有没有bug

使用特权

评论回复
板凳
cjhk| | 2013-7-17 18:35 | 只看该作者
或者   降低波特率试一试    应该问题不是很大   

使用特权

评论回复
地板
lukehua1990| | 2013-7-17 20:28 | 只看该作者
keil编译的么?
设置里有个使用什么什么库,需要勾上,楼主勾了么

使用特权

评论回复
5
zhaoyu2005| | 2013-7-18 09:18 | 只看该作者
用光耦没?如果用了,并且是低速光耦,那很正常了

使用特权

评论回复
6
zhaoyu2005| | 2013-7-18 09:19 | 只看该作者
还有一个就是头文件对晶振频率的定义,时钟的配置

使用特权

评论回复
7
通宵敲代码| | 2013-7-18 11:10 | 只看该作者
通信时钟频率不对。

使用特权

评论回复
8
joypark159|  楼主 | 2013-7-22 11:52 | 只看该作者
好了,是系统时钟配置的错误,默认的是8M。我的外部晶振是12M,改了系统文件就好了

使用特权

评论回复
9
outstanding| | 2013-7-22 11:54 | 只看该作者

使用特权

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

本版积分规则

8

主题

30

帖子

0

粉丝