历史上的今天
今天是:2025年01月29日(星期三)
2019年01月29日 | STM32L151C8T6的串口配置
2019-01-29 来源:eefocus
**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);
}**
史海拾趣
|
做芯片这行好几年,各种技术会议也参加了不少,只是飞思卡尔的技术论坛还没有去过。一直想参加,毕竟属于行业的老大啊!可是哪有那么容易! 今年的飞思卡尔大学生智能车赛开办了一个博客大赛,参与投票的工程师就有机会参加技术论坛。赶紧去投 ...… 查看全部问答> |
|
【经典提问】 有没有办法去掉UART的FIFO,或者怎样才能实现每收发一个字符就中断一次呢?Stellaris系列ARM的UART好像无法做到这一点,存在缺陷! 【精妙解答】 误解了,根本就不是缺陷,这恰恰是优点! 看来许多人还没有真正理解FI ...… 查看全部问答> |
|
#define bwMCDR2_ADDRESS 4 #define bsMCDR2_ADDRESS 17 #define bmMCDR2_ADDRESS BIT_MASK(MCDR2_ADDRESS) #define BIT_MASK(__bf) (((1U … 查看全部问答> |
|
高压电线的巡检机器人,主要并行执行三个任务: 1.在电线上自主行走; 2.不断的摄像 3.并把摄像的数据传给地面的基站. 用多进程,还是多线程比较合适?有以下三种意见: (1)用三个线程; (2)第一个任务,作为一个进程.第二和第三个任务作为两个线程放 ...… 查看全部问答> |
|
在八零后的集体记忆里,玻璃珠拍纸片、一款四驱车就征服了一代人。这辆高科技的涂鸦轨道车比四驱车更加妙不可言。它不像其他的远程遥控小车,必须放在固定的玩具轨道里或者用遥控器控制。只要用一个黑色记号笔画出你想要的路途,小车里的光学感应器 ...… 查看全部问答> |




