|
求教怎样将STM32的DMA串口接收到的数据存到一个数组中,然后再用串口发送出来,谢谢
[复制链接]
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
LCD_Port_Init(115200);
while(1){
if(RxOverFlag2 = 1){
USART_SendString2(USART_RXBUF);
USART_SendString3(buzzer);
}
}
}
/*函数名称:DMA_Configuration2
函数用途:STM32 USART2接收DMA通道配置
函数参数:无
函数说明:无
*/
void DMA_Configuration2(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 Channel7 (triggered by USART2 Rx event) Config */
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART_RXBUF;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 400;
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_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
}
/*函数名称:LCD_Port_Init
函数用途:与LCD连接的STM32 USART2配置
函数参数:baud,串口波特率配置
函数说明:无
*/
void LCD_Port_Init(unsigned int baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //PA2&PA3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //Enable DMA clock
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
DMA_Configuration2(); //
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;
/* Configure USART1 Rx (PA3) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOA, &GPIO_InitStructure );
/* Configure USART1 Tx (PA2) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init( GPIOA, &GPIO_InitStructure );
//
USART_Init( USART2, &USART_InitStructure );
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE); //Channel 6:USART1-RX
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); //
USART_Cmd(USART2, ENABLE);
}
unsigned int dummy2 = 0; //dummy
unsigned char RxOverFlag2 = 0; //串口2中断标志
unsigned short RxNumber2 = 0; //串口2中断接收数量
void USART2_IRQHandler(void)
{
if((USART2->SR & (1<<4)) != 0)
{
dummy2 = USART2->SR;
dummy2 = USART2->DR;
RxOverFlag2 = 1;
DMA1_Channel6->CCR &= 0xFFFE;
RxNumber2 = 400 - DMA1_Channel6->CNDTR;
DMA1_Channel6->CNDTR = 400;
DMA1_Channel6->CCR |= 0x0001;
}
}
|
|