初级会员

- 积分
- 102
- 金钱
- 102
- 注册时间
- 2016-3-9
- 在线时间
- 24 小时
|
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));
}
|