STM32有关USART的配置函数

来源:互联网 发布:达内java培训具体安排 编辑:程序博客网 时间:2024/05/16 12:59
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);    
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &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(USART1, &USART_InitStructure);

/* ʹÄÜ´®¿Ú1½ÓÊÕÖÐ¶Ï */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_Cmd(USART1, ENABLE);
}


/// ÅäÖÃ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 = USART1_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(USART1, (uint8_t) ch);

/* µÈ´ý·¢ËÍÍê±Ï */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

return (ch);
}


/// Öض¨Ïòc¿âº¯Êýscanfµ½USART1
int fgetc(FILE *f)
{
/* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);


return (int)USART_ReceiveData(USART1);
}

/*********************************************END OF FILE**********************/

/**
  * @brief  USART1 TX DMA ÅäÖã¬ÄÚ´æµ½ÍâÉè(USART1->DR)
  * @param  ÎÞ
  * @retval ÎÞ
  */
void USART1_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;

/*¿ªÆôDMAʱÖÓ*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);


/*ÉèÖÃDMAÔ´£º´®¿ÚÊý¾Ý¼Ä´æÆ÷µØÖ·*/
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;  


/*ÄÚ´æµØÖ·(Òª´«ÊäµÄ±äÁ¿µÄÖ¸Õë)*/
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;


/*·½Ïò£º´ÓÄÚ´æµ½ÍâÉè*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;


/*´«Êä´óСDMA_BufferSize=SENDBUFF_SIZE*/
DMA_InitStructure.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;


/*ÄÚ´æÊý¾Ýµ¥Î» 8bit*/
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 


/*DMAģʽ£º²»¶ÏÑ­»·*/
//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;


/*ÅäÖÃDMA1µÄ4ͨµÀ*/  
DMA_Init(DMA1_Channel4, &DMA_InitStructure);  

/*ʹÄÜDMA*/
DMA_Cmd (DMA1_Channel4,ENABLE);
//DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);  //ÅäÖÃDMA·¢ËÍÍê³Éºó²úÉúÖжÏ
}

0 0
原创粉丝点击