/** * @file HW_TIMER_ATY.c * * @param Project DEVICE_GENERAL_ATY_LIB * * @author ATY * * @copyright * - Copyright 2017 - 2023 MZ-ATY * - This code follows: * - MZ-ATY Various Contents Joint Statement - * * https://mengze.top/MZ-ATY_VCJS * - CC 4.0 BY-NC-SA - * * https://creativecommons.org/licenses/by-nc-sa/4.0/ * - Your use will be deemed to have accepted the terms of this statement. * * @brief Familiar functions of timer definition for all embedded device * * @version * - 1_01_220602 > ATY * -# Preliminary version, first Release * - Undone ******************************************************************************** */ #ifndef __HW_TIMER_ATY_C #define __HW_TIMER_ATY_C #include "HW_TIMER_ATY.h" /******************************* For user *************************************/ #if defined(__STC51_ATY) /** * @brief 1 us delay with for @ 24MHz about 1.04us */ __WEAK_ATY void Delay1Us(void) { // uint8_t i = 2; // for 12MHz uint8_t i = 6; // for 24MHz while(--i); } /** * @brief us delay * @param us us */ __WEAK_ATY void DelayUs(uint32_t us) { // uint32_t i = 2 + 4 * us; // for 12MHz uint32_t i = 6 + 8 * us; // for 12MHz // while(--i); } /** * @brief ms delay * @param ms ms */ __WEAK_ATY void DelayMs(uint32_t ms) { while(ms--) DelayUs(1000); } #if TIMERC_N == 0 /** * @brief cycle timer init * @note put at main init */ void TimerLoopInit(void) //1ms@24.000MHz { AUXR |= 0x80; // timer clock 1T mode TMOD &= 0xF0; // set timer mode TL0 = 0x40; // set timer origin value TH0 = 0xA2; // set timer origin value TF0 = 0; // clear TF0 flag TR0 = 1; // start timer 0 ET0 = 1; // enable timer IT EA = 1; // enable all IT } /** * @brief cycle timer IT callback function */ void TM0_Isr(void) INTERRUPT(1) { UserTimerLoop_Cycle1ms(); } #elif TIMERC_N == 1 /** * @brief cycle timer init * @note put at main init */ void TimerLoopInit(void) //1ms@24.000MHz { AUXR |= 0x40; // timer clock 1T mode TMOD &= 0x0F; // set timer mode TL1 = 0x40; // set timer origin value TH1 = 0xA2; // set timer origin value TF1 = 0; // clear TF1 flag TR1 = 1; // start timer 1 ET1 = 1; // enable timer IT EA = 1; // enable all IT } /** * @brief cycle timer IT callback function */ void TM1_Isr(void) INTERRUPT(3) { UserTimerLoop_Cycle1ms(); } #elif TIMERC_N == 2 void TM2_Isr(void) INTERRUPT(12) { UserTimerLoop_Cycle1ms(); } #elif TIMERC_N == 3 void TM3_Isr(void) INTERRUPT(19) { UserTimerLoop_Cycle1ms(); } #elif TIMERC_N == 4 void TM4_Isr(void) INTERRUPT(20) { UserTimerLoop_Cycle1ms(); } #endif /* TIMERC_N */ #elif defined(__STM32_HAL_ATY) #include "tim.h" /** * @brief us delay with soft cycle * @param us us * @note @ 8MHz(PCLK1 = 36MHz && Others = 72MHz), about 0.9722us */ __WEAK_ATY void Delay1Us(void) { uint8_t i = 12; while(--i); } /** * @brief us delay * @param us us */ __WEAK_ATY void DelayUs(uint32_t us) { uint32_t i = 18 * us; while(--i); } /** * @brief ms delay * @param ms ms * @note long time with cycle is not recommended */ __WEAK_ATY void DelayMs(uint32_t ms) { while(ms--) DelayUs(1000); // HAL_Delay(ms); // HAL_Delay not good for little time } /** * @brief cycle timer IT callback function * @note HAL_TIM_Base_Start_IT(&TIMERC_N); start IT at main init */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { if(htim == &TIMERC_N) { UserTimerLoop_Cycle1ms(); } } #endif /* PLATFORM */ /******************************************************************************/ struct _timeBaseCounter_ATY timeCounterUnit_ATY = {0}; /** * @brief sec delay * @param sec sec */ __WEAK_ATY void DelaySec(uint32_t sec) { while(sec--) DelayMs(1000); } #if 0 uint8_t it = 0; void DelayTest(void) { it = 0; it++; Delay1Us(); it++; Delay1Us(); it++; DelayUs(2); it++; DelayUs(2); it++; DelayUs(10); it++; DelayUs(10); it++; DelayUs(100); it++; DelayUs(100); it++; DelayMs(1); it++; DelayMs(1); it++; DelayMs(2); it++; DelayMs(2); it++; DelayMs(10); it++; DelayMs(10); it++; DelayMs(100); it++; DelayMs(100); #ifdef __STM32_HAL_ATY it++; HAL_Delay(100); it++; HAL_Delay(100); #endif /* __STM32_HAL_ATY */ it++; DelaySec(1); it++; DelaySec(1); it++; it = 0; } #endif /* 0 */ #ifdef __TIMER_CYCLE_ATY /** * @brief define at user file and put at main while * @note long time process is not recommended */ __WEAK_ATY void UserTimerLoopProcess(void) { TimerLoopProcess_User(); } #ifndef __STC51_ATY __WEAK_ATY void TimerLoopProcess_User(void) { // if(timeCounterUnit_ATY.ms1_f == 1) // { // timeCounterUnit_ATY.ms1_f = 0; // } if(timeCounterUnit_ATY.ms100_f == 1) { timeCounterUnit_ATY.ms100_f = 0; // ledBlinkType = (2); // HAL_IWDG_Refresh(&hiwdg); } // if(timeCounterUnit_ATY.s2_f == 1) // { // timeCounterUnit_ATY.s2_f = 0; // } } #endif /* __STC51_ATY */ /** * @brief 1ms timer loop cycle */ void UserTimerLoop_Cycle1ms(void) { // put at 1ms timer IT callback function timeCounterUnit_ATY.msN++; if(timeCounterUnit_ATY.ms10_f == 0) timeCounterUnit_ATY.ms10++; if(timeCounterUnit_ATY.ms100_f == 0) timeCounterUnit_ATY.ms100++; if(timeCounterUnit_ATY.s1_f == 0) timeCounterUnit_ATY.s1++; if(timeCounterUnit_ATY.s2_f == 0) timeCounterUnit_ATY.s2++; if(timeCounterUnit_ATY.s10_f == 0) timeCounterUnit_ATY.s10++; /* ATY: 1ms unit **********************************************************/ timeCounterUnit_ATY.ms1_f = 1; /* ATY: 10ms unit *********************************************************/ if(timeCounterUnit_ATY.ms10 >= 10) { timeCounterUnit_ATY.ms10_f = 1; timeCounterUnit_ATY.ms10 = 0; } /* ATY: 100ms unit ********************************************************/ if(timeCounterUnit_ATY.ms100 >= 100) { timeCounterUnit_ATY.ms100_f = 1; timeCounterUnit_ATY.ms100 = 0; // ledBlinkType = (2); } /* ATY: 1s unit ***********************************************************/ if(timeCounterUnit_ATY.s1 >= 1000) { timeCounterUnit_ATY.s1_f = 1; timeCounterUnit_ATY.s1 = 0; } /* ATY: 2s unit ***********************************************************/ if(timeCounterUnit_ATY.s2 >= 2000) { timeCounterUnit_ATY.s2_f = 1; timeCounterUnit_ATY.s2 = 0; } /* ATY: 10s unit **********************************************************/ if(timeCounterUnit_ATY.s10 >= 10000) { timeCounterUnit_ATY.s10_f = 1; timeCounterUnit_ATY.s10 = 0; } } #endif /* __TIMER_CYCLE_ATY */ #endif /* __HW_TIMER_ATY_C */ /******************************** End Of File *********************************/