初级会员
- 积分
- 76
- 金钱
- 76
- 注册时间
- 2014-2-22
- 在线时间
- 0 小时
|
发表于 2014-5-1 21:22:32
|
显示全部楼层
原子哥,可以帮我看一下程序!通过usart2的dma接收gps信息,在通过usart1发送到电脑上!但这段程序,dma不产生中断?我估计是gps的发送时间,或者dma的接收长度有问题!但还是查不出来!
#include "stm32f10x.h"
#include "misc.h"
#include <stdio.h>
#include <stdbool.h>
#include "stm32f10x_usart.h"
#define dma_len 100
u8 USART2_DMA_Buf1[dma_len];
u8 USART2_DMA_Buf2[dma_len];
typedef enum {BUF_NO1=0,BUF_NO2=1}BUF_NO;
BUF_NO Free_Buf_No;
bool Buf_Ok;
DMA_InitTypeDef DMA_InitStructure;
void DMA1_Channel7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC7))
{
DMA_ClearITPendingBit(DMA1_IT_GL7);
GPIO_SetBits(GPIOB,GPIO_Pin_10);
if(Free_Buf_No==BUF_NO1)
{
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)USART2_DMA_Buf1;
DMA_Init(DMA1_Channel7,&DMA_InitStructure);
Free_Buf_No=BUF_NO2;
}
else
{
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)USART2_DMA_Buf2;
DMA_Init(DMA1_Channel7,&DMA_InitStructure);
Free_Buf_No=BUF_NO1;
}
Buf_Ok=true;
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
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_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=38400;
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(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel =DMA1_Channel7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate =38400;
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);
}
int fputc(int ch,FILE *f)
{
if(ch=='\n')
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,'\r');
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,ch);
return ch;
}
void DMA_Configuration(void)
{
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&USART2->DR ;
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)USART2_DMA_Buf1;
DMA_InitStructure.DMA_DIR =DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize =10;
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_High;
DMA_InitStructure.DMA_M2M =DMA_M2M_Disable;
DMA_Init(DMA1_Channel7,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel7,DMA_IT_TC,ENABLE);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
Free_Buf_No=BUF_NO2;
Buf_Ok=false;
DMA_Cmd(DMA1_Channel7,ENABLE);
}
int USART2_Init()
{
USART2_Configuration();
NVIC_Configuration();
USART_ClearFlag(USART1,USART_FLAG_TC);
DMA_Configuration();
return 0;
}
int main(void)
{
u16 x1=0,x2=0;
RCC_Configuration();
GPIO_Configuration();
USART_Config();
while(USART2_Init())
{
printf("???????§°???");
}
while(1)
{
if(Buf_Ok==true)
{
Buf_Ok=false;
x1=0;
x2=0;
if(Free_Buf_No==BUF_NO1)
{
while(x1<dma_len)
{
USART_SendData(USART1,USART2_DMA_Buf1[x1++]);
}
}
else
{
while(x2><dma_len)
{
USART_SendData(USART1,USART2_DMA_Buf2[x2++]);
}
}
}
}
}> |
|