2022-05-24 09:29 AM
Hi All,
I need some help and guidance as to how I can read data off UART sensor?
I have tried to following many instructions from my professor and from online but have been so far unsuccessful at obtaining any data.
Currently doing testing through CubeIDE and have the STM32F411RE plugged into my pc through USB and am able to transmit and receive data using TERA TERM but cannot get any readings to show from the sensor.
I am wondering if I'm at all reading correctly? Can I read data from the sensor while plugged into my PC, similar to I2C?
If not how should I read data from the sensor? Please see my current code below.
Oh and we have to use LL Logic not HAL!
uint8_t USART2_GetChar()
{
uint8_t data;
data = RxBuffer[RxTailIndex]; // read the character indexed by RxTailIndex (ie the oldest valid character in the buffer)
RxTailIndex++; //increment the tail index (now points to the next oldest char
if(RxTailIndex>= 1000) // wrap the tail index around to zero if it reaches the max value of the array
RxTailIndex = 0;
return data;
}
// Function to determine if a character is available.
// Returns non zero if there are characters in the buffer
// Returns zero if no characters are in the buffer
uint8_t USART2_CharAvail()
{
return(RxHeadIndex-RxTailIndex); // compare head and tail index, if they are different it means there are characters present. If difference is 0
// there are no characters in the buffer
}
void USART2_IRQHandler()
{
if(LL_USART_IsActiveFlag_RXNE(USART2) && LL_USART_IsEnabledIT_RXNE(USART2)) // Check to see if it is the character received interrupt that has fired
{
Data = LL_USART_ReceiveData8(USART2); // Reading from the USART Data Rx register clears the RXNE flag
RxBuffer[RxHeadIndex] = Data; // Store the received character in the RxBuffer at the location inexed by RxHeadIndex
RxHeadIndex++; // Increment RxHeadIndex
if(RxHeadIndex >= 1000) // If RxHeadIndex is at the max value of the array wrap around to 0. This may overwrite oldest chars
RxHeadIndex = 0; // if they have not been cleared. Moral of the story , read from the buffer before it overwrites.
}
}
while (1)
{
AirQuality();
}
void AirQuality()
{
uint8_t data;
if(USART2_CharAvail()) // Test to see if a character has been recieved
{
data = USART2_GetChar(); // if yes do something interesting with it
}
}
2022-05-24 09:55 AM
The data is looks like it's not going to be in ASCII (human readable), it is in binary, packetized, in a format described in the manual.
Basically a stream of data you'll need to synchronize too, and parse.
You might also need to send commands to start it.
Not sure how qualified the professors is, or in what, but his job is to teach you the means of solving problems, probably not to solve them for you. To be honest the task looks doable for a secondary school MCU programming project.
2022-05-25 04:09 AM
Thanks for that. I am aware that I need to send the command but how would I do this in code?
The professor has shown us but only showed us how to blink an LED through the means of UART and use Tera Term to send and receive characters. DId not actually demo communicating with an actual device other them a terminal :(