**STM32L151C8T6的串口配置
//////////////////////////////////////////////////////////////////
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
uint16_t CNT=0;
while((USART1->SR&0X40)==0)//循环发送,直到发送完毕
{
if((CNT++)>60000)//防止异常超时退出
{
break;
}
}
USART1->DR = (u8) ch;
return ch;
}
#endif
/**
* @brief Uart Init program
* @param1 USARTx:USART1 USARTUSART2 USART3
* @param2 bound: USARTx Baud Rate
* @retval None
*/
void My_USARTx_Config(USART_TypeDef* USARTx,uint32_t bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
My_Struct_Clear(USART_InitStructure);
My_Struct_Clear(NVIC_InitStructure);
/* Enable the USARTx Pins Software Remapping */
if(USARTx == USART1)
{
My_Struct_Clear(UART1_Parameter);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
else if(USARTx == USART2)
{
My_Struct_Clear(UART2_Parameter);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
else if(USARTx == USART3)
{
My_Struct_Clear(UART3_Parameter);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
}
if(USARTx == USART1)
{
/* Configure USART1 Rx (PA.10) as input floating */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
else if(USARTx == USART2)
{
/* Configure USART2 Rx (PA.03) as input floating */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
else if(USARTx == USART3)
{
/* Configure USART3 Rx (PB.11) as input floating */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
My_Struct_Clear(GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Enable the USARTx Interrupt */
if(USARTx == USART1)
{
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
}
else if(USARTx == USART2)
{
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
}
else if(USARTx == USART3)
{
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
}
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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(USARTx, &USART_InitStructure);
/* Enable USARTx */
USART_Cmd(USARTx, ENABLE);
USART_ITConfig(USARTx, USART_IT_RXNE,DISABLE);
USART_ITConfig(USARTx, USART_IT_TXE,DISABLE);
if(USARTx == USART1)
{
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
}
else if(USARTx == USART2)
{
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
}
else if(USARTx == USART3)
{
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
}
}
/**
* @brief USRATx transmit transmit datas or string
* @param1 USARTx:USART1 USARTUSART2 USART3
* @param2 Str: Data Addr
* @retval None
*/
void USARTx_SendString(USART_TypeDef* USARTx,char * str)
{
uint16_t CNT=0;
while(*str != '\0')// && *str != '\n'
{
USARTx->DR = (u8) *str++;
while((USARTx->SR&0X40)==0)//循环发送,直到发送完毕
{
if((CNT++)>60000)
{
break;
}
}
}
}
/**
* @brief USRATx transmit transmit datas or string
* @param1 USARTx:USART1 USARTUSART2 USART3
* @param2 Str: Data Addr
* @param3 StrLength: Data Length
* @retval None
*/
void USARTx_printf(USART_TypeDef* USARTx,const char *fmt,...)
{
va_list ap;
char string[256];
My_Struct_Clear(string);
va_start(ap,fmt);
vsprintf(string,fmt,ap);//此处也可以使用sprintf函数,用法差不多,稍加修改即可,此处略去
USARTx_SendString(USARTx,string);
va_end(ap);
}**
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』