#include "stm32f2xx.h" #include <stdio.h> #define USART1_DR_ADDRESS ((uint32_t)USART1 + 0x04) #define DMA_Rec_Len 32 void RCC_Configuration(void); void NVIC_Configuration(void); void GPIO_Configuration(void); void USART_Configuration(void); void DMA_Configuration(void); void USART1_SendString(uint8_t *ch); void UART1_SendByte(uint16_t Data); void USART1_Send(u8 *buf,u8 len); uint8_t DMA_Rece_Buf[DMA_Rec_Len]; int16_t USART1_Rec_Cnt=0; int main(void) { //int16_t j; uint8_t i; RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); DMA_Configuration(); NVIC_Configuration(); while (1) { } void DMA_Configuration(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA2_Stream5); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DMA_Rece_Buf; DMA_InitStructure.DMA_BufferSize = DMA_Rec_Len; DMA_Init(DMA2_Stream5, &DMA_InitStructure); DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_Init(DMA2_Stream5, &DMA_InitStructure); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); DMA_Cmd(DMA2_Stream5, ENABLE); } //USART1 void USART1_IRQHandler(void) { u16 i; flag=1; if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)////½óêÕÖD¶Ï { DMA_Cmd(DMA2_Stream5, DISABLE);//1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET) { USART_ClearFlag(USART1,USART_FLAG_ORE); //Ïè¶áSR USART_ReceiveData(USART1); //Ôù¶áDR } USART1_Rec_Cnt= DMA_Rec_Len-DMA_GetCurrDataCounter(DMA2_Stream5); USART1_Send(DMA_Rece_Buf,USART1_Rec_Cnt); USART_ClearITPendingBit(USART1, USART_IT_IDLE|USART_IT_RXNE|USART_IT_TC); //USART_ITConfig(USART1, USART_IT_IDLE , ENABLE); /* Clear all DMA Streams flags */ DMA_ClearFlag(DMA2_Stream5, DMA_FLAG_HTIF2 | DMA_FLAG_TCIF2); DMA_SetCurrDataCounter(DMA2_Stream5,DMA_Rec_Len); DMA_Cmd(DMA2_Stream5, ENABLE); } } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /*Enable DMA Interrupt NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /*éèÖÃê±Öó*/ void RCC_Configuration(void) { RCC_DeInit(); RCC_HSICmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET){} RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div4); RCC_PCLK2Config(RCC_HCLK_Div2); FLASH_SetLatency(FLASH_Latency_7) ; FLASH_PrefetchBufferCmd(ENABLE); RCC_PLLConfig(RCC_PLLSource_HSI, 16, 240, 2, 5); RCC_PLLCmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); } /*GPIOéèÖÃ*/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART1); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; //éÏà-êäèë GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; GPIO_Init(GPIOC, &GPIO_InitStructure); } /*USARTéèÖÃ*/ void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; 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_ClearFlag(USART1, USART_FLAG_TC); USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_IDLE , ENABLE); //USART_ITConfig(USART1, USART_IT_RXNE , ENABLE); USART_Cmd(USART1, ENABLE); } void USART1_Send(u8 *buf,u8 len) { u8 t; for(t=0;t<len;t++) //Ñ-»··¢Ëíêy¾Y { while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); USART_SendData(USART1,buf[t]); } while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); } void USART1_SendString(uint8_t *ch) { while(*ch!=0) { while(!USART_GetFlagStatus(USART1, USART_FLAG_TXE)); USART_SendData(USART1, *ch); ch++; } } 收到的数据与发送的相差太大,求大神帮帮忙啊 |
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
以上大小更改为byte,
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
更改为Normal试试看。
另:建议不要再接收中断中立即发送数据!!!
祝你好运...
评分
查看全部评分
评分
查看全部评分