cancel
Showing results for 
Search instead for 
Did you mean: 

DMX512 control with STM32

Aniket
Associate II

i have done the dmx512 receiving in the stm32 
but now i am trying to make it robust , when i connect disconnect the cable many times the data in my buffer changes for a second which effects in the flickering 
i am using the framing error methood
i will share u the code below 

void USART3_IRQHandler(void)

{

// Check for overrun error

// if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE) != RESET) {

// ore = 1 ;

// memcpy(rxBuffer , bitArray , sizeof(bitArray));

//// if (skipFirstByte) {

// skipFirstByte = 0; // Set the flag to false after skipping

//// } else {

// if (rxIndex < BUFFER_SIZE) {

//

//// rxBuffer[rxIndex++] = receivedByte; // Store valid data

// // if (rxIndex ==513){

// scaledValue = (rxBuffer[1] * 257) ;

// scaledValue2 = (rxBuffer[2] * 257) ;

// scaledValue3 = (rxBuffer[3] * 257) ;

// scaledValue4 = (rxBuffer[4] * 257) ;

// scaledValue5 = (rxBuffer[5] * 257) ;

//

// __HAL_TIM_SET_COMPARE(RT1, R1, scaledValue);

// __HAL_TIM_SET_COMPARE(GT1, G1, scaledValue2);

// __HAL_TIM_SET_COMPARE(BT1, B1, scaledValue3);

// __HAL_TIM_SET_COMPARE(WWT1, WW1, scaledValue4);

// __HAL_TIM_SET_COMPARE(CWT1, CW1, scaledValue5);

//

// __HAL_TIM_SET_COMPARE(RT2, R2, scaledValue);

// __HAL_TIM_SET_COMPARE(GT2, G2, scaledValue2);

// __HAL_TIM_SET_COMPARE(BT2, B2, scaledValue3);

// __HAL_TIM_SET_COMPARE(WWT2, WW2, scaledValue4);

// __HAL_TIM_SET_COMPARE(CWT2, CW2, scaledValue5);

// }

// __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_ORE); // Clear the overrun error flag

//// skipFirstByte = 1; // Set flag to skip first byte on next reception

// }

// Check for framing error

if ((__HAL_UART_GET_FLAG(&huart3, UART_FLAG_FE) != RESET) && (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) != RESET)&& (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE) == RESET)&& ((volatile uint32_t)(USART3->SR)!= 448)) {

// Handle framing error

rxIndex = 0; // Reset index

fe = 1 ;

currentFrameTime = HAL_GetTick(); // Replace with your system tick function if different



// Calculate time between frames

timeBetweenFrames = currentFrameTime - lastFrameTime;



// Update last frame time

lastFrameTime = currentFrameTime;

if ((volatile uint32_t)(USART3->SR)!= 448){

if (rxBuffer[0] == 0 ) {

memcpy(bitArray , rxBuffer , sizeof(bitArray));

memset(rxBuffer, 0, sizeof(rxBuffer));

}



}







// Clear buffer



__HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_FE); // Clear the error flag

skipFirstByte = 1; // Reset the flag to skip first byte on next reception

}

// if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET) {

// // Handle idle line detection

// rxIndex = 0; // Reset buffer index

// memset(rxBuffer, 0, sizeof(rxBuffer)); // Clear buffer

// skipFirstByte = 1; // Indicate signal has started

// __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_IDLE); // Clear the idle flag

// }

// Check if data is available to read

if ((__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) != RESET) &&

(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TC) != RESET) &&

((volatile uint32_t)(USART3->SR) != 448)) {



// Get current timestamp (assuming you have a function to get system ticks)

idleFlagStatus = __HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE);

receivedByte = (uint16_t)(USART3->DR & 0xFF); // Read data from USART DR

rxne = 1;



// Store data in buffer, skipping the first byte

