UART1_DMA.c
char Uart1_DMA_Rxdata[DMA_DATA_LEN];
char Uart1_DMA_Txdata[DMA_DATA_LEN];
u16 DMA_Rx_LEN = 0;
void Usart1_Config(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART1_nitStructure;
NVIC_InitTypeDef USART1_NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_DeInit(USART1);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART1_nitStructure.USART_BaudRate=bound;
USART1_nitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART1_nitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART1_nitStructure.USART_Parity=USART_Parity_No;
USART1_nitStructure.USART_StopBits=USART_StopBits_1;
USART1_nitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART1_nitStructure);
USART_Cmd(USART1,ENABLE);
USART1_NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
USART1_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=15;
USART1_NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
USART1_NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接收(非空)中断
// USART_ITConfig(USART1, USART_IT_TC, ENABLE);//发送为空
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//串口空闲中断
NVIC_Init(&USART1_NVIC_InitStructure);
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
void Usart1_DMA1_Config(u32 Tx_Addr,u16 Tx_Addr_DataNumber,u32 Rx_Addr,u16 Rx_Addr_DataNumber)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitTypeDef DMA_InitStruct;
NVIC_InitTypeDef DMA1_NVIC_InitStruct;
DMA1_NVIC_InitStruct.NVIC_IRQChannel=DMA1_Channel4_IRQn;
DMA1_NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=14;
DMA1_NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
DMA1_NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
NVIC_Init(&DMA1_NVIC_InitStruct);
DMA_InitStruct.DMA_MemoryBaseAddr=Tx_Addr;
DMA_InitStruct.DMA_PeripheralBaseAddr=(u32)&USART1->DR;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralDST;
DMA_InitStruct.DMA_BufferSize=Tx_Addr_DataNumber;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_Mode=DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority=DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStruct);
DMA_InitStruct.DMA_MemoryBaseAddr=Rx_Addr;
DMA_InitStruct.DMA_PeripheralBaseAddr=(u32)&USART1->DR;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize=Rx_Addr_DataNumber;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_Mode=DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority=DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
USART_DMACmd(USART1,USART_DMAReq_Tx|USART_DMAReq_Rx,ENABLE);
// DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
DMA_Init(DMA1_Channel5,&DMA_InitStruct);
DMA_Cmd(DMA1_Channel4,ENABLE);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void DMA1_Enable(DMA_Channel_TypeDef* DMAy_Channelx,u16 DataNumber)
{
DMA_Cmd(DMAy_Channelx,DISABLE);
DMA_SetCurrDataCounter(DMAy_Channelx,DataNumber);
DMA_Cmd(DMAy_Channelx,ENABLE);
}
stm32f10x_it.c【疑问:在写中断函数名时候,不知道去哪里找】
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) == 1 )//接收空闲中断//接收完成
{
DMA_Cmd(DMA1_Channel5,DISABLE);//关闭接收
DMA_Rx_LEN =200-DMA_GetCurrDataCounter(DMA1_Channel5);//实际接收数据大小(动态求数据量)
// DMA_Cmd(DMA1_Channel4,DISABLE);
// DMA_SetCurrDataCounter(DMA1_Channel4,DMA_Rx_LEN);//设置发送数据
// DMA_Cmd(DMA1_Channel4,ENABLE);//使能发送
DMA1_Enable(DMA1_Channel4,DMA_Rx_LEN);//等效前面3句
USART1->SR;
USART1->DR;//必须先读SR,再读DR,才能清除接收空闲标志位
USART_ClearITPendingBit(USART1,USART_IT_IDLE);
}
}
void DMA1_Channel4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC4) == 1 )//发送完成
{
DMA_Cmd(DMA1_Channel4,DISABLE); //关闭发送
// DMA_Cmd(DMA1_Channel5,DISABLE);//关闭接收
// DMA_SetCurrDataCounter(DMA1_Channel5,200);//配置接收
// DMA_Cmd(DMA1_Channel5,ENABLE);//使能接收
DMA1_Enable(DMA1_Channel5,200);//等效前面3句
DMA_ClearITPendingBit(DMA1_IT_TC4);
}
}
main.c
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
SysTick_Config(SystemCoreClock/1000);
LED_GPIO_Config();
Usart1_Config(115200);
Usart1_DMA1_Config((u32) Uart1_DMA_Rxdata,(u16)sizeof(Uart1_DMA_Rxdata),(u32) Uart1_DMA_Rxdata,(u16) sizeof(Uart1_DMA_Rxdata));
while(1)
{
if(SysTickTime>=1000)//1S流水灯
{
GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)?
GPIO_ResetBits(GPIOC, GPIO_Pin_13),
SysTickTime=0:
GPIO_SetBits(GPIOC, GPIO_Pin_13),
SysTickTime=0;
printf("\r\nDMA_Rx_LEN=%d\r\n",DMA_Rx_LEN);
DMA_Rx_LEN=0;//清空接收数据大小
}
}
}
数据量太小了 传输进度没搞出来
|