在移植野火串口列印工程的時候遇到這樣一個問題,野火工程不需要勾選Use MicroLIB,程式運行正常,列印也正常,
采用正點原子工程的編程方式,移植了之后,仿真環境下,單步運行,全速運行,程式沒問題,列印也沒問題,
但是開發板(野火開發板)上電單獨運行,程式卡死,
但是勾選了Use MicroLIB,程式運行合適了,
后面采用了支持printf函式,而不需要選擇use MicroLIB處理方式出現(就是usart.c這部分).\Objects\LEDApp.axf: Error: L6200E: Symbol __stdout multiply defined (by stdio_streams.o and usart.o).
在這個報錯下,勾選了Use MicroLIB,程式正常了。。。。。。。
下面直接上代碼請大神們幫忙分析一下:
#include "myusart.h"
void usart_init(void)
{
//GPIO設定
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //使能USART1,GPIOA時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA時鐘
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA.9
//USART1_RX GPIOA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA.10
//USART 初始化設定
USART_InitStructure.USART_BaudRate = 115200; //串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長為8位資料格式
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(USART1,&USART_InitStructure); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
}
/********************發送一個字符***********************/
void Usart_SendByte(USART_TypeDef * pUSARTx, uint8_t ch)
{
USART_SendData(pUSARTx, ch); //發送一個位元組資料到USART
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == 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(USART1, (uint8_t) ch); //發送一個位元組資料到串口
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET); //等待發送完成
return (ch);
}
/****重定向c庫函式scanf到串口,重寫向后可使用scanf,getchar等函式******/
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); //等待串口輸入資料
return (int)USART_ReceiveData(USART1);
}
#ifndef __MYUSART_H
#define __MYUSART_H
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
#endif
#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//如果使用ucos,則包括下面的頭檔案即可.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos 使用
#endif
//加入以下代碼,支持printf函式,而不需要選擇use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//標準庫需要的支持函式
struct __FILE
{
int handle;
};
FILE __stdout;
//定義_sys_exit()以避免使用半主機模式
_sys_exit(int x)
{
x = x;
}
/***************************增加下面這個函式*****************************/
//__use_no_semihosting was requested, but _ttywrch was
_ttywrch(int ch)
{
ch = ch;
}
/***************************增加上面這個函式*****************************/
#endif
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
#endif
#include "led.h"
#include "myusart.h"
#include "stm32f10x.h"
#include "usart.h"
static void Show_Message(void);
int main(void)
{
char ch;
/* 初始化RGB彩燈 */
LED_Init();
/* 初始化USART 配置模式為 115200 8-N-1 */
usart_init();
/* 列印指令輸入提示資訊 */
Show_Message();
while(1)
{
/* 獲取字符指令 */
ch=getchar();
printf ("接收到字符:%c\n",ch);
/* 根據字符指令控制RGB彩燈顏色 */
switch(ch)
{
case '1':
LED_RED;
break;
case '2':
LED_GREEN;
break;
case '3':
LED_BLUE;
break;
case '4':
LED_YELLOW;
break;
case '5':
LED_PURPLE;
break;
case '6':
LED_CYAN;
break;
case '7':
LED_WHITE;
break;
case '8':
LED_RGBOFF;
break;
default:
/* 如果不是指定指令字符,列印提示資訊 */
Show_Message();
break;
}
}
}
static void Show_Message(void)
{
printf("\r\n 這是一個通過串口通信指令控制RGB彩燈實驗 \n");
printf("使用 USART 引數為:%d 8-N-1 \n",115200);
printf("開發板接到指令后控制RGB彩燈顏色,指令對應如下:\n");
printf(" 指令 ------ 彩燈顏色 \n");
printf(" 1 ------ 紅 \n");
printf(" 2 ------ 綠 \n");
printf(" 3 ------ 藍 \n");
printf(" 4 ------ 黃 \n");
printf(" 5 ------ 紫 \n");
printf(" 6 ------ 青 \n");
printf(" 7 ------ 白 \n");
printf(" 8 ------ 滅 \n");
}
uj5u.com熱心網友回復:
在不使用半主機操作的時候,只有在仿真環境下,才會正常運行,列印,如圖:
,只有勾選了,Use MicroLIB,才會正常運行。
但是使用了半主機操作,.\Objects\LEDApp.axf: Error: L6200E: Symbol __stdout multiply defined (by stdio_streams.o and usart.o).報這個錯誤,同樣勾選了,Use MicroLIB,錯誤解決,程式,正常運行,列印,
這種操作,如何能正確執行哦,這樣仍然鏈接了使用半主機的函式,聯結器繼續報錯誤,不等于沒解決嗎,大佬們如何看
#pragma import(__use_no_semihosting) // 確保沒有從 C 庫鏈接使用半主機的函式
_sys_exit(int x) //定義 _sys_exit() 以避免使用半主機模式
{
x = x;
}
struct __FILE // 標準庫需要的支持函式
{
int handle;
};
/* FILE is typedef ’ d in stdio.h. */
FILE __stdout;
uj5u.com熱心網友回復:
還有野火此實驗例程,既沒有勾選Use MicroLIB,也沒有使用這種操作#pragma import(__use_no_semihosting) // 確保沒有從 C 庫鏈接使用半主機的函式_sys_exit(int x) //定義 _sys_exit() 以避免使用半主機模式
{
x = x;
}
struct __FILE // 標準庫需要的支持函式
{
int handle;
};
/* FILE is typedef ’ d in stdio.h. */
FILE __stdout;
但就是正常的,奇怪不
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/254176.html
標籤:單片機/工控
