/** ******************************** STM32F10x ********************************* * @文件名 : usart.c * @作者 : sun * @库版本 : V3.5.0 * @文件版本 : V1.0.0 * @日期 : 2016年05月09日 * @摘要 : USART源文件 ******************************************************************************/ /*---------------------------------------------------------------------------- 更新日志: 2016-05-09 V1.0.0:初始版本 ----------------------------------------------------------------------------*/ /* 包含的头文件 --------------------------------------------------------------*/ #include "usart.h" #include "led.h" #include "systick.h" #include /********************uart1****************************/ volatile uint8_t m_state_code=M_STATE_IDLE; uint32_t usart2_cmd_time_out=0; uint32_t usart2_cmd_time_counter=0; #if 1 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; }; FILE __stdout; //定义_sys_exit()以避免使用半主机模式 void _sys_exit(int x) { x = x; } //重定义fputc函数 int fputc(int ch, FILE *f) { uint16_t temp=1000; while((temp>0)&((USART1->SR&0X40)==0)) { temp--; };//循环发送,直到发送完毕 //while((USART2->SR & USART_FLAG_TXE) == RESET); USART1->DR = (u8) ch; return ch; } #endif /************************************************ 函数名称 : USART1_GPIO_Configuration 功 能 : USART所使用管脚输出输入定义 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART1_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* 定义 USART1-TX 引脚为复用输出 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //IO口的第A9脚 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口 /* 定义 USART1-Rx 引脚为悬空输入 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第A10脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口 } /************************************************ 函数名称 : USART_Configuration 功 能 : 配置USART 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; /****************************************************************** USART参数初始化: 波特率 传输位数 停止位数 校验位数 115200 8 1 0(NO) *******************************************************************/ USART_InitStructure.USART_BaudRate = USART1_BAUDRATE; //设定传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数 USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能 USART_Init(USART1, &USART_InitStructure); //初始化USART1 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能USART1接收中断 USART_Cmd(USART1, ENABLE); //使能USART1 } /************************************************ 函数名称 : USART1_GPIO_Configuration 功 能 : USART所使用管脚输出输入定义 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART2_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* 定义 USART1-TX 引脚为复用输出 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第A9脚 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口 /* 定义 USART1-Rx 引脚为悬空输入 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //IO口的第A10脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IPU GPIO_Mode_IN_FLOATING; //IO口悬空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口 } /************************************************ 函数名称 : USART_Configuration 功 能 : 配置USART 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART2_Configuration(void) { USART_InitTypeDef USART_InitStructure; /****************************************************************** USART参数初始化: 波特率 传输位数 停止位数 校验位数 115200 8 1 0(NO) *******************************************************************/ USART_InitStructure.USART_BaudRate = USART2_BAUDRATE; //设定传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数 USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能 USART_Init(USART2, &USART_InitStructure); //初始化USART1 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能USART1接收中断 USART_ClearFlag(USART2, USART_FLAG_TC); USART_Cmd(USART2, ENABLE); //使能USART1 } /************************************************ 函数名称 : USART1_GPIO_Configuration 功 能 : USART所使用管脚输出输入定义 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART4_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* 定义 USART1-TX 引脚为复用输出 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第C10脚 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口 /* 定义 USART1-Rx 引脚为悬空输入 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //IO口的第C11脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入 GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输入IO口 } /************************************************ 函数名称 : USART_Configuration 功 能 : 配置USART 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART4_Configuration(void) { USART_InitTypeDef USART_InitStructure; /****************************************************************** USART参数初始化: 波特率 传输位数 停止位数 校验位数 115200 8 1 0(NO) *******************************************************************/ USART_InitStructure.USART_BaudRate = UART4_BAUDRATE; //设定传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数 USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数 USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能 USART_Init(UART4, &USART_InitStructure); //初始化UART4 USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能UART4接收中断 USART_Cmd(UART4, ENABLE); //使能UART4 } /************************************************ 函数名称 : USART1_GPIO_Configuration 功 能 : USART所使用管脚输出输入定义 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART5_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); /* 定义 USART1-TX 引脚为复用输出 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //IO口的第PC12脚 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口 /* 定义 USART1-Rx 引脚为悬空输入 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第PD2脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入 GPIO_Init(GPIOD, &GPIO_InitStructure); //USART输入IO口 } /************************************************ 函数名称 : USART_Configuration 功 能 : 配置USART 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART5_Configuration(void) { USART_InitTypeDef USART_InitStructure; /****************************************************************** USART参数初始化: 波特率 传输位数 停止位数 校验位数 115200 8 1 0(NO) *******************************************************************/ USART_InitStructure.USART_BaudRate = UART5_BAUDRATE; //设定传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数 USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数 USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能 USART_Init(UART5, &USART_InitStructure); //初始化USART5 USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //使能USART5接收中断 USART_Cmd(UART5, ENABLE); //使能USART5 } /************************************************ 函数名称 : USART_Initializes 功 能 : 串口初始化 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void USART_Initializes(void) { USART1_GPIO_Configuration(); USART1_Configuration(); } /************************************************ 函数名称 : USART1_SendChar 功 能 : 串口1发送一个字符 参 数 : Data --- 数据 返 回 值 : 无 作 者 : sun *************************************************/ void USART1_SendByte(uint8_t Data) { while((USART1->SR & USART_FLAG_TXE) == RESET); USART1->DR = (Data & (uint16_t)0x01FF); } /************************************************ 函数名称 : USART1_SendNByte 功 能 : 串口1发送N个字符 参 数 : pData ----- 字符串 Length --- 长度 返 回 值 : 无 作 者 : sun *************************************************/ void USART1_SendNByte(uint8_t *pData, uint16_t Length) { while(Length--) { USART1_SendByte(*pData); pData++; } } /************************************************ 函数名称 : USART1_Printf 功 能 : 串口1打印输出 参 数 : string --- 字符串 返 回 值 : 无 作 者 : sun *************************************************/ void USART1_Printf(uint8_t *String) { while((*String) != '\0') { USART1_SendByte(*String); String++; } } /************************************************ 函数名称 : USART1_SendChar 功 能 : 串口1发送一个字符 参 数 : Data --- 数据 返 回 值 : 无 作 者 : sun *************************************************/ void USART2_SendByte(uint8_t Data) { while((USART2->SR & USART_FLAG_TXE) == RESET); USART2->DR = (Data & (uint16_t)0x01FF); } /************************************************ 函数名称 : USART1_SendNByte 功 能 : 串口1发送N个字符 参 数 : pData ----- 字符串 Length --- 长度 返 回 值 : 无 作 者 : sun *************************************************/ void USART2_SendNByte(uint8_t *pData, uint16_t Length) { while(Length--) { USART2_SendByte(*pData); pData++; } } /************************************************ 函数名称 : USART1_Printf 功 能 : 串口1打印输出 参 数 : string --- 字符串 返 回 值 : 无 作 者 : sun *************************************************/ void USART2_Printf(uint8_t *String) { while((*String) != '\0') { USART2_SendByte(*String); String++; } } /************************************************ 函数名称 : UART4_SendChar 功 能 : 串口4发送一个字符 参 数 : Data --- 数据 返 回 值 : 无 作 者 : sun *************************************************/ void UART4_SendByte(uint8_t Data) { while((UART4->SR & USART_FLAG_TXE) == RESET); UART4->DR = (Data & (uint16_t)0x01FF); } /************************************************ 函数名称 : USART4_SendNByte 功 能 : 串口4发送N个字符 参 数 : pData ----- 字符串 Length --- 长度 返 回 值 : 无 作 者 : sun *************************************************/ void UART4_SendNByte(uint8_t *pData, uint16_t Length) { while(Length--) { UART4_SendByte(*pData); pData++; } } /************************************************ 函数名称 : USART4_Printf 功 能 : 串口4打印输出 参 数 : string --- 字符串 返 回 值 : 无 作 者 : sun *************************************************/ void UART4_Printf(uint8_t *String) { while((*String) != '\0') { UART4_SendByte(*String); String++; } } /************************************************ 函数名称 : UART5_SendChar 功 能 : 串口5发送一个字符 参 数 : Data --- 数据 返 回 值 : 无 作 者 : sun *************************************************/ void UART5_SendByte(uint8_t Data) { while((UART5->SR & USART_FLAG_TXE) == RESET); UART5->DR = (Data & (uint16_t)0x01FF); } /************************************************ 函数名称 : USART4_SendNByte 功 能 : 串口5发送N个字符 参 数 : pData ----- 字符串 Length --- 长度 返 回 值 : 无 作 者 : sun *************************************************/ void UART5_SendNByte(uint8_t *pData, uint16_t Length) { while(Length--) { UART5_SendByte(*pData); pData++; } } /************************************************ 函数名称 : USART4_Printf 功 能 : 串口5打印输出 参 数 : string --- 字符串 返 回 值 : 无 作 者 : sun *************************************************/ void UART5_Printf(uint8_t *String) { while((*String) != '\0') { UART5_SendByte(*String); String++; } } void USART_printfHex(const uint8_t* SendChars,uint16_t len) { uint16_t i = 0; // uint8_t temp = 0; for(i=0;i= 'a' && ch <= 'z')?(ch-0x20):ch) /** * @brief ascii convert hex * @par param[in] *hex:hex data * @par param[in] *ascii:ascii data * @par param[in] asciiLen:length of ascii * @retval length */ uint8_t Ascii2Hex(uint8_t *hex, uint8_t *ascii, uint8_t asciiLen) { uint8_t i,ch,value; value = 0; for(i=0;i<(asciiLen>>1);i++) { ch = CHAR_TO_UPPER(ascii[i*2]); if(ch >= '0' && ch <= '9') { value = ch -'0'; } else if(ch >= 'A' && ch <= 'F') { value = ch - 'A' + 0x0A; } else { return i; } hex[i] = (value<<4); ch = CHAR_TO_UPPER(ascii[i*2+1]); if(ch >= '0' && ch <= '9') { value = ch -'0'; } else if(ch >= 'A' && ch <= 'F') { value = ch - 'A' + 0x0A; } else { return i; } hex[i] += value; } return i; } /** * @brief hex convert ascii * @par param[in] *ascii:ascii data * @par param[in] *hex:hex data * @par param[in] hexLen:length of hex * @retval length */ uint8_t Hex2Ascii(uint8_t *ascii, uint8_t *hex, uint8_t hexLen) { uint8_t i, value; for(i=0;i>4); if(value > 9) { value += 0x07; } ascii[2*i] = value+0x30; value = (hex[i]&0x0F); if(value > 9) { value += 0x07; } ascii[2*i+1] = value+0x30; } return hexLen*2; } /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/