#include "
STM32f10x.h"
/*
开发板:原子mini开发板,f103的
实验内容:
1、usart通过DMA方式把数据 SendBuff 发送到外设(即usart数据寄存器),发送成功LED状态会取反
2、通过usart把通过DMA接收的数据data=SendBuff发送给串口调试助手
(程序没有出错,可以下载到板子上)
我的问题:不知道我对DMA方式有没有理解错误,
程序发送到串口的数据和DMA传输得到的不一样,
希望各位指点指点,多谢!!!
(如果能有程序再好不够了)
*/
void GPIO_int(void);
void usart_init(void);
void RCC_Configura
tion(void);
void NVIC_Configuration(void);
void DMA_init(void);
void MYDMA(void);//DMA1次发送
u8 SendBuff=9; //DMA要发送的数据
u8 data; //读取DMA发来的数据
DMA_InitTypeDef DMA_InitStructure;
int main(void)
{
RCC_Configuration();
GPIO_int();
usart_init();
NVIC_Configuration();
DMA_init();
while(1)
{
MYDMA();//把设定的数据SendBuff=9发送到串口
USART_SendData(USART1, data);//Data: 待发送的数据
}
}
void USART1_IRQHandler(void)//接收DMA数据中断
{
if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中断标志位
while(USART_GetITStatus(USART1, USART_IT_RXNE)==RESET)
{}
data=USART_ReceiveData(USART1);//读取DMA发来的数据
//USART_SendData(USART1, data);//Data: 待发送的数据
GPIOA->ODR^=(1<<8);//取反,中断发生LED取反
}
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
}
void usart_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
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_ITConfig(USART1, USART_IT_RXNE,ENABLE);//接收中断
USART_Cmd(USART1, ENABLE);//使能或者失能USART外设
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);//使能DMA功能
}
void GPIO_int()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA10
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-->PA.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);
}
void RCC_Configuration(void)//设置系统时钟
{
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_AHBPeriph_DMA1, ENABLE);
}
void DMA_init()
{
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 256;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode =DMA_Mode_Normal;
// DMA_InitStructure.DMA_Mode =DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
void MYDMA()
{
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 所指示的通道
if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
{
GPIOA->ODR^=(1<<8);//取反led
DMA_ClearFlag(DMA1_FLAG_TC4);
}
}
一周热门 更多>