本帖最后由 hpdell 于 2012-12-14 10:13 编辑
参考stm32f1xx的帖子修改而来,望多多指教
在此感谢感谢!!!11
http://www.amobbs.com/forum.php? ... C%E5%8F%91%E9%80%81
顺便请教下,我想用DMA发送一个字符或者多个字符时,调试没有成功,请大仙们指点指点一下!
此工程文件不含固件库,使用者需要自己增加固件库,固件库与本工程文件放同一目录就可以了,路径请自行修改!!
//USART1使用DMA接收数据中断服务程序, DMA接收完成中断
void DMA2_Stream5_IRQHandler(void)
{
/* Test on DMA Stream Transfer Complete interrupt */
if(DMA_GetITStatus(DMA2_Stream5, DMA_IT_TCIF5)) //此处是我增加,不然单片机会不停的发送数据给pc机
{
DMA_ClearITPendingBit(DMA2_Stream5,DMA_IT_TCIF5);
DMA_ClearITPendingBit(DMA2_Stream5,DMA_IT_TEIF5);
DMA_Cmd(DMA2_Stream5, DISABLE); //关闭DMA,防止处理其间有数据
DMA2_Stream5->NDTR = RECAVEMAXCOUNT; //重装填
DMA_Cmd(DMA2_Stream5, ENABLE); //处理完,重开DMA
}
}
//USART1使用DMA发数据中断服务程序
void DMA2_Stream7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream7, DMA_IT_TCIF7) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TCIF7);
DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TEIF7);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA2_Stream7, DISABLE); //关闭DMA
USART1_TxFinish = 1; //置DMA传输完成
}
}
工程文件:
V26-DMA2_Stream5.zip
(76.78 KB, 下载次数: 151)
2012-12-14 10:04 上传
点击文件名下载附件
void InitDMA1Chanle5ForUSART1_RX(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_BufferSize = 512;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)UART1_REC_Buffer1;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void InitUSART1(void)
{
/* Enable USART1 Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Config USART1 Parameter */
UART_InitStrcture.USART_BaudRate = 115200;
UART_InitStrcture.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
UART_InitStrcture.USART_Parity = USART_Parity_No;
UART_InitStrcture.USART_StopBits = USART_StopBits_1;
UART_InitStrcture.USART_WordLength = USART_WordLength_8b;
UART_InitStrcture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&UART_InitStrcture);
/*Config UART_IO:PA9-->TXD PA10-->RXD */
GPIO_InitStrcture.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStrcture.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStrcture.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrcture);
GPIO_InitStrcture.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStrcture.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStrcture);
/* Config USART1 Interrupt */
USART_ITConfig(USART1,USART_IT_TC,DISABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*Enable USART1 */
USART_Cmd(USART1,ENABLE);
}
//中断处理函数
void USART1_IRQHandler(void)//2uS
{
u32 temp,i;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
//USART_ClearFlag(USART1,USART_IT_IDLE);
temp = USART1->SR;
temp = USART1->DR; //清USART_IT_IDLE标志
DMA_Cmd(DMA1_Channel5,DISABLE);
temp = (512-DMA1_Channel5->CNDTR);
for(i=0;i<temp;i++)// 数据转移(可优化)
{
InsertLineQueue(&Comm_Main,UART1_REC_Buffer1);
}
RecLED_On = TRUE;
DMA1_Channel5->CNDTR = 512;
DMA_Cmd(DMA1_Channel5,ENABLE);
}
}
一周热门 更多>