PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : مشکل با printf در stm32f103re



mohsen-p
18-10-2012, 00:54
من یه کد نوشتم که یه پین ست و ریست میشه و سپس یه عبارت به USART ارسال میشه.مشکل اینه که این کد بدون دستور printf اجرا میشه. اما هنگامی که printf از حالت کامنت خارج میشه برنامه دیگه اجرا نمیشه (حتی پین هم ست نمیشه).کسی می دونه مشکل از کجاست؟در حالی که همین کد در سری rb مشکلی نداره.

#include <stm32f10x.h>
#include <stdio.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_usart.h>

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);

int i;
int main()
{
RCC_Configuration();
GPIO_Configuration();
GPIO_SetBits(GPIOB, GPIO_Pin_6); //Set LED_Power
USART_Configuration();
printf("USART3 Is OK\n\r");
while (1)
{
{
GPIO_SetBits(GPIOB, GPIO_Pin_7); //Set LED_Scan
for(i=1;i<100000;i++);
GPIO_ResetBits(GPIOB, GPIO_Pin_7); //Reset LED_Scan
for(i=1;i<100000;i++);
}
}
}

void RCC_Configuration(void) //Crystal=8Mhz
{
//RCC_DeInit(); // RESET to defult RCC
RCC_HSEConfig (RCC_HSE_ON); // High Speed External is ON
//RCC_HSICmd ( DISABLE ); // Low Speed Internal is OFF
RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_7); // 8MHz*1*7=56MHz
RCC_PLLCmd (ENABLE); // PLL is ENABLE
RCC_SYSCLKConfig ( RCC_SYSCLKSource_PLLCLK ); // SYSTEM_CLOCK get Frq as PLL
RCC_HCLKConfig ( RCC_SYSCLK_Div1 ); // SYSTEM_CLOCK*1
RCC_PCLK1Config ( RCC_HCLK_Div2 ); // Feq+APB2=28MHz
RCC_PCLK2Config ( RCC_HCLK_Div1 ); // Feq+APB2=56MHz
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE); // GPIOA is ENABLE
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE); // GPIOB is ENABLE
RCC_ADCCLKConfig ( RCC_PCLK2_Div4 );
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, ENABLE);// UART1 is ENABLE
// (RCC_APB1Periph_USART3, ENABLE);// UART3 is ENABLE
}
void GPIO_Configuration(void)
{
//LED (PinB.6 & PinB.7)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //LED_Power & LED_Scan
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void USART_Configuration(void)
{
// UART1 (1 8 1 n 230400 & Hardware flow control disabled(RTS & CTS signals)) ================================================== =========
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//USART_DeInit (USART1);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);

/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
return ch;
}

agape
18-10-2012, 09:29
فکر میکنم برای استفاد از فرمان printf باید توابع تطح پایین رو اضافه کنید . برای کامپایلر کیل باید ریتارجت رو به پروژتون اضافه کنید

mohsen-p
18-10-2012, 12:31
فکر میکنم برای استفاد از فرمان printf باید توابع تطح پایین رو اضافه کنید . برای کامپایلر کیل باید ریتارجت رو به پروژتون اضافه کنید
باید چی رو اضافه کنم؟

electeron_man
20-10-2012, 00:07
سلام
منم همین مشکل رو دارم

pooriya
20-10-2012, 00:48
با سلام
تو تابع GPIO_Configuration اینارو اضافه کنید:


/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
و قسمت USART_Configuration1 رو هم به این صورت اصلاح کنید:


/************************************************** *****************************
* Function Name : USART_Configuration
* Description : Configures the USART1.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void USART_Configuration1(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure);

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 USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
}

/************************************************** *****************************
* Function Name : fputc
* Description : Retargets the C library printf function to the USART.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch);

/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}

return ch;
}
همچنین تو اول برنامت،قبل از تابع اصلی برنامه ،تو قسمت معرفی توابع ،اینم اضافه کن:


int fputc(int ch, FILE *f);

agape
20-10-2012, 09:25
بله - آقای علیزاده درست می فرماینده و شما باید تابع

int fputc(int ch, FILE *f)
که درواقع لیست کامل توابع برای راه اندازی مجموعه دستورات STDIO در فایلی به نام Retarget هست که اگر به پروژتون اضافه کنید میتونید از فرمان های STDIO به صورت کامل استفاده کنید.
این فایل رو هم میتونید تو مسیر پروژه های خود کیل پیدا کنید.

mohsen-p
20-10-2012, 10:42
من نتونستم از راهنمایی های شما استفاده کنم.اگه ممکنه یه پروژه کامل برای stm32f103re که یک دیتا رو به usart می فرستده بزارید.

sadmonew
20-10-2012, 13:35
من نتونستم از راهنمایی های شما استفاده کنم.اگه ممکنه یه پروژه کامل برای stm32f103re که یک دیتا رو به usart می فرستده بزارید.
سلام مهندس
اينم يه مثال ساده براي كار با usart.