Amphenol SM-UART-04L是一款激光粉尘传感器从UART传感器读取数据。我不知道如何从UART传感器读取数据。
我已经试过网上的许多说明,但至今未能成功读取数据。
目前通过CubeIDE进行测试,将STM32F411RE通过USB连接到电脑,能够使用TERA TERM传输和接收数据,但无法从传感器读数。
是否可以在连接到电脑时从传感器读取数据,类似于I2C?
如果不能,应该如何从传感器读取数据?以下是代码。
使用LL库而不是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
}
}
|
|