Реализован обработчик прерывания USART3

This commit is contained in:
Radzhab Bisultanov
2026-02-20 14:51:25 +03:00
parent 6c9c1ba35b
commit a6f47a2e73
2 changed files with 41 additions and 1 deletions

View File

@@ -5,6 +5,9 @@
#include "stm32g431xx.h" #include "stm32g431xx.h"
#define USART3_START_BYTE 0x59
#define USART3_FRAME_SIZE 9
typedef struct typedef struct
{ {
uint8_t header1; // 0x59 uint8_t header1; // 0x59
@@ -16,7 +19,7 @@ typedef struct
uint8_t temp_l; uint8_t temp_l;
uint8_t temp_h; uint8_t temp_h;
uint8_t checksum; uint8_t checksum;
} lidar_data; } lidar_data_buf;
void lidar_init(); void lidar_init();
void lidar_update(); void lidar_update();

View File

@@ -1,5 +1,11 @@
#include "lidar.h" #include "lidar.h"
volatile uint8_t usart3_index = 0;
volatile uint8_t usart3_checksum = 0;
volatile uint8_t usart3_frame_ready = 0;
static lidar_data_buf buffer;
static uint8_t* buff_data = (uint8_t*)&buffer;
void lidar_init() void lidar_init()
{ {
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN; RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
@@ -52,3 +58,34 @@ void lidar_init()
// Interrupt enable // Interrupt enable
NVIC_EnableIRQ(USART3_IRQn); NVIC_EnableIRQ(USART3_IRQn);
} }
void USART3_IRQHandler()
{
if (USART3->ISR & USART_ISR_RXNE)
{
uint8_t b = USART3->RDR;
if (usart3_index < 2)
{
if (b == USART3_START_BYTE)
buff_data[usart3_index++] = b;
}
else if (usart3_index < USART3_FRAME_SIZE)
buff_data[usart3_index++] = b;
if (usart3_index == USART3_FRAME_SIZE)
{
for (uint8_t i = 0; i < USART3_FRAME_SIZE; ++i) usart3_checksum += buff_data[i];
if (buffer.checksum == usart3_checksum)
{
usart3_index = 0;
usart3_frame_ready = 1;
}
else
{
usart3_index = 0;
usart3_frame_ready = 0;
}
}
}
}