Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx_hal.h"
- #define BUFFER_SIZE 100
- // Circular buffer structure
- typedef struct {
- uint8_t buffer[BUFFER_SIZE];
- uint32_t head;
- uint32_t tail;
- } CircularBuffer;
- CircularBuffer rxBuffer;
- // Flag to indicate continuous data reading from port D
- volatile uint8_t continuousRead = 0;
- // External interrupt callback function
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
- // Check if the interrupt is from the expected pin
- if (GPIO_Pin == GPIO_PIN_0) {
- continuousRead = 1; // Set the flag for continuous reading
- }
- }
- int main(void) {
- HAL_Init();
- // Initialize the HAL Library
- HAL_Init();
- // Configure GPIO pins for external interrupt and port D for input
- GPIO_InitTypeDef GPIO_InitStruct;
- __HAL_RCC_GPIOD_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- // Enable and set EXTI line 0 Interrupt to the lowest priority
- HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
- HAL_NVIC_EnableIRQ(EXTI0_IRQn);
- // Initialize circular buffer
- rxBuffer.head = 0;
- rxBuffer.tail = 0;
- while (1) {
- // Continuously read from port D if the flag is set
- if (continuousRead) {
- // Read data from port D and store it in the circular buffer
- rxBuffer.buffer[rxBuffer.head] = GPIOB->IDR & 0xFF; // Assuming port D
- rxBuffer.head = (rxBuffer.head + 1) % BUFFER_SIZE;
- }
- // Process the data
- if (rxBuffer.head != rxBuffer.tail) {
- uint8_t data = rxBuffer.buffer[rxBuffer.tail];
- // Your processing code here
- rxBuffer.tail = (rxBuffer.tail + 1) % BUFFER_SIZE;
- }
- // Clear the flag after processing data
- continuousRead = 0;
- }
- }
- // EXTI0 interrupt handler
- void EXTI0_IRQHandler(void) {
- HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement