这是NVIC和串口DMA的配置..
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* ÅäÖÃÖD¶ÏÔ′ */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(DEBUG_USART_DMA_CLK, ENABLE);
DMA_DeInit(DEBUG_USART_DMA_STREAM);
while (DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != DISABLE);
/*usart2 Rx dma1,channel4,stream5*/
DMA_InitStructure.DMA_Channel = DEBUG_USART_DMA_CHANNEL;
DMA_InitStructure.DMA_PeripheralBaseAddr = DEBUG_USART_DR_BASE;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)RecvBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = BUFF_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;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// DMA_Mode_Circular; //
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DEBUG_USART_DMA_STREAM, &DMA_InitStructure);
//¿aÆôDMAêy¾Y·¢Ëííê3éÖD¶Ï
DMA_ITConfig(DEBUG_USART_DMA_STREAM,DMA_IT_TC,ENABLE);
//Çå±êÖ¾
DMA_ClearITPendingBit(DEBUG_USART_DMA_STREAM,DMA_IT_TCIF5);
//¿aÆô′®¿ú½óêÕÇëÇó
USART_DMACmd(RS232_USART, USART_DMAReq_Rx, ENABLE);
//¿aÆôDMA
DMA_Cmd(DEBUG_USART_DMA_STREAM, ENABLE);
/* μè′yDMAá÷óDD§*/
while(DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != ENABLE);
USART_Cmd(RS232_USART, ENABLE);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd( RS232_USART_RX_GPIO_CLK|RS232_USART_TX_GPIO_CLK, ENABLE);
/* ê1Äü USART2 ê±Öó */
RCC_APB1PeriphClockCmd(RS232_USART_CLK, ENABLE);
/* ᬽó PXx μ½ USARTx_Tx*/
GPIO_PinAFConfig(RS232_USART_RX_GPIO_PORT,RS232_USART_RX_SOURCE, RS232_USART_RX_AF);
/* ᬽó PXx μ½ USARTx__Rx*/
GPIO_PinAFConfig(RS232_USART_TX_GPIO_PORT,RS232_USART_TX_SOURCE,RS232_USART_TX_AF);
/* ÅäÖÃTxòy½ÅÎa¸′óÃ1|Äü */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = RS232_USART_TX_PIN ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RS232_USART_TX_GPIO_PORT, &GPIO_InitStructure);
/* ÅäÖÃRxòy½ÅÎa¸′óÃ1|Äü */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = RS232_USART_RX_PIN;
GPIO_Init(RS232_USART_RX_GPIO_PORT, &GPIO_InitStructure);
/* ÅäÖÃ′®¿úRS232_USART Ä£ê½ */
USART_InitStructure.USART_BaudRate = RS232_USART_BAUDRATE;
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(RS232_USART, &USART_InitStructure);
//USART_ITConfig(RS232_USART, USART_IT_RXNE, ENABLE);
}
///Öض¨Ïòc¿aoˉêyprintfμ½′®¿úRS232_USART£¬Öض¨Ïòoó¿éê1óÃprintfoˉêy
int fputc(int ch, FILE *f)
{
/* ·¢Ëíò»¸ö×Ö½úêy¾Yμ½′®¿úRS232_USART */
USART_SendData(RS232_USART, (uint8_t) ch);
/* μè′y·¢Ëííê±Ï */
while (USART_GetFlagStatus(RS232_USART, USART_FLAG_TXE) == RESET);
return (ch);
}
/*********************************************END OF FILE**********************/
这是中断函数,不晓得为什么一直进不去
extern uint8_t Rxflag;
void DMA1_Stream5_IRQHandler(void)
{
LED_WHITE;
if(DMA_GetITStatus(DMA1_Stream5,DMA_IT_TCIF5) == SET)
{
Rxflag=1;
LED1_TOGGLE;
DMA_ClearITPendingBit(DEBUG_USART_DMA_STREAM,DMA_IT_TCIF5);
}
}
这个是main.....
uint8_t Rxflag=0;
extern uint8_t RecvBuff[BUFF_SIZE];
int main(void)
{
uint16_t i=0;
uint8_t buff[BUFF_SIZE];
memset(RecvBuff,0,BUFF_SIZE);
LED_GPIO_Config();
NVIC_Configuration();
USART2_Config();
USART2_DMA_Config();
printf("ÕaêÇò»¸ö′®¿úDMA½óêÕêμÑé!
");
while(1)
{
if(Rxflag)
{
memset(buff,0,BUFF_SIZE);
memcpy(buff,RecvBuff,BUFF_SIZE);
for(i=0;(strlen(buff)!=0) && (buff!= 0x00);i++)
{
printf("%c",buff);
}
}
Rxflag=0;
}
}
哪位大哥能帮下小弟看下哪里的错误吗,我已经被折磨了2天了.......东西也不多可是为什么用电脑通过串口向F4发送数据就是不出结果呢....
一周热门 更多>