Ответов много и все верный.. ну как же моих трех копеек ))...
#include "events.h" static TTMyEvent t2_events[MAX_EVENT] = { { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE }, { DISABLE, 0, NULL, DISABLE } }; static int pause = DISABLE; void event_Disable(void* event) { pause = ENABLE; if (event == EVENT_ALL) { for (int i = 0; i < MAX_EVENT; i++) t2_events[i].enabled = DISABLE; } else { for (int i = 0; i < MAX_EVENT; i++) if (t2_events[i].event == event) { t2_events[i].enabled = DISABLE; t2_events[i].event = NULL; break; } } pause = DISABLE; } void event_Init(void* event, uint32_t ms) { event_Disable(event); for (int i = 0; i < MAX_EVENT; i++) if (t2_events[i].event == NULL) { t2_events[i].event = event; t2_events[i].ms = ms; t2_events[i].cycle = 0; t2_events[i].enabled = ENABLE; break; } } void event_InitCycle(void* event, uint32_t cycle) { event_Disable(event); for (int i = 0; i < MAX_EVENT; i++) if (t2_events[i].event == NULL) { t2_events[i].event = event; t2_events[i].ms = cycle; t2_events[i].cycle = cycle; t2_events[i].enabled = ENABLE; break; } } void TIM2_IRQHandler(void) { if (pause == DISABLE) for (int i = 0; i < MAX_EVENT; i++) if (t2_events[i].enabled == ENABLE) { if (--t2_events[i].ms == 0) { if (t2_events[i].cycle) t2_events[i].ms = t2_events[i].cycle; else t2_events[i].enabled = DISABLE; if (t2_events[i].event != NULL) ((TFunc)t2_events[i].event)(); } } TIM_ClearITPendingBit(TIM2, TIM_IT_Update); __ASM("nop"); __ASM("nop"); }
#ifndef EVENTS_H #define EVENTS_H #include "common.h" #define MAX_EVENT 10 typedef void (*TFunc)(); typedef struct { uint32_t enabled; uint32_t ms; void* event; uint8_t cycle; } TTMyEvent; #define EVENT_ALL NULL void event_Disable(void* event); void event_Init(void* event, uint32_t ms); void event_InitCycle(void* event, uint32_t cycle); #endif // EVENTS_H