PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : [سوال] راه اندازی usartدر stm32f103RET



Raha323
15-03-2015, 10:04
با سلام دوستان من این کدها رو نوشتم ولی سریال میکروم دیتایی ارسال نمیکنه البته تازه کار هستم؛امکانش هست اشکال این پروژه رو بهم بگید و اگر میخوام چیزی چاپ در ترمینال سریال در کجا باید بنویسم و چیزی که نوشتم صحیحه با سپاس

#include "stm32f10x.h"

#include "RTC_Time.h"
#include <stdio.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__ */


/* Private function prototypes -----------------------------------------------*/
void GPIO_Configuration(void);
void USART_Configuration(void);


/************************************************** *****************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}


int main(void)
{
GPIO_Configuration();
USART_Configuration();
// printf("\r\n");
//printf("************************************************** *************\r\n");
//printf("* *\r\n");
//printf("* hi *\r\n");
//printf("* *\r\n");
//printf("************************************************** *************\r\n");
/* LED1-ON LED2-ON */
GPIO_SetBits(GPIOA, GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
// GPIO_ResetBits(GPIOA , GPIO_Pin_2);
//GPIO_SetBits(GPIOB , GPIO_Pin_1);


//RTC_Init();


/* Infinite loop */
while (1){
USART_SendData(USART1,0x02);
// printf("* Thank you *\r\n");
GPIO_ResetBits(GPIOA , GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);

GPIO_SetBits(GPIOA , GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);

}
}


/************************************************** *****************************
* Function Name : GPIO_Configuration
* Description : Configure GPIO Pin
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
/**
* LED1 -> PB0 LED2 -> PB1
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;//| GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}


/************************************************** *****************************
* Function Name : USART_Configuration
* Description : Configure USART1
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;


RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
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_10MHz;
GPIO_Init(GPIOA, &GPIO_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;


USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
}


/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
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;
}


#ifdef USE_FULL_ASSERT


/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


/* Infinite loop */
while (1)
{
}
}
#endif


/************************************************** ************************************************** *****
END FILE
************************************************** ************************************************** *****/

Raha323
16-03-2015, 09:37
پاسخی برای این پست نیست؟

mstkmn
16-03-2015, 10:30
سلام
1. اول سعی کن کدت رو توی تگ قرار بدی تا بشه خوند.
2. برای فعال کردن usart باید کلاک مربوط به قسمت afio و usart فعال بشه.
3. میتونی توی مد دیباگ ببینی که اصلا usart فعال شده یا نه.

Raha323
16-03-2015, 11:13
خوب بلد نیستم اینارو ممنون میشم بهم یاد بدید

mstkmn
16-03-2015, 11:23
خوب بلد نیستم اینارو ممنون میشم بهم یاد بدید
الان منظورتون کدوم قسمتش بود؟

Raha323
16-03-2015, 11:34
برای فعال کردن usart باید کلاک مربوط به قسمت afio و usart فعال بشه

mstkmn
16-03-2015, 11:37
rcc->apb2enr|=rcc_apb2enr_afioen
rcc->apb2enr|=rcc_apb2enr_usart1en

Raha323
16-03-2015, 12:32
اینارو کجا بنویسم؟

- - - Updated - - -

تو کیل من مینویسم

mstkmn
16-03-2015, 13:52
این قسمت ها باید قبل از انجام تنظیمات مربوط به USART فعال بشن.
البته شاید کتابخونه های CMSIS خودش این کار رو انجام بده.
به هر حال شما میتونی این دستورها رو قبل از فراخونی تابع USART_Configuration قرار بدی.