OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2926|回复: 1

stm32的485接收问题

[复制链接]

14

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
102
金钱
102
注册时间
2016-3-9
在线时间
24 小时
发表于 2016-8-23 21:08:08 | 显示全部楼层 |阅读模式
5金钱
硬件
       stm32模块 ,485模块
使用串口助手作为上位机(发送)数据和(接受)数据,发送一位数据可以正常的发送和接收,但是发送多位的时候,就不行,下面是我的代码。使用的是串口2,我这边我想发送三位数据,之后串口那边可以打印出来。
麻烦大家帮我看一下我的程序

#include "stm32f0xx.h"
#include "string.h"
#include "usart_1.h"
#include "delay.h"
#include "stdio.h"

#define TX_485 GPIO_SetBits(GPIOA,GPIO_Pin_5) //µÍµçƽ,½ÓÊÕ
#define RX_485 GPIO_ResetBits(GPIOA,GPIO_Pin_5)
uint8_t array[3] = {0};
static uint8_t count = 0;
uint8_t rx_flag;
void USART_2_Init()
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Enable GPIO clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);
    /* Enable USART2 clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    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;
    /* Connect PXx to USARTx_Tx */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1);
    /* Connect PXx to USARTx_Rx */
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);

    //PB8
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Configure USART Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Configure USART Rx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    /* USART configuration */
    USART_Init(USART2,&USART_InitStructure);
    USART_ClearFlag(USART2,USART_FLAG_TC);
    USART_ITConfig(USART2, USART_IT_RXNE,ENABLE);  //ÔÊÐí´®¿Ú2½ÓÊÕÖжÏ
    /* Enable USART */
    USART_Cmd(USART2, ENABLE);
    /* Enable the USARTx Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}
void USART2_IRQHandler(void)
{
    uint8_t dat;
    if(USART_GetITStatus(USART2, USART_IT_RXNE)) // ½ÓÊÕµ½Êý¾Ý
    {
        dat = USART_ReceiveData(USART2);
        USART_ClearFlag(USART2,USART_FLAG_RXNE);
        while(count < 3)
        {
            array[count] = dat;
            count++;
        }
        if(count == 3)
        rx_flag = 1;
    }
}
void RS485_Send_Data(uint8_t *buf)
{
     uint8_t t = 0;
  TX_485;
     for(t = 0;t < 3 ; t++)
     {
        USART_SendData(USART2,buf[t]);
     }
while(!USART_GetFlagStatus(USART2, USART_FLAG_TXE))
        ;   
     RX_485;
}
int main()
{
       USART_2_Init();
       delay_init();
       RX_485;

       while(1)
       {
             if(rx_flag==1)
            {
                 RS485_Send_Data(array);
                 rx_flag=0;
            }
      }
}

最佳答案

查看完整内容[请看2#楼]

for(t = 0;t < 3 ; t++) { USART_SendData(USART2,buf[t]); } while(!USART_GetFlagStatus(USART2, USART_FLAG_TXE)); 改成 for(t = 0;t < 3 ; t++) { USART_SendData(USART2,buf[t]); while(!USART_GetFlagStatus(USART2, USART_FLAG_TXE)); }
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

22

主题

751

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1606
金钱
1606
注册时间
2015-6-10
在线时间
223 小时
发表于 2016-8-23 21:08:09 | 显示全部楼层
     for(t = 0;t < 3 ; t++)
     {
        USART_SendData(USART2,buf[t]);
     }
while(!USART_GetFlagStatus(USART2, USART_FLAG_TXE));

改成
     for(t = 0;t < 3 ; t++)
     {
        USART_SendData(USART2,buf[t]);
        while(!USART_GetFlagStatus(USART2, USART_FLAG_TXE));

     }

回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-1 06:50

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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