STM32/STM8技术论坛
直播中

尼一甲

未满1年用户 4经验值
擅长:可编程逻辑 嵌入式技术 存储技术
私信 关注
[问答]

rs485通信没接收到数据就发送了00

接收函数
uint8_t Reception_buff[RX_SIZE] attribute((aligned(4)));
uint8_t USART_flag=0;
void USART1_IRQHandler(void)
{
if(USART1 ->ISR & USART_FLAG_IDLE){
Reception_buff[RX_SIZE-1] = USART1->RDR & 0x1ff; //读取一次数据清除标志
USART_flag = 0xff;
DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN);
DMA1_Channel3->CNDTR = RX_SIZE;
DMA1_Channel3->CCR |= DMA_CCR_EN;
}
USART1->ICR = (uint32_t)USART_FLAG_IDLE;
}
发送函数
void Scan_data(uint8_t *p,uint8_t num)
{
GPIOB->BRR |= GPIO_Pin_1;
while(num--){
while((USART1->ISR & 0x40)==0);
USART1->TDR = *p++;
}
while((USART1->ISR & 0x40)==0);
delay_ms(2);
GPIOB->BSRR |= GPIO_Pin_1;
}
串口配置
void USART1_Init(uint32_t baud)
{
char usart_isr;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;

USART1_DMA_Init();

USART_DeInit(USART1);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);  //使能GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART的时钟
/* USART1的端口配置 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);	//配置PA9成第二功能引脚	TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);	//配置PA10成第二功能引脚  RX	
//485流向控制
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1的基本配置 */
USART_InitStructure.USART_BaudRate = baud;              //波特率
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(USART1, &USART_InitStructure);		
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); //使能空闲中断
USART_Cmd(USART1,ENABLE);                             //使能USART1
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
/* USART1的NVIC中断配置 */
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0x0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
usart_isr = USART1->ISR ;		
GPIOB->BSRR |= GPIO_Pin_1;

}

int fputc(int c, FILE stream)
{
/
将Printf内容发往串口 */
GPIOB->BRR |= GPIO_Pin_1;
USART1->TDR = (uint8_t)c;
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
delay_ms(1);
GPIOB->BSRR |= GPIO_Pin_1;
return (c);
}

extern uint8_t Reception_buff[RX_SIZE] attribute((aligned(4)));

static void USART1_DMA_Init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitMode;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

DMA_DeInit(DMA1_Channel3);
DMA_InitMode.DMA_BufferSize = RX_SIZE;
DMA_InitMode.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitMode.DMA_M2M = DMA_M2M_Disable;
DMA_InitMode.DMA_MemoryBaseAddr = (uint32_t)Reception_buff;
DMA_InitMode.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitMode.DMA_Mode = DMA_Mode_Circular;
DMA_InitMode.DMA_PeripheralBaseAddr = (uint32_t)&USART1->RDR;
DMA_InitMode.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitMode.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitMode.DMA_Priority = DMA_Priority_High;
DMA_InitMode.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_Init(DMA1_Channel3,&DMA_InitMode);

DMA_Cmd(DMA1_Channel3,ENABLE);

}

屏幕截图 2024-08-24 162613.png屏幕截图 2024-08-24 162253.png
芯片是stm32f030f4,程序运行到发送函数的GPIOB->BRR |= GPIO_Pin_1;然后会发送00。但我没给从机发数据,他自动回传的00,这是为什么。

已退回10积分

回帖(1)

syemour

2024-9-2 17:23:05
看起来是不断在重启
举报

更多回帖

发帖
×
20
完善资料,
赚取积分