本帖最后由 诡异之云 于 2014-10-30 16:00 编辑
STM32使用整理(一):Usart打印功能 最近在调试串口,需要用到打印功能,查找很多网上的资料,主要有两种方法。 l 第一种:使用的是MDK,请在工程属性的“Target“-》”Code Generation“中勾选“Use MicroLIB”。然后重写int fgetc(FILE *f)、int fputc(int ch, FILE *f)这两个函数。此种方法我未调通。 l 第二种:自己重写一个打印函数。综合网上找到的资料,我整理、调试出几个可用的函数,与大家分享。 一、具体实现: 1、 串口的初始化 此处为串口的初始化函数,主要是对串口1、2进行初始化,没什么可说的,直接粘代码。 /* ******************************************************************************* * Usart_Hwinit * Description : 串口初始化 * Arguments : none * Returns : 0: if succeed -1: if failed * Notes : none ******************************************************************************* */ int8 Usart_Hwinit(void) { GPIO_InitTypeDef GPIO_InitStructure; 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;
/* Enable GPIO clock */ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA,ENABLE); /* Enable UART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //usart1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //usart1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //usart2 GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //usart2
/*********************USART1*******************************/ /* Configure USART Tx as alternate function */ 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 = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */ USART_Init(USART1, &USART_InitStructure);
/* Enable USART */ USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/**************************USART2**************************/ 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;
/* Configure USART Tx as alternate function */ 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 = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */ USART_Init(USART2, &USART_InitStructure);
/* Enable USART */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
return 0; } 2、 Usart_Send_Byte函数实现 /* ******************************************************************************* * Usart_Send_Byte * Description : 发送一个字节 * Arguments : USARTx 选择发送使用的串口 * byte 要发送的字节 * Returns : none * Notes : none ******************************************************************************* */ void Usart_Send_Byte(USART_TypeDef* USARTx, uint8 byte) { USART_SendData(USARTx,byte); while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET) ; } 3、 Usart_Send_Str函数实现 /* ******************************************************************************* * Usart_Send_Str * Description : 发送一个字符串 * Arguments : USARTx 选择发送使用的串口 * s 要发送的字符串 * Returns : none * Notes : none ******************************************************************************* */ void Usart_Send_Str(USART_TypeDef* USARTx,uint8 *s) { uint8i = 0; while(s!= '\0') { USART_SendData(USARTx,s); while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET) ; i++; } } 4、 Usart_Send_Array函数实现 /* ******************************************************************************* * Usart_Send_Array * Description : 发送一个数组 * Arguments : USARTx选择发送使用的串口 * send_array[]要发送的数组名称 * num发送数组的大小 * Returns : none * Notes : none ******************************************************************************* */ void UART1_Send_Array(USART_TypeDef*USARTx,uint8 send_array[],uint8 num) { uint8 i=0; while(i<num) { USART_SendData(USARTx,send_array); while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!= SET); i++; } } 5、 Usart_Send函数实现 /* ******************************************************************************* * Usart_Send * Description : 采用阻塞方式通过串口将数据发送 * Arguments : pack 要发送的字符串 * length要发送的字符串长度 * com 选择的串口 * Returns : none * Notes : com 0 USART1 1 USART2 2 USART3 3 USART4 4 USART5 ********************************************************************************/ void Usart_Send( uint8* pack,uint32length,uint8 com ) { uint32 i=0; if(com>4) return; switch( com ) { case 0: USART_ClearITPendingBit(USART1,USART_IT_TC);//清除发送中断标志 while( length-- ) { USART_SendData(USART1,(uint8)pack[i++]); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET); } break;
case 1: USART_ClearITPendingBit(USART2,USART_IT_TC);//清除发送中断标志 while( length-- ) { USART_SendData(USART2,pack[i++]); while(USART_GetFlagStatus(USART2, USART_FLAG_TC)== RESET); } break;
default: break; } return; } 6、 Print函数实现 /* ******************************************************************************* * Print * Description : 通过串口打印函数 * Arguments : string要打印的字符串 * len要发送的字符串长度 * Returns : none * Notes : none ******************************************************************************* */ void Print(uint8*string, uint32 len) { uint8*ps=(uint8*)string; if(len<1) return; #ifdef DEBUG Usart_Send(ps,len,0); //主要是调用Usart_Send函数来实现 #endif return; } 二、实验结果 测试代码截图:![]()
测试结果截图:![]()
三、总结: 1、几个函数的总体实现都是使用USART_SendData这个库函数,代码上很简单。 2、在调试过程中,发现可以打印,但是结果和预期不一样,这时候需要检查时钟配置。
![]() 3、关于这个串口使用的MDK工程我已经上传到CSDN上,附上链接地址: http://download.csdn.net/detail/guzhuliuyun/6849183。
|