全国中小企业网站,新网站seo外包,公司注销的网站备案,专业格泰建站环形队列DMA空闲中断接收串口数据 一.序言二.实验原理三.实战是检验真理的唯一标准3.1 usart1.c3.2 串口中断 三.队列代码4.1 fifo.c4.2 fifo.h 五.结语 一.序言
本次实验利用环形队列DMA空闲中断串口。。通过这个实验可以非常深入的理解队列#xff0c;DMA,串口的知识。如果… 环形队列DMA空闲中断接收串口数据 一.序言二.实验原理三.实战是检验真理的唯一标准3.1 usart1.c3.2 串口中断 三.队列代码4.1 fifo.c4.2 fifo.h 五.结语 一.序言
本次实验利用环形队列DMA空闲中断串口。。通过这个实验可以非常深入的理解队列DMA,串口的知识。如果你能自己实现掌握这个实验那么你应该基本掌握了队列DMA,串口的知识。
二.实验原理
本次使用的是用环形队列当缓冲器区接收串口数据。我们可以先区了解DMA的空闲中断。本次实验就是使用DMA空闲中断。这里就简单介绍一下当串口接收到一帧数据后就会产生中断那么如何判断数据是一帧呢这里的判断机制就是如果收到数据后大概接收一个字节的时间都没有接收到数据的话就判断已经接收的数据是一帧。接收一帧数据后串口就会产生空闲中断。同时DMA会把串口DR移位寄存器的值搬运到环形队列缓冲区。我们只需要在环形队列缓冲器拿数据即可。
三.实战是检验真理的唯一标准
仅仅通过理论只能说你知道有这个东西但是你可能并不会。下面我通过代码讲解也帮助深入理解理论。
3.1 usart1.c void Usart1_Init(void)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;DMA_InitTypeDef DMA_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //USART1_TX GPIOA.9GPIO_InitStructure.GPIO_Pin GPIO_Pin_9; //PA.9GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode GPIO_Mode_AF_PP; GPIO_Init(GPIOA, GPIO_InitStructure);//³õʼ»¯GPIOA.9//USART1_RX GPIOA.10³õʼ»¯GPIO_InitStructure.GPIO_Pin GPIO_Pin_10;//PA10GPIO_InitStructure.GPIO_Mode GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, GPIO_InitStructure);////Usart1 NVIC ÅäÖÃNVIC_InitStructure.NVIC_IRQChannel USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority5 ;NVIC_InitStructure.NVIC_IRQChannelSubPriority 0; NVIC_InitStructure.NVIC_IRQChannelCmd ENABLE; NVIC_Init(NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷USART_InitStructure.USART_BaudRate 115200USART_InitStructure.USART_WordLength USART_WordLength_8bUSART_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_ITConfig(USART1, USART_IT_IDLE , ENABLE);//使能发送完成中断--通过串口发送数据USART_ITConfig(USART1, USART_IT_TC , ENABLE);USART_DMACmd(USART1, USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE);USART_Cmd(USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel5);//外设地址--串口1 的DR寄存器DMA_InitStructure.DMA_PeripheralBaseAddr (uint32_t)(USART1-DR);DMA_InitStructure.DMA_MemoryBaseAddr (uint32_t)Uart1_Rx_Buffer; DMA_InitStructure.DMA_DIR DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize BSP_UART1_RX_SIZE; 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_Medium;DMA_InitStructure.DMA_M2M DMA_M2M_Disable;//串口1的接收引脚为DAM1的通道5DMA_Init(DMA1_Channel5, DMA_InitStructure)DMA_Cmd (DMA1_Channel5,ENABLE); //初始化环形队列Fifo_Init(Uart1_Rx_Fifo,Uart1_Rx_Buffer,BSP_UART1_RX_SIZE);3.2 串口中断
void USART1_IRQHandler(void)
{ //加上volatile 关键字只是为了防止警告volatile uint32_t temp 0;BaseType_t xHigherPriorityTaskWoken pdFALSE;if(USART_GetITStatus(USART1,USART_IT_IDLE)! RESET)//¿ÕÏÐÖжÏ{//根据数据量数据入队列//DMA_GetCurrDataCounter(DMA1_Channel5); 通道5还剩下多少个数据应该传输。//初始化的时候设置的通道5传输多少数据。Uart1_Rx_Fifo.in BSP_UART1_RX_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);//先读SR 再读DR 只是为了消除空闲中断标志位。手册上有说明temp USART1-SR; temp USART1-DR;USART_ClearITPendingBit(USART1,USART_IT_IDLE);}三.队列代码
代码可能看起来简单但是不是那么容易理解。得自己慢慢体会才能真正掌握 而不是一昧的copy,不然出了问题也解决不了自己代码水平也不能提高。
4.1 fifo.c
#include sys.h
#include app_fifo.h//初始化
void Fifo_Init(FIFO_Type* fifo, uint8_t* buffer, uint16_t size)
{fifo-buffer buffer;fifo-in 0 ;fifo-out 0;fifo-size size;
}
//从队列中拿数据
uint16_t Fifo_Get(FIFO_Type* fifo, uint8_t* buffer, uint16_t len)
{uint16_t lenght;uint16_t in fifo-in; uint16_t i;lenght (in fifo-size - fifo-out)%fifo-size;if(lenght len)lenght len;for(i 0; i lenght; i){buffer[i] fifo-buffer[(fifo-out i)%fifo-size];}fifo-out (fifo-out lenght)%fifo-size;return lenght;
}
4.2 fifo.h
#ifndef FIFO_H
#define FIFO_H
#include stm32f10x.h
typedef struct {uint8_t* buffer; uint16_t in; uint16_t out; uint16_t size;
}FIFO_Type;void Fifo_Init(FIFO_Type* fifo, uint8_t* buffer, uint16_t size);uint16_t Fifo_Get(FIFO_Type* fifo, uint8_t* buffer, uint16_t len);#endif
五.结语
代码放出来的就是以上这些都放上去也比较麻烦同时也没什么意义。写这篇博客是想让大家有大致的思路以及参考代码从而根据自己的项目或者需求区进行改动。最后如果真的需要全部代码的可以私信博主最好点点关注 文章转载自: http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn