ST意法半导体
直播中

洪茗苞

7年用户 260经验值
私信 关注
[问答]

UART3不会发送数据

我正在使用UART3进行RS-485通信(我在PC和威廉希尔官方网站 板之间连接了B& B RS-232到RS-485转换器)。麻烦的是UART3不会“发送”数据,除非我在通过UART3发送数据后不久放置调试断点。然后数据发送正常。如果没有断点,PC就不会收到数据。这是我发送数据的功能:


void send_485(uint8_t * ptr,uint8_t length)
{
uint8_t i = 0;
uint8_t count = _1s_counter;
GPIO_WriteHigh(GPIOE,GPIO_PIN_4);
while(_1s_counter< count + 5){}
而(长度 - > 0)
{
/ *等待UART3 TXE = 0 * /
while(UART3_GetFlagStatus(UART1_FLAG_TXE)== RESET)
{
}
UART3_SendData8((PTR [I ++]));
}
count = _1s_counter;
while(UART3_GetFlagStatus(UART1_FLAG_TXE)== RESET)
{
}
while(_1s_counter< count + 1000){}
GPIO_WriteLow(GPIOE,GPIO_PIN_4); //在这一行放置断点。
}

关于调试器做什么使得通信工作的任何想法?添加该断点,可以完美地接收字节。

谢谢,
万斯


#uart3#rs485#stm8

以上来自于谷歌翻译


以下为原文




I'm using UART3 for RS-485 communications (I have a B&B RS-232 to RS-485 converter connected between the PC and the board). The trouble is that the UART3 won't ''send'' the data unless I put a debug break-point shortly after sending data via UART3. The data then sends fine. Without the break-point, the PC doesn't receive the data. Here is my function for sending data:


void send_485(uint8_t *ptr, uint8_t length)
{
  uint8_t i = 0;
uint8_t count = _1s_counter;
  GPIO_WriteHigh(GPIOE, GPIO_PIN_4);
while(_1s_counter < count+5){}
  while (length-- > 0)
  {
    /* Wait while UART3 TXE = 0 */
    while (UART3_GetFlagStatus(UART1_FLAG_TXE) == RESET)
    {
    }
    UART3_SendData8((ptr[i++]));
  }
count = _1s_counter;
    while (UART3_GetFlagStatus(UART1_FLAG_TXE) == RESET)
    {
    }
while(_1s_counter < count + 1000){}
  GPIO_WriteLow(GPIOE, GPIO_PIN_4); // Put break-point on this line.
}  

Any ideas as to what the debugger does that makes the communication work? Adding that break-point, the bytes are received flawlessly.

Thanks,
Vance

  
#uart3 #rs485 #stm8

回帖(1)

张林锋

2019-4-30 15:14:38
嗨,万斯。
 
 对我来说,你的代码看起来有点多余。这是我使用STM8 MCU通过RS485发送字节结果的代码:
 
 #include&lt; iostm8s003f3.h&gt;
 #define TX_ENABLE PD_ODR_ODR4
 void RS485_send_packet(unsigned char * packet,int len)
 {
 UART1_CR2_RIEN = 0; //禁用接收中断
 TX_ENABLE = 1;
 
 而(len)
 {
 UART1_DR = *数据包; //将下一个字符放入数据传输寄存器。
 而(UART1_SR_TXE == 0); //等待传输完成
 包++; //抓住下一个角色。
 len--;
 }
 而(!UART1_SR_TC); //等待最后一个字节的传输完成
 UART1_CR2_RIEN = 1; //启用接收中断
 TX_ENABLE = 0;
 
 }
 
 它工作正常。
 P.S。:我不记得了,为什么我在开始传输之前禁用接收中断...可能有一些问题。

以上来自于谷歌翻译


以下为原文





Hi, Vance.

Your code looks a little bit redundant, as for me. Here's the code I use to send consequence of bytes via RS485 using STM8 MCU:

#include
#define TX_ENABLE PD_ODR_ODR4
void RS485_send_packet(unsigned char *packet, int len)
{
     UART1_CR2_RIEN = 0;   // Disable receive interrupt
     TX_ENABLE = 1;  

     while (len)
     {
         UART1_DR = *packet;     //  Put the next character into the data transmission register.
         while (UART1_SR_TXE == 0);          //  Wait for transmission to complete.
         packet++;                               //  Grab the next character.
         len--;
     }
     while (!UART1_SR_TC); // Wait for transmission of the last byte to complete
     UART1_CR2_RIEN = 1;   // Enable receive interrupt
     TX_ENABLE = 0;

}

It works fine.
P.S.: I don't remember, why I disable receive interrupt before start of transmission... Probably had some issues with that.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分