請問我利用DMA來接收PC端輸入的數據資料,不用中斷來實現,可是卻無法得到資料。想要請問哪裡出錯呢? 謝謝 #define USART1_DR_Base 0x40013804 uint32_t SendBuff[4]; void USART_Configuration1(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = 4800; 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_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TXE, ENABLE); } void USART_DMA_Configuration(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ReadBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize =4; 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_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC,ENABLE); USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); DMA_Cmd(DMA1_Channel4, ENABLE); } 接收PC端數據,此段寫再MAIN裡面 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { temp = USART1->SR; temp = USART1->DR; DMA_Cmd(DMA1_Channel4,DISABLE); temp = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel4); for (i=0; i<temp ; i++) { Data_Receive_Usart = SendBuff[i]; // 讀取PC數據 } DMA_SetCurrDataCounter(DMA1_Channel4,UART_RX_LEN); DMA_Cmd(DMA1_Channel4,ENABLE); } |
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
您指的應該是兩段碼吧!
2、你不是电脑发数据到UART_DR,再由DMA传输到readbuf吗?数据到这里查看就行。怎么看到
你在MAIN里不停读UART_DR.
且接收到的數字怎麼可以讓其他函式使用呢?
我將我的程式改成以下接收到字元,再將字元輸出。這樣是可以接收到每個字元,但是怎麼變成一個數字和如何被其他函式所使用,就不得而知了。
if(USART1->SR & USART_FLAG_RXNE)
{
temp = USART1->SR;
temp = USART1->DR;
USART_SendData(USART1, temp);
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
}