初级会员

- 积分
- 118
- 金钱
- 118
- 注册时间
- 2015-3-6
- 在线时间
- 1 小时
|
5金钱
#include "stm32f10x.h"
/*
开发板:原子mini开发板,f103的
实验内容:
1、usart通过DMA方式把数据 SendBuff 发送到外设(即usart数据寄存器),发送成功LED状态会取反
2、通过usart把通过DMA接收的数据data=SendBuff发送给串口调试助手
(程序没有出错,可以下载到板子上使用)
我的问题:不知道我对DMA方式有没有理解错误,
串口助手收到的数据一直是00,和DMA传输得到的不一样,
希望各位指点指点,多谢!!!
*/
void Delay_Int(void);
void delay_ms(u16 nms);
void GPIO_int(void);
void usart_init(void);
void DMA_init(void);
u8 SendBuff=6; //DMA要发送的数据
u8 data; //读取DMA发来的数据
DMA_InitTypeDef DMA_InitStructure;
int main(void)
{
Delay_Int();
GPIO_int();
usart_init();
DMA_init();
while(1)
{
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);//使能DMA功能
/*开启DMA一次传送*/
DMA_Cmd(DMA1_Channel4, DISABLE ); //关闭USART1 TX DMA1 所指示的通道
DMA_InitStructure.DMA_BufferSize = sizeof(SendBuff);
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel4, ENABLE); //使能USART1 TX DMA1 所指示的通道
while(1)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC4) == SET)//等待通道4传输完成
{
DMA_ClearFlag(DMA1_FLAG_TC4); //清除发送完成标志
GPIOA->ODR^=(1<<8);//LED1状态取反
break;
}
data=USART1->DR;//读取DMA发来的数据
USART_SendData(USART1, data);//Data: 把数据发送到串口助手
delay_ms(300);
GPIOD->ODR^=(1<<2);//LED2状态取反
}
}
}
void usart_init(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);//使能或者失能USART外设
}
void GPIO_int()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9,USART1_TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA10 ,USART1_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0--> A.8 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED0--> d.2 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
void DMA_init()
{
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
delay_ms(2);
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&(USART1->DR);//外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = SendBuff; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //外设作为数据目的地
DMA_InitStructure.DMA_BufferSize = sizeof(SendBuff);//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通道优先级为高中
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //禁止DMA通道设置为内存至内存传输
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
void Delay_Int()
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//外部时钟,8分频,72/8M
}
/*ms延时 */
void delay_ms(u16 nms)//nms<=1864
{
u32 flag;
u32 num=(u32)nms*9*1000;//72M晶振8分频成为9M
SysTick->LOAD=num;//重装载值
SysTick->VAL=0; //清空计数器
SysTick->CTRL=1;//开启计数
do
{
flag=SysTick->CTRL;
}
while(!(flag&(1<<16))&&(flag&0x01));
SysTick->CTRL=0;//关闭计数
SysTick->LOAD=0; // 清空
}
|
最佳答案
查看完整内容[请看2#楼]
回复【2楼】正点原子:
---------------------------------
谢谢原子哥,我找到问题了,是开始的时钟设置错误了(唉,3天都没发现这个错误),应该把RCC_APB2PeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
改为RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);这样它的标志位可以是SET了,但是另一个问题,我用DMA发送的数据是 u8 SendBuff=8 到串口数据寄存器,我用 data=(u32)&(USART1->DR);然后 ...
|