初级会员

- 积分
- 74
- 金钱
- 74
- 注册时间
- 2015-8-2
- 在线时间
- 16 小时
|

楼主 |
发表于 2015-8-11 14:16:35
|
显示全部楼层
#include "delay.h"
#include "sys.h"
void dmai(u32 cmar);
void Senddma(void);
void uart_init(u32 bound);
u8 SendBuff[]={0x01,0x02,0x03,0x04,0x05};
u16 DMA1_MEM_LEN;//保存DMA每次数据传送的长度
int main(void)
{ u8 i=0;
delay_init(); //延时函数初始化
uart_init(9600); //串口初始化为9600
dmai((u32)SendBuff);
for(i=0;i<5;i++)
{
dmai((u32)SendBuff);
Senddma();
}
while(1)
{}
}
void dmai(u32 cmar)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
DMA_DeInit(DMA1_Channel7); //将DMA的通道1寄存器重设为缺省值
DMA1_MEM_LEN=5;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART2->DR; //DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //数据传输方向,从内存读取发送到外设
DMA_InitStructure.DMA_BufferSize =5; //DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //数据宽度为8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作在正常缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA通道 x拥有中优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
DMA_Init(DMA1_Channel7, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道USART1_Tx_DMA_Channel所标识的寄存器
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
}
//开启一次DMA传输
void Senddma(void)
{
DMA_Cmd(DMA1_Channel7, DISABLE ); //关闭USART1 TX DMA1 所指示的通道
DMA_SetCurrDataCounter(DMA1_Channel4,DMA1_MEM_LEN);//DMA通道的DMA缓存的大小
DMA_Cmd(DMA1_Channel7, ENABLE); //使能USART1 TX DMA1 所指示的通道
}
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(USART2); //复位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX   A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
USART_Cmd(USART2, ENABLE); //使能串口
}
这个代码的现象如下,
还有一种就是我把发送指令放在while(1)大循环环里:
int main(void)
{
delay_init();
uart_init(9600);
dmai((u32)SendBuff);
while(1)
{
dmai((u32)SendBuff);
Senddma();
}
}
现象如下
|
|