Help Reading Data off UART sensor - Amphenol SM-UART-04L is a laser dust sensor
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
}
}