各位大大好,打扰了,最近在写一个串口接收程序,但配置了好久也没配制出来,也参考一些前辈写的配置,然后自己配置了个串口DMA接收数据,但发现配置都设置好了,DMA却始终收不到发送的数据,显示的全部为0,哪位前辈有这方面经验的希望指导下,万分感谢!下面是的串口DMA配置程序:- #include "usart_led.h"
- //int USART1_DATA;
- unsigned int buffer[64]={0};
- unsigned char XY_led_num;
- unsigned char XX=0,YY=0;
- unsigned char flag_IR=0;
- extern unsigned char tabl_R[8][8];
- u8 data[64];
- uint32_t temp = 0;
-
- void USART1_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //Rx
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
-
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)(&USART1->DR);
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)buffer;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = 64;
- 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_Normal;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
-
- DMA_Cmd(DMA1_Channel1, 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_Tx | USART_Mode_Rx;
- USART_Init(USART1, &USART_InitStructure);
- USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); // Enable USART1 DMA TX request
- USART_Cmd(USART1, ENABLE);
-
- USART_ITConfig(USART1,USART_IT_TC,DISABLE);
- USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
- USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
-
- USART_ClearFlag( USART1,USART_FLAG_IDLE );
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- #ifdef VECT_TAB_RAM
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else //VECT_TAB_FLASH
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
-
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_INIT()
- {
- USART1_Configuration();
- NVIC_Configuration();
- }
- void USART1_SendData(uint16_t Data)
- {
- USART_SendData(USART1,(uint16_t)Data);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
- }
- void USART1_IRQHandler(void)
- {
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
- {
- flag_IR=1;
- temp = USART1->SR;
- temp = USART1->DR;
- }
- __nop();
- }
复制代码
|
我是用中断的方式接收啊!
刚才注意到串口1的DMA接收通道是通道5,然后改了过来现在能接收数据了,但接收的数据都是乱的啊,哭晕在厕所:'
我刚才又配了个不用DMA串口传输的程序,能用了,但DMA的还是没解决。。。
楼主是想用DMA IDLE 空闲中断接收串口数据吧!!
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
flag_IR=1;
temp = USART1->SR;
temp = USART1->DR; }
__nop();
}
你参考下吧。。。你上面中断处理程序还缺进中断关闭DMA通道和获取CNDTR里数据长度。以上都处理好了。需要再给DMA CNDTR重新赋值,然后重新开启DMA接收数据。。
void USART1_IRQHandler(void)
{
uint32_t Length = 0;//数据长度
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5,DISABLE);
Length = USART1->SR;
Length = USART1->DR; //清USART_IT_IDLE标志
Length = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5); //设置传输数据长度
DMA1_Channel5->CNDTR = UART_RX_LEN;//重装填,并让接收地址偏址从0开始
DMA_Cmd(DMA1_Channel5, ENABLE);//处理完,重开DMA
}
__nop();
}
这个我也配了,但也是接收错误!
嗯好的,等下发给你!
test program.rar
2016-1-22 09:49 上传
点击文件名下载附件
下载积分: ST金币 -15.32 MB, 下载次数: 59, 下载积分: ST金币 -1
Usart DMA