初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-2-25
|
野火mini usart3 使用DMA 接收发送 接收数据不能接收到数据
/**
******************************************************************************
* @file main.c
* @author fire
* @version V1.0
* @date 2013-xx-xx
* @brief 串口中断接收测试
******************************************************************************
* @attention
*
* 实验平台:野火 iSO-MINI STM32 开发板
* 论坛 :http://www.chuxue123.com
* 淘宝 :http://firestm32.taobao.com
*
******************************************************************************
*/
#include "stm32f10x.h"
#include "bsp_usart1.h"
#define RS485_RE_Tx GPIO_SetBits(GPIOE, GPIO_Pin_15)
#define RS485_RE_Rx GPIO_ResetBits(GPIOE, GPIO_Pin_15)
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* USART1 配置模式为 115200 8-N-1,中断接收 */
USART1_Config();
USART3_DMA_Config();
//PE15 RE
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
NVIC_Configuration();
RS485_RE_Tx;
printf("\r\n 这是一个串口中断接收回显实验 \r\n");
printf("\r\n 请在超级终端或者串口调试助手输入字符 \r\n");
RS485_RE_Rx;
for(;;)
{
}
}
/*********************************************END OF FILE**********************/
/**
******************************************************************************
* @file bsp_usart1.c
* @author fire
* @version V1.0
* @date 2013-xx-xx
* @brief usart应用bsp
******************************************************************************
* @attention
*
* 实验平台:野火 iSO-MINI STM32 开发板
* 论坛 :http://www.chuxue123.com
* 淘宝 :http://firestm32.taobao.com
*
******************************************************************************
*/
#include "bsp_usart1.h"
/**
* @brief USART1 GPIO 配置,工作模式配置。9600 8-N-1
* @param 无
* @retval 无
*/
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART1 clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
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_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
/* 使能串口1接收中断 */
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
USART_Cmd(USART3, ENABLE);
}
#define USART3_DR_Base 0x40004804
u8 USART3_DMA_SendBuff[USART3_DMA_SendBuffSize]; //串口3 DMA发送数据内存地址
u8 USART3_DMA_ReceiveBuff[USART3_DMA_ReceiveBuffSize]; //串口3 DMA接收数据内存地址
/**
* @brief USART3 TX DMA 配置,内存到外设(USART1->DR)
* @param 无
* @retval 无
*/
void USART3_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /*开启DMA时钟*/
DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base; /*设置DMA源:串口数据寄存器地址*/
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART3_DMA_SendBuff; /*内存地址(要传输的变量的指针)*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /*方向:从内存到外设*/
DMA_InitStructure.DMA_BufferSize = USART3_DMA_SendBuffSize; /*传输大小DMA_BufferSize=SENDBUFF_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; /*内存数据单位 8bit*/
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; /*DMA模式:单次循环*/
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /*DMA模式:不断循环*/
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; /*优先级:中*/
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /*禁止内存到内存的传输 */
DMA_Init(DMA1_Channel2, &DMA_InitStructure); /*配置DMA1的2通道*/
DMA_Cmd (DMA1_Channel2,ENABLE); /*使能DMA*/
DMA_ITConfig(DMA1_Channel2,DMA_IT_TC,ENABLE); //配置DMA发送完成后产生中断
DMA_ClearFlag(DMA1_FLAG_TC2); // 清除标志
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART3_DMA_ReceiveBuff; /*内存地址(要传输的变量的指针)*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /*方向:从外设到内存*/
DMA_InitStructure.DMA_BufferSize = USART3_DMA_ReceiveBuffSize; /*传输大小DMA_BufferSize=SENDBUFF_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; /*内存数据单位 8bit*/
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /*DMA模式:不断循环*/
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; /*优先级:中*/
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /*禁止内存到内存的传输 */
DMA_Init(DMA1_Channel3, &DMA_InitStructure); /*配置DMA1的3通道*/
DMA_ClearFlag(DMA1_FLAG_GL3);
DMA_Cmd (DMA1_Channel3,ENABLE); /*使能DMA*/
}
/// 配置USART1接收中断
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/// 重定向c库函数printf到USART1
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到USART1 */
USART_SendData(USART3, (uint8_t) ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
return (ch);
}
/*********************************************END OF FILE**********************/
#ifndef __USART1_H
#define __USART1_H
#include "stm32f10x.h"
#include <stdio.h>
#define USART3_DMA_SendBuffSize 256
#define USART3_DMA_ReceiveBuffSize 512
void USART1_Config(void);
void NVIC_Configuration(void);
void USART3_DMA_Config(void);
int fputc(int ch, FILE *f);
int fgetc(FILE *f);
#endif /* __USART1_H */
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTI
AL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include <stdio.h>
/** @addtogroup STM32F10x_StdPeriph_Template
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
}
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f10x_xx.s). */
/******************************************************************************/
void USART3_IRQHandler(void)
{
uint8_t ch;
if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
{
//ch = USART1->DR;
USART_GetFlagStatus(USART3,USART_SR_IDLE);
ch = USART_ReceiveData(USART3);
USART_ClearITPendingBit(USART3, USART_IT_IDLE);
printf( "%c", ch ); //将接受到的数据直接返回打印
}
}
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
|
|