Why elements in an array elements shift?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-12-23 4:05 PM
Hi, i am using stm32f4. in my project i have one transmitter and one receiver.
i transmit an 2 byte integer value using
HAL_UART_Transmit(&huart1,&temp_byte1,sizeof(temp_byte1), 500);
HAL_UART_Transmit(&huart1,&temp_byte2,sizeof(temp_byte2), 500);
and on the receiver part. i read these values using
void USART6_IRQHandler(void)
{
HAL_UART_Receive_IT(&huart6,&rx_data,1);
temp_data[rx_index]=rx_data;
rx_index++;
if(rx_index==2)
{
rx_index=0;
temp = ((temp_data[0]<<8)|temp_data[1]);
if(temp!=79){ //sometimes i see a 79 in my buffer!
hc05_flag = 3; //irrelevant flag with this question
nm=0; //irrelevant flag with this question
}
}
it works fine for one time. but after, i see temp_data[1] in temp_data[0]. it shift 8 bits and i see a big temp value (63744 etc.) . where is my problem?
- Labels:
-
STM32CubeMX
-
STM32F4 Series
-
UART-USART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-12-23 5:40 PM
My guess is that this is a classroom exercise.
Your problem is that you have two processes and no method to synchronize related transmits and receives.
Its your project. I will leave it up to you to figure out how to keep them in sync.
Cheers, Hal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-12-23 6:34 PM
HAL_UART_Receive_IT() does not work like this. You need to collect the data in the callback, as the function you're using should return immediately, not when it actually has data.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-12-23 11:20 PM
You must use HAL_UART_IRQHandler() and HAL_UART_RxCpltCallback().
