2014-07-29 12:28 PM
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 #stm82014-08-01 07:57 AM
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 <iostm8s003f3.h> #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.