2013-02-16 09:02 AM
hi everyone,
i want to make a simple comunication from usart2 to usart3, i have shorted usart2_tx (PA2) to usart3_rx (PB11). The program send 8bit from usart2 to usart3 and ceck if the transmitted data is equal to the received data, but i can' t understand why this is not working./**
*****************************************************************************
**
** test transmitting usart2 receiving usart3
** PA2 -> usart2 tx
** PB11-> usart3
**
** turn on blue led if transmitted data = received data
** trun on green led when the trasmission end
*****************************************************************************
*/
/* Includes */
#include <
stddef.h
>
#include ''stm32f10x.h''
void usart2_settings(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//i don't know if i have to configure this structure
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART2, &USART_ClockInitStructure);
/* USART parameters */
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;
/* Configuring and enabling USART */
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
/*configuration for PA2 -> tx*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void usart3_settings(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//i don't know if i have to configure this structure
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART3, &USART_ClockInitStructure);
/* USART parameters */
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;
/* Configuring and enabling USART */
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
/*configuration for PB11 -> rx*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void led_blue_config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the GPIO_LED Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure the GPIO_LED pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void led_green_config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the GPIO_LED Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure the GPIO_LED pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
int main(void)
{
int k;
u8 data_tx= 0x59;
u8 data_rx=0;
usart2_settings();
usart3_settings();
led_blue_config();
led_green_config();
//turn off leds
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
USART_SendData(USART2, data_tx);
for(k=0;k<100000;k++) {}
data_rx = USART_ReceiveData(USART3);
//turn on blue led if transmitted data = received data
if (data_rx==data_tx)
GPIO_SetBits(GPIOC, GPIO_Pin_8);
while (1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_9);
}
}
The blue led never turns on.
I hope someone can help me.
2013-02-16 09:13 AM
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure); // GPIOB not GPIOC Should probably also be checking on USART status before sending/receiving.2013-02-16 10:03 AM
thank you very much
clive1.
I changed GPIOC to GPIOB and i modified the main function, now before sending/receiving i check the flag status but the program still not working, i add an istruction to turn on the green led when the trasmission is complete but it never turns on. Can be a problem with theusart initialization ?int main(void)
{
int k;
u8 data_tx= 0x59;
u8 data_rx=0;
usart2_settings();
usart3_settings();
led_blue_config();
led_green_config();
//turn off leds
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
for(k=0;k<500000;k++) {}
USART_SendData(USART2, data_tx);
//wait for the transmission is complete
while (!USART_GetFlagStatus(USART2, USART_FLAG_TC))
{
}
GPIO_SetBits(GPIOC, GPIO_Pin_9);
//wait for the data is ready to read
while(!USART_GetFlagStatus(USART2, USART_FLAG_RXNE))
{
}
data_rx = USART_ReceiveData(USART3);
//turn on blue led if transmitted data = received data
if (data_rx==data_tx)
GPIO_SetBits(GPIOC, GPIO_Pin_8);
while (1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_9);
}
}
2013-02-16 10:15 AM
thank you very much
clive1.
I changed GPIOC to GPIOB and i modified the main function, now before sending/receiving i check the flag status but the program still not working, i add an istruction to turn on the green led when the trasmission is complete but it never turns on. Can be a problem with theusart initialization ?int main(void)
{
int k;
u8 data_tx= 0x59;
u8 data_rx=0;
usart2_settings();
usart3_settings();
led_blue_config();
led_green_config();
//turn off leds
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
for(k=0;k<500000;k++) {}
USART_SendData(USART2, data_tx);
//wait for the transmission is complete
while (!USART_GetFlagStatus(USART2, USART_FLAG_TC))
{
}
GPIO_SetBits(GPIOC, GPIO_Pin_9);
//wait for the data is ready to read
while(!USART_GetFlagStatus(USART2, USART_FLAG_RXNE))
{
}
data_rx = USART_ReceiveData(USART3);
//turn on blue led if transmitted data = received data
if (data_rx==data_tx)
GPIO_SetBits(GPIOC, GPIO_Pin_8);
while (1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_9);
}
}
2013-02-16 10:16 AM
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // Should be APB1
2013-02-16 10:27 AM
Thank you again for your
patience
and the very quick answer. Now ewrything is ok.