2012-11-20 02:34 PM
Hello,
I looking for some example code for communication between my ST-DISCOVERY F0 board and PC (UART). Can someone help me with it? I´m novice.Thank you2012-11-20 08:35 PM
Something like this will probably work
/* USART2 PA.2 Tx, PA.3 Rx STM32F0-Discovery sourcer32@gmail.com */
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure USART2 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
while(1)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
USART_SendData(USART2, 'X');
}
}
2012-11-21 12:30 AM
And if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry.
2012-11-21 01:18 AM
''if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry''
Or use something like this for the connection to the PC:
It connects direct to the microcontroller's UART pins - be sure to choose the correct voltage version! Or this: Although it's described as a ''Raspberry Pi'' cable, it neither knows nor cares what you actually connect it to! This one just provides Rx, Tx, and Ground. 3.3V only.2013-01-31 04:14 PM
And for USART1, this should be workable for polling. Edit: fixed bug
/* USART1 PA.9 Tx, PA.10 Rx STM32F0-Discovery sourcer32@gmail.com */
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'X');
}
}
2013-02-16 05:27 AM
2013-02-16 06:41 AM
Looks like I missed a semi-colon in my wait of TXE loop.
You need to enable the interrupt on the USART USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);2013-03-08 03:50 AM
Hello,
I have written the program for the Usart2 and i want to see the result on putty terminal but i am not able to see any kind of data on the putty terminal here is my source code int main (void){ USART_InitTypeDef USART_InitStructure; RCC_Configurartion(); GPIO_Configurartion(); USART_InitStructure.USART_BaudRate = 115200;/*set the baud rate to 115200 */ USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*set the data bit 8 */ USART_InitStructure.USART_StopBits = USART_StopBits_1;/*set the stop bit 1 */ USART_InitStructure.USART_Parity = USART_Parity_No;/*no parity */ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*No hardware flow control */ USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx;/*send and receive */ /*Configure the serial port */ USART_Init (USART1, &USART_InitStructure); /*Configure the serial port */ USART_Init (USART2, &USART_InitStructure); /*Enable serial port 1 */ USART_Cmd (USART1, ENABLE); /*Enable the serial port */ USART_Cmd (USART2, ENABLE); /*Wait until end of transmission from USART1 to USART2 */ while (1) { USART_SendData (USART2, '1'); while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET); USART_SendData (USART2, '5'); while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET);// if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE)== SET) USART_SendData (USART2, USART_ReceiveData (USART1)); while (USART_GetFlagStatus (USART1, USART_FLAG_TC)== RESET); Delay (0XFFFFF); }}void GPIO_Configurartion(void){ GPIO_InitTypeDef GPIO_InitStructure;//////Configure USART1 Rx as input Floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);//////Configure USART1 Tx as alternet function push pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);//////Configure USART2 Rx as input Floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure);//////Configure USART2 Tx as alternet function push pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure);}void RCC_Configurartion(void){RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);}2013-03-08 06:28 AM
Tags are great and all, but perhaps you should specify what part you're using, and start a new thread if it doesn't relate to this thread's topic??
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // << APB2 SURELY?2013-03-13 09:49 AM
Have someone working program for demonstrating USART with interrupts for F0?