打印

STM32能实现485和DMA的数据收发吗?

[复制链接]
275|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
稻花香1号|  楼主 | 2019-11-27 17:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
串口、DMA、中断配置代码如下:
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* ?2?:?USART2 Tx?GPIO????????? */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  
                            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
                              GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                            GPIO_Init(GPIOD, &GPIO_InitStructure);                          
                            GPIO_ResetBits(GPIOD, GPIO_Pin_11);                  
                                       
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);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;

//USART_ClearFlag(USART1, USART_FLAG_TC);
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_ITConfig(USART2,USART_IT_TC,DISABLE);  
USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART2, USART_IT_IDLE , ENABLE);
USART_Cmd(USART2, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA1
/* DMA1 Channel4 (triggered by USART1 Tx event) Config */
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_BASE+4;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_SEND_DATA;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 512;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
/* Enable USART1 DMA TX request */
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
//DMA_Cmd(DMA1_Channel4, DISABLE);

/* DMA1 Channel5 (triggered by USART1 Rx event) Config */
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_BASE+4;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_RECEIVE_DATA;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 512;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
/* Enable USART1 DMA RX request */
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);

}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Enable DMA Channel4 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*Enable DMA Channel5 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

中断处理函数代码:
void USART2_IRQHandler(void)
{
u16 DATA_LEN;
u16 i;
        u16 tmp;
        
if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)//?????????
{
DMA_Cmd(DMA1_Channel5, DISABLE);//??DMA,?????????
//USART_RX_STA = USART1->SR;//??SR,???DR????
//USART_RX_STA = USART1->DR;
DATA_LEN=512-DMA_GetCurrDataCounter(DMA1_Channel5);
        tmp=USART1_RECEIVE_DATA[0];
        tmp=USART1_RECEIVE_DATA[1];
        tmp=USART1_RECEIVE_DATA[2];
if(DATA_LEN>0)
{
while(USART1_TX_Finish==0)//????????????
{
;
}
//????DMA????
for(i=0;i<DATA_LEN;i++)
{
USART1_SEND_DATA=USART1_RECEIVE_DATA;
}
//USART?DMA??????????,?????????????????
DMA_Cmd(DMA1_Channel4, DISABLE); //??datasize?????????
DMA1_Channel4->CNDTR=DATA_LEN; //DMA1,?????
USART1_TX_Finish=0;//DMA???????
DMA_Cmd(DMA1_Channel4, ENABLE);
}
//DMA_Cmd(DMA1_Channel5, DISABLE);//??DMA,?????????
DMA_ClearFlag(DMA1_FLAG_GL5 | DMA1_FLAG_TC5 | DMA1_FLAG_TE5 | DMA1_FLAG_HT5);//???
DMA1_Channel5->CNDTR = 512;//???
DMA_Cmd(DMA1_Channel5, ENABLE);//???,??DMA
//?SR??DR??Idle
i = USART2->SR;
i = USART2->DR;
}
if(USART_GetITStatus(USART2, USART_IT_PE | USART_IT_FE | USART_IT_NE) != RESET)//??
{
USART_ClearITPendingBit(USART2, USART_IT_PE | USART_IT_FE | USART_IT_NE);
}
USART_ClearITPendingBit(USART2, USART_IT_TC);
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}


//USART1??DMA?????????
void DMA1_Channel4_IRQHandler(void)
{
DMA_ClearFlag(DMA1_FLAG_TC4);
DMA_Cmd(DMA1_Channel4, DISABLE);//??DMA
USART1_TX_Finish=1;//?DMA????
}
void DMA1_Channel5_IRQHandler(void)
{
DMA_ClearFlag(DMA1_FLAG_TC5);
DMA_Cmd(DMA1_Channel5, DISABLE);//??DMA,?????????
DMA1_Channel5->CNDTR = 512;//???
DMA_Cmd(DMA1_Channel5, ENABLE);//???,??DMA
}


说明:
同样的代码如果使用串口时能正常工作的, 但串口加了MAX485后不能进行正常数据收发(收到乱码,或不能收到数据)

请指教,谢谢

使用特权

评论回复

相关帖子

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

本版积分规则

381

主题

382

帖子

1

粉丝