Advertisement
Guest User

Untitled

a guest
May 9th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include "stm32f4xx_hal.h"
  2.  
  3. #define BUFFER_SIZE 100
  4.  
  5. // Circular buffer structure
  6. typedef struct {
  7.     uint8_t buffer[BUFFER_SIZE];
  8.     uint32_t head;
  9.     uint32_t tail;
  10. } CircularBuffer;
  11.  
  12. CircularBuffer rxBuffer;
  13.  
  14. // Flag to indicate continuous data reading from port D
  15. volatile uint8_t continuousRead = 0;
  16.  
  17. // External interrupt callback function
  18. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  19.     // Check if the interrupt is from the expected pin
  20.     if (GPIO_Pin == GPIO_PIN_0) {
  21.         continuousRead = 1; // Set the flag for continuous reading
  22.     }
  23. }
  24.  
  25. int main(void) {
  26.     HAL_Init();
  27.     // Initialize the HAL Library
  28.     HAL_Init();
  29.    
  30.     // Configure GPIO pins for external interrupt and port D for input
  31.     GPIO_InitTypeDef GPIO_InitStruct;
  32.     __HAL_RCC_GPIOD_CLK_ENABLE();
  33.    
  34.     GPIO_InitStruct.Pin = GPIO_PIN_0;
  35.     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  36.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  37.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  38.  
  39.     // Enable and set EXTI line 0 Interrupt to the lowest priority
  40.     HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
  41.     HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  42.  
  43.     // Initialize circular buffer
  44.     rxBuffer.head = 0;
  45.     rxBuffer.tail = 0;
  46.  
  47.     while (1) {
  48.         // Continuously read from port D if the flag is set
  49.         if (continuousRead) {
  50.             // Read data from port D and store it in the circular buffer
  51.             rxBuffer.buffer[rxBuffer.head] = GPIOB->IDR & 0xFF; // Assuming port D
  52.             rxBuffer.head = (rxBuffer.head + 1) % BUFFER_SIZE;
  53.         }
  54.        
  55.         // Process the data
  56.         if (rxBuffer.head != rxBuffer.tail) {
  57.             uint8_t data = rxBuffer.buffer[rxBuffer.tail];
  58.             // Your processing code here
  59.             rxBuffer.tail = (rxBuffer.tail + 1) % BUFFER_SIZE;
  60.         }
  61.  
  62.         // Clear the flag after processing data
  63.         continuousRead = 0;
  64.     }
  65. }
  66.  
  67. // EXTI0 interrupt handler
  68. void EXTI0_IRQHandler(void) {
  69.     HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement