Hello everyone,
I am quite new to microcontroller so i need to know how can i send data from Stmf103 microcontroller using UART (using c). I just start learning about this so at 1st i just wanted to send a simple "hello world" using UART from Stmf103rbt6 microprocesssor to a server via wifi. Is there any library for this that i can use? if you give me a head start that would be really helpful.
Being non-specific about a board, uart and pins, I picked on for you. This uses the v3.5.0 Standard Peripheral Library.
// STM32 Keil printf USART2 (Tx PA.2, Rx PA.3) STM32F103RB NUCLEO - sourcer32@gmail.com #include "stm32f10x.h" #include <stdio.h> #include <stdlib.h> //**************************************************************************** void USART2_Configuration(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO and USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; 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 configuration */ USART_Init(USART2, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART2, ENABLE); } //**************************************************************************** void OutString(char *s) { while(*s) { while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, *s++); // Send Char } } //**************************************************************************** int main(void) { RCC_ClocksTypeDef RCC_Clocks; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ RCC_GetClocksFreq(&RCC_Clocks); USART2_Configuration(); OutString("Welcome to Nucleo F103RB\r\n"); printf("Hello World!\n"); printf("Running at %d MHz\n", RCC_Clocks.SYSCLK_Frequency / 1000000); while(1); // Don't want to exit } //**************************************************************************** // Hosting of stdio functionality through USART2 //**************************************************************************** #include <rt_misc.h> #pragma import(__use_no_semihosting_swi) struct __FILE { int handle; /* Add whatever you need here */ }; FILE __stdout; FILE __stdin; int fputc(int ch, FILE *f) { while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, ch); return(ch); } int fgetc(FILE *f) { char ch; while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); ch = USART_ReceiveData(USART2); return((int)ch); } int ferror(FILE *f) { /* Your implementation of ferror */ return EOF; } void _ttywrch(int ch) { while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, ch); } void _sys_exit(int return_code) { label: goto label; /* endless loop */ } //**************************************************************************** #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 //****************************************************************************
Hallo I am using the STM32F103R8T6, Want to transfer one character by UASRT. I write this code but I got this error which I can not solve can you help me how can I solve this problem . Error: Flash Download failed - Could not load file 'D:\STM32F10_Fixed\STM32F10\Project\Objects\stm32f103r8t6_.axf'
/* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f10x_usart.h"
void RCC_Configuration(void); void Delay(__IO uint32_t nCount); void GPIO_Configuration(void); void USART1_Configuration(void);
int main(void) { RCC_Configuration(); GPIO_Configuration(); USART1_Configuration(); ////////////////
//USART_Init();
/////////////////////USART_Init(USE_USART1);
while (1) {
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'X');
/* set pin */ GPIO_ResetBits(GPIOC, GPIO_Pin_4); ////////////////////// ooo //////// /* Insert delay */ Delay(1000);
/// USART_SendData(USART1, 'T'); ////// if ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)) ///////////////////////+ ////// { /////////////////////+ ////// GPIO_ResetBits(GPIOC, GPIO_Pin_4); ///////////////////////// A 1 ////////////// Delay(100); ////// } ////////////////////////////////////////////////////////////////// + ////// else //////////////////////////////////////////////////////////////// + ////// { ////////////////////////////////////////////////////////////// + GPIO_SetBits(GPIOC, GPIO_Pin_4); ///////////////////////////////////// Delay(1000); //////} ////////////////////////////////// +
}
/////////////////////////////////////////////////////////////////////////////////////
void USART1_Configuration(void) {
USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO and USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; 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 configuration */ USART_Init(USART1, &USART_InitStructure);
/* Enable USART */ USART_Cmd(USART1, ENABLE); }
////////////////////////////////////////////// void Delay(__IO uint32_t num) { __IO uint32_t index = 0;
/* default system clock is 72MHz */ for(index = (72000 * num); index != 0; index--) { } }
void RCC_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); ////////////////////////////A
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //////////////////////////////////////////// 1 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //////////////////////////////////// C
#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
/** * @} */
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
///////////////////////////////////////////