这是usart.c和usart.h代码:- #include "usart.h"
- /**
- * @brief 配置嵌套向量中断控制器NVIC
- * @param 无
- * @retval 无
- */
- static void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* 嵌套向量中断控制器组选择 */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /* 配置USART为中断源 */
- NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
- /* 抢断优先级*/
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- /* 子优先级 */
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- /* 使能中断 */
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- /* 初始化配置NVIC */
- NVIC_Init(&NVIC_InitStructure);
- }
- /**
- * @brief USART GPIO 配置,工作参数配置
- * @param 无
- * @retval 无
- */
- void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- // 打开串口GPIO的时钟
- DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
-
- // 打开串口外设的时钟
- DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
- // 将USART Tx的GPIO配置为推挽复用模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
- // 将USART Rx的GPIO配置为浮空输入模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
-
- // 配置串口的工作参数
- // 配置波特率
- USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
- // 配置 针数据字长
- 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(DEBUG_USARTx, &USART_InitStructure);
-
- // 串口中断优先级配置
- NVIC_Configuration();
-
- // 使能串口接收中断
- USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);
-
- // 使能串口
- USART_Cmd(DEBUG_USARTx, ENABLE);
- }
- /***************** 发送一个字节 **********************/
- void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
- {
- /* 发送一个字节数据到USART */
- USART_SendData(pUSARTx,ch);
-
- /* 等待发送数据寄存器为空 */
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- /****************** 发送8位的数组 ************************/
- void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
- {
- uint8_t i;
-
- for(i=0; i<num; i++)
- {
- /* 发送一个字节数据到USART */
- Usart_SendByte(pUSARTx,array[i]);
-
- }
- /* 等待发送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
- }
- /***************** 发送字符串 **********************/
- void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
- {
- unsigned int k=0;
- do
- {
- Usart_SendByte( pUSARTx, *(str + k) );
- k++;
- } while(*(str + k)!='\0');
-
- /* 等待发送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
- {}
- }
- /***************** 发送一个16位数 **********************/
- void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
- {
- uint8_t temp_h, temp_l;
-
- /* 取出高八位 */
- temp_h = (ch&0XFF00)>>8;
- /* 取出低八位 */
- temp_l = ch&0XFF;
-
- /* 发送高八位 */
- USART_SendData(pUSARTx,temp_h);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
-
- /* 发送低八位 */
- USART_SendData(pUSARTx,temp_l);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- ///重定向c库函数printf到串口,重定向后可使用printf函数
- int fputc(int ch, FILE *f)
- {
- /* 发送一个字节数据到串口 */
- USART_SendData(DEBUG_USARTx, (uint8_t) ch);
-
- /* 等待发送完毕 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
-
- return (ch);
- }
- ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
- int fgetc(FILE *f)
- {
- /* 等待串口输入数据 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(DEBUG_USARTx);
- }
复制代码 这是我移植usart.c文件遇到的错误编译提示:
Build target 'LED'
compiling usart.c...
..\USER\usart.c(35): error: #20: identifier "USART_InitTypeDef" is undefined
USART_InitTypeDef USART_InitStructure;
..\USER\usart.c(58): error: #20: identifier "USART_WordLength_8b" is undefined
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
..\USER\usart.c(60): error: #20: identifier "USART_StopBits_1" is undefined
USART_InitStructure.USART_StopBits = USART_StopBits_1;
..\USER\usart.c(62): error: #20: identifier "USART_Parity_No" is undefined
USART_InitStructure.USART_Parity = USART_Parity_No ;
..\USER\usart.c(65): error: #20: identifier "USART_HardwareFlowControl_None" is undefined
USART_HardwareFlowControl_None;
..\USER\usart.c(67): error: #20: identifier "USART_Mode_Rx" is undefined
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
..\USER\usart.c(67): error: #20: identifier "USART_Mode_Tx" is undefined
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
..\USER\usart.c(69): warning: #223-D: function "USART_Init" declared implicitly
USART_Init(DEBUG_USARTx, &USART_InitStructure);
..\USER\usart.c(75): warning: #223-D: function "USART_ITConfig" declared implicitly
USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);
..\USER\usart.c(75): error: #20: identifier "USART_IT_RXNE" is undefined
USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);
..\USER\usart.c(78): warning: #223-D: function "USART_Cmd" declared implicitly
USART_Cmd(DEBUG_USARTx, ENABLE);
..\USER\usart.c(85): warning: #223-D: function "USART_SendData" declared implicitly
USART_SendData(pUSARTx,ch);
..\USER\usart.c(88): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(88): error: #20: identifier "USART_FLAG_TXE" is undefined
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(103): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
..\USER\usart.c(103): error: #20: identifier "USART_FLAG_TC" is undefined
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
..\USER\usart.c(117): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
..\USER\usart.c(117): error: #20: identifier "USART_FLAG_TC" is undefined
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
..\USER\usart.c(132): warning: #223-D: function "USART_SendData" declared implicitly
USART_SendData(pUSARTx,temp_h);
..\USER\usart.c(133): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(133): error: #20: identifier "USART_FLAG_TXE" is undefined
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(144): warning: #223-D: function "USART_SendData" declared implicitly
USART_SendData(DEBUG_USARTx, (uint8_t) ch);
..\USER\usart.c(147): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(147): error: #20: identifier "USART_FLAG_TXE" is undefined
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
..\USER\usart.c(156): warning: #223-D: function "USART_GetFlagStatus" declared implicitly
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
..\USER\usart.c(156): error: #20: identifier "USART_FLAG_RXNE" is undefined
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
..\USER\usart.c(158): warning: #223-D: function "USART_ReceiveData" declared implicitly
return (int)USART_ReceiveData(DEBUG_USARTx);
..\USER\usart.c: 13 warnings, 14 errors
".\Obj\Project.axf" - 14 Error(s), 13 Warning(s).
Target not created.
Build Time Elapsed: 00:00:00
|