C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 1 C51 COMPILER V9.60.7.0, COMPILATION OF MODULE TIMER OBJECT MODULE PLACED IN .\Objects\timer.obj COMPILER INVOKED BY: d:\Keil_v5\C51\BIN\C51.EXE ..\..\..\..\driver\src\timer.c OBJECTADVANCED OPTIMIZE(9,SPEED) BROWSE O -RDER NOAREGS MODC2 INCDIR(..\..\..\..\driver\inc;..\..\..\..\mcu;..\..\..\..\middleware\log;..\..\..\..\middleware\delay -;..\..\..\..\middleware\rf_basis) DEBUG PRINT(.\Listings\timer.lst) TABS(2) OBJECT(.\Objects\timer.obj) line level source 1 /** 2 ************************************************************************ 3 * @file timer.c 4 * @author Panchip Team 5 * @version V0.5 6 * @date 2024-04-28 7 * @brief This file provides all the timer firmware functions. 8 * @note 9 * Copyright (C) 2024 Panchip Technology Corp. All rights reserved. 10 **************************************************************************** 11 */ 12 13 #include "timer.h" 14 15 /** @addtogroup PAN262x_Std_Driver 16 * @{ 17 */ 18 19 /** @defgroup TIMER 20 * @brief TIMER driver modules 21 * @{ 22 */ 23 24 /** @defgroup TIMER_Private_Functions 25 * @{ 26 */ 27 28 /** 29 * @brief Deinitializes the TIMx peripheral registers to their default reset values. 30 * @param TIMx: where x can be 0 to 2 to select the TIM peripheral. 31 * @retval None 32 */ 33 void TIM_DeInit(TIM_IdTypeDef TIMERx) 34 { 35 1 /* Check the parameters */ 36 1 assert_param(IS_TIM_ALL_PERIPH(TIMERx)); 37 1 38 1 if (TIMERx == TIMER0) 39 1 { 40 2 TR0 = 0; 41 2 TF0 = 0; 42 2 TL0 = 0; 43 2 TH0 = 0; 44 2 TMOD &= ~0x0F; 45 2 } 46 1 else if (TIMERx == TIMER1) 47 1 { 48 2 TR1 = 0; 49 2 TF1 = 0; 50 2 TL1 = 0; 51 2 TH1 = 0; 52 2 TMOD &= ~0xF0; 53 2 } C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 2 54 1 else if (TIMERx == TIMER2) 55 1 { 56 2 T2CON = 0; 57 2 CCEN = 0; 58 2 TL2 = 0; 59 2 TH2 = 0; 60 2 TCAPCON = 0; 61 2 TCAPSTA = 0; 62 2 CRCH = 0; 63 2 CRCL = 0; 64 2 CCH1 = 0; 65 2 CCL1 = 0; 66 2 CCH2 = 0; 67 2 CCL2 = 0; 68 2 CCH3 = 0; 69 2 CCL3 = 0; 70 2 } 71 1 } 72 73 /** 74 * @brief Initializes the TIMERx Time Base Unit peripheral according to 75 * the specified parameters in the TIM_TimeBaseInitStruct. 76 * @param TIMERx: where x can be 0 to 2 to select the TIM peripheral. 77 * @param TIM_Mode: specifies the workmode of timer. 78 * This parameter can be a value of @ref TIM_ModeTypeDef 79 * @param TIM_Clk: Specifies the clock division. 80 * This parameter can be a value of @ref TIM_ClkDivTypeDef 81 * @param InitCnt: specifies the Counter register initial value. 82 * @retval None 83 */ 84 void TIM_TimeBaseInit(TIM_IdTypeDef TIMERx, TIM_ModeTypeDef TIM_Mode, TIM_ClkDivTypeDef TIM_Clk, u16 InitC -nt) 85 { 86 1 /* Check the parameters */ 87 1 assert_param(IS_TIM_ALL_PERIPH(TIMERx)); 88 1 89 1 if (TIMERx == TIMER0) 90 1 { 91 2 /* set timer0 disable */ 92 2 TR0 = 0; 93 2 94 2 /* set timer0 initial count */ 95 2 if (TIM_Mode == TIM0_Mode2_8BitAutoReload) 96 2 { 97 3 TH0 = InitCnt & 0xff; 98 3 TL0 = TH0; 99 3 } 100 2 else 101 2 { 102 3 TIM_SET_VAL(TH0, TL0, InitCnt); 103 3 } 104 2 105 2 /* set timer0 basetime enable */ 106 2 MODIFY_REG(TMOD, TMOD_TIM0_CT_Msk, 0 << TMOD_TIM0_CT_Pos); 107 2 108 2 /* set timer0 workmode */ 109 2 MODIFY_REG(TMOD, TMOD_TIM0_MODE_Msk, TIM_Mode << TMOD_TIM0_MODE_Pos); 110 2 } 111 1 else if (TIMERx == TIMER1) 112 1 { 113 2 /* set timer1 disable */ 114 2 TR1 = 0; C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 3 115 2 116 2 /* set timer1 initial count */ 117 2 if (TIM_Mode == TIM1_Mode2_8BitAutoReload) 118 2 { 119 3 TH1 = InitCnt & 0xff; 120 3 TL1 = TH1; 121 3 } 122 2 else 123 2 { 124 3 TIM_SET_VAL(TH1, TL1, InitCnt); 125 3 } 126 2 127 2 /* set timer1 basetime enable */ 128 2 MODIFY_REG(TMOD, TMOD_TIM1_CT_Msk, 0 << TMOD_TIM1_CT_Pos); 129 2 130 2 /* set timer1 workmode */ 131 2 MODIFY_REG(TMOD, TMOD_TIM1_MODE_Msk, TIM_Mode << TMOD_TIM1_MODE_Pos); 132 2 } 133 1 else if (TIMERx == TIMER2) 134 1 { 135 2 /* set timer2 disable */ 136 2 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_STOP << T2CON_T2I_Pos); 137 2 138 2 /* set timer2 initial count */ 139 2 TIM_SET_VAL(TH2, TL2, InitCnt); 140 2 141 2 /* set timer2 reload count */ 142 2 TIM_SET_VAL(CRCH, CRCL, InitCnt); 143 2 144 2 /* set timer2 clock division */ 145 2 MODIFY_REG(T2CON, T2CON_T2PS_Msk, TIM_Clk << T2CON_T2PS_Pos); 146 2 147 2 if (TIM2_Mode0_16BitAutoReload == TIM_Mode) 148 2 { 149 3 /* set timer2 reload mode0 */ 150 3 MODIFY_REG(T2CON, T2CON_T2R_Msk, TIM2_RELOAD_MODE0 << T2CON_T2R_Pos); 151 3 } 152 2 else 153 2 { 154 3 /* set timer2 reload mode0 */ 155 3 MODIFY_REG(T2CON, T2CON_T2R_Msk, TIM2_RELOAD_DISABLE << T2CON_T2R_Pos); 156 3 } 157 2 } 158 1 } 159 160 /** 161 * @brief Enables or disables TIMERx peripheral external signal count function. 162 * @param TIMERx: where x can be 0 to 2 to select the TIM peripheral. 163 * @param TIM_Mode: specifies the workmode of timer. 164 * This parameter can be a value of @ref TIM_ModeTypeDef 165 * @param NewState: new state of the TIMERx peripheral count function. 166 * This parameter can be: ENABLE or DISABLE. 167 * @retval None 168 */ 169 void TIM_ExtCntConfig(TIM_IdTypeDef TIMERx, FunctionalState NewState) 170 { 171 1 /* Check the parameters */ 172 1 assert_param(IS_TIM_ALL_PERIPH(TIMERx)); 173 1 assert_param(IS_FUNCTIONAL_STATE(NewState)); 174 1 175 1 switch (TIMERx) 176 1 { C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 4 177 2 case TIMER0: 178 2 { 179 3 if (NewState != DISABLE) 180 3 { 181 4 /* set timer0 clock source from t0 pin(falling edge) */ 182 4 MODIFY_REG(TMOD, TMOD_TIM0_CT_Msk, 1 << TMOD_TIM0_CT_Pos); 183 4 } 184 3 else 185 3 { 186 4 /* set timer0 basetime enable */ 187 4 MODIFY_REG(TMOD, TMOD_TIM0_CT_Msk, 0 << TMOD_TIM0_CT_Pos); 188 4 } 189 3 break; 190 3 } 191 2 case TIMER1: 192 2 { 193 3 if (NewState != DISABLE) 194 3 { 195 4 /* set timer1 clock source from t1 pin(falling edge) */ 196 4 MODIFY_REG(TMOD, TMOD_TIM1_CT_Msk, 1 << TMOD_TIM1_CT_Pos); 197 4 } 198 3 else 199 3 { 200 4 /* set timer1 basetime enable */ 201 4 MODIFY_REG(TMOD, TMOD_TIM1_CT_Msk, 0 << TMOD_TIM1_CT_Pos); 202 4 } 203 3 break; 204 3 } 205 2 case TIMER2: 206 2 { 207 3 /* set timer2 disable */ 208 3 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_STOP << T2CON_T2I_Pos); 209 3 210 3 if (NewState != DISABLE) 211 3 { 212 4 /* set timer2 clock source from t2 pin(falling edge) */ 213 4 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_T2PIN << T2CON_T2I_Pos); 214 4 } 215 3 else 216 3 { 217 4 /* set timer2 clock source from xtal or rch */ 218 4 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_INTERNAL << T2CON_T2I_Pos); 219 4 } 220 3 break; 221 3 } 222 2 default: 223 2 break; 224 2 } 225 1 } 226 227 /** 228 * @brief Enables or disables TIMERx peripheral clock gate. 229 * @param TIMERx: where x can be 0 to 2 to select the TIM peripheral. 230 * @param TIM_Mode: specifies the workmode of timer. 231 * This parameter can be a value of @ref TIM_ModeTypeDef 232 * @param NewState: new state of the TIMERx peripheral clock gate. 233 * This parameter can be: ENABLE or DISABLE. 234 * @retval None 235 */ 236 void TIM_GateConfig(TIM_IdTypeDef TIMERx, FunctionalState NewState) 237 { 238 1 /* Check the parameters */ C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 5 239 1 assert_param(IS_TIM_ALL_PERIPH(TIMERx)); 240 1 assert_param(IS_FUNCTIONAL_STATE(NewState)); 241 1 242 1 switch (TIMERx) 243 1 { 244 2 case TIMER0: 245 2 { 246 3 if (NewState != DISABLE) 247 3 { 248 4 /* set timer0 external clock count gate function enable */ 249 4 MODIFY_REG(TMOD, TMOD_TIM0_GATE_Msk, 1 << TMOD_TIM0_GATE_Pos); 250 4 } 251 3 else 252 3 { 253 4 /* set timer0 basetime enable */ 254 4 MODIFY_REG(TMOD, TMOD_TIM0_GATE_Msk, 0 << TMOD_TIM0_GATE_Pos); 255 4 } 256 3 break; 257 3 } 258 2 case TIMER1: 259 2 { 260 3 if (NewState != DISABLE) 261 3 { 262 4 /* set timer1 external clock gate function enable */ 263 4 MODIFY_REG(TMOD, TMOD_TIM1_GATE_Msk, 1 << TMOD_TIM1_GATE_Pos); 264 4 } 265 3 else 266 3 { 267 4 /* set timer1 basetime enable */ 268 4 MODIFY_REG(TMOD, TMOD_TIM1_GATE_Msk, 0 << TMOD_TIM1_GATE_Pos); 269 4 } 270 3 break; 271 3 } 272 2 case TIMER2: 273 2 { 274 3 /* set timer2 disable */ 275 3 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_STOP << T2CON_T2I_Pos); 276 3 277 3 if (NewState != DISABLE) 278 3 { 279 4 /* set timer1 internal clock gate function enable */ 280 4 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_GATE_CTRL << T2CON_T2I_Pos); 281 4 } 282 3 else 283 3 { 284 4 /* set timer2 clock source from xtal or rch */ 285 4 MODIFY_REG(T2CON, T2CON_T2I_Msk, TIM2_CLK_INTERNAL << T2CON_T2I_Pos); 286 4 } 287 3 break; 288 3 } 289 2 default: 290 2 break; 291 2 } 292 1 } 293 294 /** 295 * @brief Initializes the TIMER2 compare peripheral 296 * according to the specified parameters. 297 * @param TIMERx: TIMER2(only support TIMER2). 298 * @param channel: specifies the compare channel of TIMER2. 299 * This parameter can be a value of @ref TIM_ChTypeDef 300 * @param CmpMode: Specifies the compare mode of TIMER2. C51 COMPILER V9.60.7.0 TIMER 11/21/2025 17:21:11 PAGE 6 301 * This parameter can be a value of @ref TIM_CmpModeTypeDef 302 * @param CmpVal: specifies the initial value of compare register. 303 * @retval None 304 */ 305 void TIM_CmpInit(TIM_IdTypeDef TIMERx, TIM_ChTypeDef Channel, TIM_CmpModeTypeDef CmpMode, u16 CmpVal) 306 { 307 1 /* Check the parameters */ 308 1 assert_param(TIMER2 == TIMERx); 309 1 310 1 if (TIMER2 != TIMERx) 311 1 { 312 2 return; 313 2 } 314 1 315 1 /* set timer2 reload mode disable */ 316 1 // MODIFY_REG(T2CON, T2CON_T2R_Msk, TIM2_RELOAD_DISABLE<