if (skipFirstByte) {

skipFirstByte = 0; // Set the flag to false after skipping

} else {

if (rxIndex < BUFFER_SIZE) {

// Check if the condition is met

if (rxBuffer[0] != 0) {

// Reset rxBuffer to prevBuffer

memcpy(rxBuffer, prevBuffer, sizeof(rxBuffer)); // Assuming prevBuffer is defined and has the same size

} else {

// Store valid data

rxBuffer[rxIndex++] = receivedByte;



// Scale and update PWM values

scaledValue = (rxBuffer[1] * 257);

scaledValue2 = (rxBuffer[2] * 257);

scaledValue3 = (rxBuffer[3] * 257);

scaledValue4 = (rxBuffer[4] * 257);

scaledValue5 = (rxBuffer[5] * 257);



// Update PWM outputs

__HAL_TIM_SET_COMPARE(RT1, R1, scaledValue);

__HAL_TIM_SET_COMPARE(GT1, G1, scaledValue2);

__HAL_TIM_SET_COMPARE(BT1, B1, scaledValue3);

__HAL_TIM_SET_COMPARE(WWT1, WW1, scaledValue4);

__HAL_TIM_SET_COMPARE(CWT1, CW1, scaledValue5);



__HAL_TIM_SET_COMPARE(RT2, R2, scaledValue);

__HAL_TIM_SET_COMPARE(GT2, G2, scaledValue2);

__HAL_TIM_SET_COMPARE(BT2, B2, scaledValue3);

__HAL_TIM_SET_COMPARE(WWT2, WW2, scaledValue4);

__HAL_TIM_SET_COMPARE(CWT2, CW2, scaledValue5);

}

}

}



__HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_IDLE); // Clear the idle flag

}



// if ((volatile uint32_t)(USART3->SR)== 448){

// memcpy(rxBuffer , bitArray , sizeof(bitArray));

//// if (skipFirstByte) {

// skipFirstByte = 0; // Set the flag to false after skipping

//// } else {

// if (rxIndex < BUFFER_SIZE) {

//

// // rxBuffer[rxIndex++] = receivedByte; // Store valid data

// // if (rxIndex ==513){

// scaledValue = (rxBuffer[1] * 257) ;

// scaledValue2 = (rxBuffer[2] * 257) ;

// scaledValue3 = (rxBuffer[3] * 257) ;

// scaledValue4 = (rxBuffer[4] * 257) ;

// scaledValue5 = (rxBuffer[5] * 257) ;

//

// __HAL_TIM_SET_COMPARE(RT1, R1, scaledValue);

// __HAL_TIM_SET_COMPARE(GT1, G1, scaledValue2);

// __HAL_TIM_SET_COMPARE(BT1, B1, scaledValue3);

// __HAL_TIM_SET_COMPARE(WWT1, WW1, scaledValue4);

// __HAL_TIM_SET_COMPARE(CWT1, CW1, scaledValue5);

//

// __HAL_TIM_SET_COMPARE(RT2, R2, scaledValue);

// __HAL_TIM_SET_COMPARE(GT2, G2, scaledValue2);

// __HAL_TIM_SET_COMPARE(BT2, B2, scaledValue3);

// __HAL_TIM_SET_COMPARE(WWT2, WW2, scaledValue4);

// __HAL_TIM_SET_COMPARE(CWT2, CW2, scaledValue5);

// }

//

// // }

//// }

// }



}


thiis is working but how can i solve the buffer value change when i regoresly connect or discoonect pelase help

2 REPLIES 2
SofLit
ST Employee

Hello @Aniket ,

Please review tips on how to post a thread in this community on this link, mainly for the title, the STM32 part number and the code source sharing. I'm editing your post.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
SofLit
ST Employee

+

Related thread: https://community.st.com/t5/stm32-mcus-embedded-software/dmx512-with-stm32f103/td-p/728244

Please if you have solved your issue in it, please provide how did you solve it and mark it as solution.

Thank you.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.