本帖最后由 lily_2010 于 2010-10-19 16:47 编辑
下面是我代码:我是接收20个数据,然后返回100个数据,在接收中断里打开DMA发送,在发送完成后打开接收中断
接收中断:
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) !=RESET)
{
/* Read one byte from the receive data register */
RxBuffer1[RxCounter++] = USART_ReceiveData(USART1);
if(RxCounter == 20)
{
// Disable the USART1 Receive interrupt
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
RxCounter=0;
TxBuffer1[0]=0x11;
TxBuffer1[1]=0x22;
TxBuffer1[2]=0x33;
TxBuffer1[3]=0x44;
TxBuffer1[4]=0x55;
GPIO_SetBits(GPIOA, GPIO_Pin_12); //使能发送 RTS=1
DMA_Configuration();
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); //打开DMA发送
DMA_Cmd(DMA1_Channel4, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
if(USART_GetITStatus(USART1,USART_IT_ORE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_ORE);
}
}
}
主函数:
int main(void)
{
#ifdef DEBUG
debug();
#endif
u16 i,j;
for(i=0;i<20;i++)
{
RxBuffer1=0x00;
}
for(i=0;i<800;i++)
{
TxBuffer1=0x00;
}
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* Configure the DMA */
DMA_Configuration();
GPIO_ResetBits(GPIOA, GPIO_Pin_12); //receive RTS=0
/* USART1 and USART2 configuration ------------------------------------------------------*/
USART_DeInit(USART1);
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 DMA TX request */
// USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
/* Enable DMA1 Channel4 */
//DMA_Cmd(DMA1_Channel4, ENABLE);
GPIO_ResetBits(GPIOA, GPIO_Pin_12); //使能接收 RTS=0
/* Enable the USART1 Receive Interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
while (1)
{
/* Wait until DMA1_Channel 4 Transfer Complete */
while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET) //等待发送完成
{
}
DMA_ClearFlag(DMA1_FLAG_GL4);
//for(i=2000;i>0;i--);
GPIO_ResetBits(GPIOA, GPIO_Pin_12); //使能接收 RTS=0
/* Enable the USART1 Receive Interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
}
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : DMA_Configuration
* Description : Configures the DMA.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 Channel4 (triggered by USART1 Tx event) Config */
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
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_Mode = DMA_Mode_Circular;//add in 10.11
//DMA_InitStructure.DMA_Priority = ;DMA_Priority_Medium
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
} |