#ifndef SW_TIMER_H_ #define SW_TIMER_H_ #define SwTimerCount 64 // /* */ typedef enum { SWTIMER_MODE_EMPTY, SWTIMER_MODE_WAIT_ON, SWTIMER_MODE_WAIT_OFF, SWTIMER_MODE_CYCLE, SWTIMER_MODE_SINGLE } SwTimerMode; /* */ typedef struct { unsigned LocalCount:32; // unsigned Count:24; // unsigned Mode:3; // unsigned On:1; // unsigned Reset:1; // unsigned Off:1; // unsigned Out:1; // unsigned Status:1; // }SW_TIMER; #if (SwTimerCount>0) volatile SW_TIMER TIM[SwTimerCount]; // #endif void SwTimerWork(volatile SW_TIMER* TIM, unsigned char Count); // void OnSwTimer(volatile SW_TIMER* TIM, SwTimerMode Mode, unsigned int SwCount); // unsigned char GetStatusSwTimer(volatile SW_TIMER* TIM); // #endif /* SW_TIMER_H_ */
void SwTimerWork(volatile SW_TIMER* TIM, unsigned char Count);
void SwTimerWork(volatile SW_TIMER* TIM, unsigned char Count){ unsigned short i=0; for (i=0; i<Count; i++){ if (TIM->Mode==SWTIMER_MODE_EMPTY) { TIM++; continue; } if (TIM->Mode==SWTIMER_MODE_WAIT_ON){ // if (TIM->On){ if (TIM->LocalCount>0) TIM->LocalCount--; else { TIM->Out=1; TIM->Status=1; } } else { TIM->Out=0; TIM->LocalCount=TIM->Count-1; } } if (TIM->Mode==SWTIMER_MODE_WAIT_OFF){ // if (TIM->On){ TIM->Out=1; TIM->Status=1; TIM->LocalCount=TIM->Count-1; } else { if (TIM->LocalCount>0) TIM->LocalCount--; else TIM->Out=0; } } if (TIM->Mode==SWTIMER_MODE_CYCLE){ if (TIM->Off){ if (TIM->On){ TIM->Off=0; if (TIM->LocalCount>0) TIM->LocalCount--; } } else{ if (TIM->LocalCount>0) { TIM->LocalCount--; TIM->Out=0; } else { TIM->Out=1; TIM->Status=1; TIM->LocalCount=TIM->Count-1; } } if (TIM->Reset){ TIM->LocalCount=TIM->Count-1; TIM->Out=0; TIM->Status=0; } } if (TIM->Mode==SWTIMER_MODE_SINGLE){ if (TIM->Off){ if (TIM->On){ TIM->Off=0; if (TIM->LocalCount>0) TIM->LocalCount--; } } else{ if (TIM->LocalCount>0) { TIM->LocalCount--; TIM->Out=0; } else { TIM->Out=1; TIM->Status=1; TIM->LocalCount=TIM->Count-1; TIM->Off=1; } } if (TIM->Reset){ TIM->LocalCount=TIM->Count-1; TIM->Out=0; TIM->Status=0; } } TIM++; } }
/* USER CODE BEGIN 0 */ #include "sw_timer.h" /* USER CODE END 0 */ void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); HAL_SYSTICK_IRQHandler(); /* USER CODE BEGIN SysTick_IRQn 1 */ SwTimerWork(TIM,SwTimerCount); /* USER CODE END SysTick_IRQn 1 */ }
/* USER CODE BEGIN 0 */ extern uint8_t FlagSwTimer; /* USER CODE END 0 */ void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); HAL_SYSTICK_IRQHandler(); /* USER CODE BEGIN SysTick_IRQn 1 */ FlagSwTimer=1; /* USER CODE END SysTick_IRQn 1 */ } /* */ while(1){ if (FlagSwTimer){ #if (SwTimerCount>0) // sw_timer.c SwTimerWork(TIM,SwTimerCount); #endif FlagSwTimer=0; }
void OnSwTimer(volatile SW_TIMER* TIM, SwTimerMode Mode, unsigned int SwCount);
unsigned char GetStatusSwTimer(volatile SW_TIMER* TIM);
#define I_DI_READ_0() HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) // OnSwTimer(&TIM[0],SWTIMER_MODE_WAIT_OFF,1000); OnSwTimer(&TIM[1],SWTIMER_MODE_SINGLE,1000); OnSwTimer(&TIM[2],SWTIMER_MODE_WAIT_ON,1000); OnSwTimer(&TIM[3],SWTIMER_MODE_CYCLE,1000); TIM[3].Off=0; TIM[3].On=1; // TIM[1].Off=0; TIM[1].On=1; //
while (1){ TIM[2].On=I_DI_READ_0(); // , HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,TIM[2].Out); // , TIM[0].On=I_DI_READ_0(); // , HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,TIM[0].Out); // , if (GetStatusSwTimer(&TIM[3])){ HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_13); // , } if (GetStatusSwTimer(&TIM[1])){ HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12); // , } TIM[3].Reset=I_DI_READ_0(); // , }
TIM[3].On=0; TIM[3].Off=1;
if (GetStatusSwTimer(&TIM[1])) // if (TIM[3].Status){ // TIM[3].Status=0; // , // , /*UserCode*/ }
OnSwTimer(&TIM[3],SWTIMER_MODE_EMPTY,0); // TIM[3].Mode=SWTIMER_MODE_EMPTY; //
Source: https://habr.com/ru/post/273077/
All Articles