cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0- RS485 communication problem

aruns06
Associate II
Posted on July 09, 2014 at 12:39

Hi,

I am trying to connect STM32F0 board with an energy meter. The Energy meter supports RS485 protocol. I have connected USART 2 to MAX 485 and the o/p of the MAX485 is connected to a level shifter 74ls25 and then to energy meter. But I'm ot getting reply from energy meter. Following is the code section. don't know whether any change in configuration of STM32 rs485 initialization.

void
init_rs485(COM_TypeDef com, USART_InitTypeDef* USART_InitStructure)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
//USART_DeInit(USART2);
/* Configure gpio pin for DE to control data flow of max485 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable USART 1 clock */
//if(com == COM1)
// RCC_APB2PeriphClockCmd(COM_USART_CLK[com], ENABLE); ///commented by DTS for usart1
// else if(com == COM2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
/// for USART2 DTS 
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
// GPIOA->BSRR = GPIO_Pin_8;
USART_InitStructure->USART_BaudRate = 9600;
USART_InitStructure->USART_WordLength = USART_WordLength_9b; 
// previously it was USART_WordLength_8b
USART_InitStructure->USART_StopBits = USART_StopBits_1;
USART_InitStructure->USART_Parity = USART_Parity_Even; 
// vriable parameter. Based on the data to be sent. 
USART_InitStructure->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
/* USART configuration */
USART_Init(USART2, USART_InitStructure);
// USART_ClockInit(USART2,&USART_ClockInitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
}

After the initialization, I have started sending data via usart1 and then a small is given and receive function starts.... Is there any problem in usart 1???? Regards, Arun.S #rs485 #stm32
4 REPLIES 4
Posted on July 09, 2014 at 14:09

What STM32F0 part are you using exactly? Different parts map the USART slightly differently.

I would make sure the MAX485 part is switched appropriately, and wait for the TC status on the USART to understand when the last bit has been transmitted completely.

I don't understand the role of the 74LS25 in your circuit, perhaps you can diagram it?

Have you looked at and confirmed the signal being sent on a scope? Can you see any response? How does this compare to other signalling with the meter which demonstrably works?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raptorhal2
Lead
Posted on July 09, 2014 at 15:16

Check that the USART's Tx ends up at the meter's Rx, and the USART's Rx ends up at the meters Tx.

Cheers, Hal 

aruns06
Associate II
Posted on July 10, 2014 at 11:28

''What STM32F0 part are you using exactly?''------------------------------------- STM32F051R8T6 (Dev board from ST micro Electronics)

''I would make sure the MAX485 part is switched appropriately, and wait for the TC status on the USART to understand when the last bit has been transmitted completely.'' -------------------------Hop the following part will answer your Query.

void
data_frame( USART_InitTypeDef* USART2_InitStructure)
{
unsigned 
char
buf[20], chTmp;
unsigned 
short
usCRC;
buf[0] = 0x01; 
buf[1] = 0x03; 
buf[2] = 0x0F; 
// 0x0F; // buf[2] = 0x00; 
buf[3] = 0x4B; 
// 0x3D; // buf[3] = 0x01; 
buf[4] = 0x00; 
buf[5] = 0x02; 
usCRC = ComputeCRC(buf,6);
chTmp = usCRC >> 8;
buf[6] = (unsigned 
char
)usCRC; 
// Low order CRC
buf[7] = chTmp; 
// High order CRC
RS485SendBuffer(buf,USART2_InitStructure, 8);
delay(10000);
}
void
RS485SendBuffer(unsigned 
char
* pBuf, USART_InitTypeDef* USART2_InitStructure, 
int
nCount)
{
unsigned 
char
c,chPar;
int
nCnt = 0;
while
(nCnt < nCount)
{
// Delay10KTCYx(255);
c = pBuf[nCnt];
USART_SendData(EVAL_COM2, c);
//USART_SendData(EVAL_COM1, c); // to display message in the terminal 
/* Loop until the end of transmission */
while
(USART_GetFlagStatus(EVAL_COM2, USART_FLAG_TC) == RESET)
{
}
nCnt++; 
}
} 

I don't understand the role of the 74LS25 in your circuit, perhaps you can diagram it?---------------------------------I think the o/p of STM32F0 is 3.5 V and the o/P of the Energy meter is through RS485 ad hence 5 v. so is the 74ls level shifter. Have you looked at and confirmed the signal being sent on a scope? --------------------------------------------Yes I have tried it and I am getting some square waves. The Energy meter(EM6436) demands Data bits- 8,Baud rate 9600, Parity Even. In the configuration section I choose World length USART_WordLength_9b. Thanks, Arun.S
aruns06
Associate II
Posted on July 14, 2014 at 07:51

Thanks to  and Clive1  for replying my query. The configuration I mentioned above is correct and problem was with the Energymeter configuration.

Regards,

Arun.S