1 /******************************************************************************************************************************************
2 * 文件名称: SWM341_timr.c
3 * 功能说明: SWM341单片机的计数器/定时器功能驱动库
4 * 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
5 * 注意事项:
6 * 版本日期: V1.0.0      2016年1月30日
7 * 升级记录:
8 *
9 *
10 *******************************************************************************************************************************************
11 * @attention
12 *
13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
14 * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
15 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
16 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
17 * -ECTION WITH THEIR PRODUCTS.
18 *
19 * COPYRIGHT 2012 Synwit Technology
20 *******************************************************************************************************************************************/
21 #include "SWM341.h"
22 #include "SWM341_timr.h"
23 
24 
25 /******************************************************************************************************************************************
26 * 函数名称: TIMR_Init()
27 * 功能说明: TIMR定时器/计数器初始化
28 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,有效值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
29 *           uint32_t mode           对于TIMR0~4: TIMR_MODE_TIMER、TIMR_MODE_COUNTER、TIMR_MODE_OC、TIMR_MODE_IC
30 *                                   对于BTIMR0~11: TIMR_MODE_TIMER、TIMR_MODE_OC
31 *           uint16_t prediv         预分频,取值1-256
32 *           uint32_t period         定时/计数周期,取值1-16777216
33 *           uint32_t int_en         中断使能
34 * 输    出: 无
35 * 注意事项: 无
36 ******************************************************************************************************************************************/
TIMR_Init(TIMR_TypeDef * TIMRx,uint32_t mode,uint16_t prediv,uint32_t period,uint32_t int_en)37 void TIMR_Init(TIMR_TypeDef * TIMRx, uint32_t mode, uint16_t prediv, uint32_t period, uint32_t int_en)
38 {
39     if((TIMRx == TIMR0) || (TIMRx == TIMR1) || (TIMRx == TIMR2) || (TIMRx == TIMR3) || (TIMRx == TIMR4))
40     {
41         SYS->CLKEN0 |= (0x01 << SYS_CLKEN0_TIMR_Pos);
42     }
43     else
44     {
45         SYS->CLKEN1 |= (0x01 << SYS_CLKEN1_BTIMR_Pos);
46     }
47 
48     TIMR_Stop(TIMRx);   //一些关键寄存器只能在定时器停止时设置
49 
50     TIMRx->CR &= ~(TIMR_CR_MODE_Msk | TIMR_CR_CLKSRC_Msk);
51     TIMRx->CR |= (mode << TIMR_CR_CLKSRC_Pos);
52 
53     TIMRx->PREDIV = prediv - 1;
54 
55     TIMRx->LOAD = period - 1;
56 
57     TIMRx->IF = (1 << TIMR_IF_TO_Pos);      //清除中断标志
58     if(int_en) TIMRx->IE |=  (1 << TIMR_IE_TO_Pos);
59     else       TIMRx->IE &= ~(1 << TIMR_IE_TO_Pos);
60 
61     switch((uint32_t)TIMRx)
62     {
63     case ((uint32_t)TIMR0):
64         if(int_en) NVIC_EnableIRQ(TIMR0_IRQn);
65         break;
66 
67     case ((uint32_t)TIMR1):
68         if(int_en) NVIC_EnableIRQ(TIMR1_IRQn);
69         break;
70 
71     case ((uint32_t)TIMR2):
72         if(int_en) NVIC_EnableIRQ(TIMR2_IRQn);
73         break;
74 
75     case ((uint32_t)TIMR3):
76         if(int_en) NVIC_EnableIRQ(TIMR3_IRQn);
77         break;
78 
79     case ((uint32_t)TIMR4):
80         if(int_en) NVIC_EnableIRQ(TIMR4_IRQn);
81         break;
82 
83     case ((uint32_t)BTIMR0):
84         if(int_en) NVIC_EnableIRQ(BTIMR0_IRQn);
85         break;
86 
87     case ((uint32_t)BTIMR1):
88         if(int_en) NVIC_EnableIRQ(BTIMR1_IRQn);
89         break;
90 
91     case ((uint32_t)BTIMR2):
92         if(int_en) NVIC_EnableIRQ(BTIMR2_IRQn);
93         break;
94 
95     case ((uint32_t)BTIMR3):
96         if(int_en) NVIC_EnableIRQ(BTIMR3_IRQn);
97         break;
98 
99     case ((uint32_t)BTIMR4):
100         if(int_en) NVIC_EnableIRQ(BTIMR4_IRQn);
101         break;
102 
103     case ((uint32_t)BTIMR5):
104         if(int_en) NVIC_EnableIRQ(BTIMR5_IRQn);
105         break;
106 
107     case ((uint32_t)BTIMR6):
108         if(int_en) NVIC_EnableIRQ(BTIMR6_IRQn);
109         break;
110 
111     case ((uint32_t)BTIMR7):
112         if(int_en) NVIC_EnableIRQ(BTIMR7_IRQn);
113         break;
114 
115     case ((uint32_t)BTIMR8):
116         if(int_en) NVIC_EnableIRQ(BTIMR8_IRQn);
117         break;
118 
119     case ((uint32_t)BTIMR9):
120         if(int_en) NVIC_EnableIRQ(BTIMR9_IRQn);
121         break;
122 
123     case ((uint32_t)BTIMR10):
124         if(int_en) NVIC_EnableIRQ(BTIMR10_IRQn);
125         break;
126 
127     case ((uint32_t)BTIMR11):
128         if(int_en) NVIC_EnableIRQ(BTIMR11_IRQn);
129         break;
130     }
131 }
132 
133 /******************************************************************************************************************************************
134 * 函数名称: TIMR_Start()
135 * 功能说明: 启动定时器,从初始值开始计时/计数
136 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
137 * 输    出: 无
138 * 注意事项: 无
139 ******************************************************************************************************************************************/
TIMR_Start(TIMR_TypeDef * TIMRx)140 void TIMR_Start(TIMR_TypeDef * TIMRx)
141 {
142     switch((uint32_t)TIMRx)
143     {
144     case ((uint32_t)TIMR0):
145         TIMRG->EN |= (1 << TIMRG_EN_TIMR0_Pos);
146         break;
147 
148     case ((uint32_t)TIMR1):
149         TIMRG->EN |= (1 << TIMRG_EN_TIMR1_Pos);
150         break;
151 
152     case ((uint32_t)TIMR2):
153         TIMRG->EN |= (1 << TIMRG_EN_TIMR2_Pos);
154         break;
155 
156     case ((uint32_t)TIMR3):
157         TIMRG->EN |= (1 << TIMRG_EN_TIMR3_Pos);
158         break;
159 
160     case ((uint32_t)TIMR4):
161         TIMRG->EN |= (1 << TIMRG_EN_TIMR4_Pos);
162         break;
163 
164     case ((uint32_t)BTIMR0):
165         BTIMRG->EN |= (1 << TIMRG_EN_TIMR0_Pos);
166         break;
167 
168     case ((uint32_t)BTIMR1):
169         BTIMRG->EN |= (1 << TIMRG_EN_TIMR1_Pos);
170         break;
171 
172     case ((uint32_t)BTIMR2):
173         BTIMRG->EN |= (1 << TIMRG_EN_TIMR2_Pos);
174         break;
175 
176     case ((uint32_t)BTIMR3):
177         BTIMRG->EN |= (1 << TIMRG_EN_TIMR3_Pos);
178         break;
179 
180     case ((uint32_t)BTIMR4):
181         BTIMRG->EN |= (1 << TIMRG_EN_TIMR4_Pos);
182         break;
183 
184     case ((uint32_t)BTIMR5):
185         BTIMRG->EN |= (1 << TIMRG_EN_TIMR5_Pos);
186         break;
187 
188     case ((uint32_t)BTIMR6):
189         BTIMRG->EN |= (1 << TIMRG_EN_TIMR6_Pos);
190         break;
191 
192     case ((uint32_t)BTIMR7):
193         BTIMRG->EN |= (1 << TIMRG_EN_TIMR7_Pos);
194         break;
195 
196     case ((uint32_t)BTIMR8):
197         BTIMRG->EN |= (1 << TIMRG_EN_TIMR8_Pos);
198         break;
199 
200     case ((uint32_t)BTIMR9):
201         BTIMRG->EN |= (1 << TIMRG_EN_TIMR9_Pos);
202         break;
203 
204     case ((uint32_t)BTIMR10):
205         BTIMRG->EN |= (1 << TIMRG_EN_TIMR10_Pos);
206         break;
207 
208     case ((uint32_t)BTIMR11):
209         BTIMRG->EN |= (1 << TIMRG_EN_TIMR11_Pos);
210         break;
211     }
212 }
213 
214 /******************************************************************************************************************************************
215 * 函数名称: TIMR_Stop()
216 * 功能说明: 停止定时器
217 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
218 * 输    出: 无
219 * 注意事项: 无
220 ******************************************************************************************************************************************/
TIMR_Stop(TIMR_TypeDef * TIMRx)221 void TIMR_Stop(TIMR_TypeDef * TIMRx)
222 {
223     switch((uint32_t)TIMRx)
224     {
225     case ((uint32_t)TIMR0):
226         TIMRG->EN &= ~(1 << TIMRG_EN_TIMR0_Pos);
227         break;
228 
229     case ((uint32_t)TIMR1):
230         TIMRG->EN &= ~(1 << TIMRG_EN_TIMR1_Pos);
231         break;
232 
233     case ((uint32_t)TIMR2):
234         TIMRG->EN &= ~(1 << TIMRG_EN_TIMR2_Pos);
235         break;
236 
237     case ((uint32_t)TIMR3):
238         TIMRG->EN &= ~(1 << TIMRG_EN_TIMR3_Pos);
239         break;
240 
241     case ((uint32_t)TIMR4):
242         TIMRG->EN &= ~(1 << TIMRG_EN_TIMR4_Pos);
243         break;
244 
245     case ((uint32_t)BTIMR0):
246         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR0_Pos);
247         break;
248 
249     case ((uint32_t)BTIMR1):
250         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR1_Pos);
251         break;
252 
253     case ((uint32_t)BTIMR2):
254         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR2_Pos);
255         break;
256 
257     case ((uint32_t)BTIMR3):
258         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR3_Pos);
259         break;
260 
261     case ((uint32_t)BTIMR4):
262         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR4_Pos);
263         break;
264 
265     case ((uint32_t)BTIMR5):
266         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR5_Pos);
267         break;
268 
269     case ((uint32_t)BTIMR6):
270         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR6_Pos);
271         break;
272 
273     case ((uint32_t)BTIMR7):
274         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR7_Pos);
275         break;
276 
277     case ((uint32_t)BTIMR8):
278         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR8_Pos);
279         break;
280 
281     case ((uint32_t)BTIMR9):
282         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR9_Pos);
283         break;
284 
285     case ((uint32_t)BTIMR10):
286         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR10_Pos);
287         break;
288 
289     case ((uint32_t)BTIMR11):
290         BTIMRG->EN &= ~(1 << TIMRG_EN_TIMR11_Pos);
291         break;
292     }
293 }
294 
295 /******************************************************************************************************************************************
296 * 函数名称: TIMR_Halt()
297 * 功能说明: 暂停定时器,计数值保持不变
298 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
299 * 输    出: 无
300 * 注意事项: 无
301 ******************************************************************************************************************************************/
TIMR_Halt(TIMR_TypeDef * TIMRx)302 void TIMR_Halt(TIMR_TypeDef * TIMRx)
303 {
304     TIMRx->HALT = 1;
305 }
306 
307 /******************************************************************************************************************************************
308 * 函数名称: TIMR_Resume()
309 * 功能说明: 恢复定时器,从暂停处继续计数
310 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
311 * 输    出: 无
312 * 注意事项: 无
313 ******************************************************************************************************************************************/
TIMR_Resume(TIMR_TypeDef * TIMRx)314 void TIMR_Resume(TIMR_TypeDef * TIMRx)
315 {
316     TIMRx->HALT = 0;
317 }
318 
319 /******************************************************************************************************************************************
320 * 函数名称: TIMR_GetCurValue()
321 * 功能说明: 获取当前计数值
322 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
323 * 输    出: uint32_t              当前计数值
324 * 注意事项: 无
325 ******************************************************************************************************************************************/
TIMR_GetCurValue(TIMR_TypeDef * TIMRx)326 uint32_t TIMR_GetCurValue(TIMR_TypeDef * TIMRx)
327 {
328     return TIMRx->VALUE;
329 }
330 
331 /******************************************************************************************************************************************
332 * 函数名称: TIMR_INTEn()
333 * 功能说明: 使能中断
334 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
335 * 输    出: 无
336 * 注意事项: 无
337 ******************************************************************************************************************************************/
TIMR_INTEn(TIMR_TypeDef * TIMRx)338 void TIMR_INTEn(TIMR_TypeDef * TIMRx)
339 {
340     TIMRx->IE |= (1 << TIMR_IE_TO_Pos);
341 }
342 
343 /******************************************************************************************************************************************
344 * 函数名称: TIMR_INTDis()
345 * 功能说明: 禁能中断
346 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
347 * 输    出: 无
348 * 注意事项: 无
349 ******************************************************************************************************************************************/
TIMR_INTDis(TIMR_TypeDef * TIMRx)350 void TIMR_INTDis(TIMR_TypeDef * TIMRx)
351 {
352     TIMRx->IE &= ~(1 << TIMR_IE_TO_Pos);
353 }
354 
355 /******************************************************************************************************************************************
356 * 函数名称: TIMR_INTClr()
357 * 功能说明: 清除中断标志
358 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
359 * 输    出: 无
360 * 注意事项: 无
361 ******************************************************************************************************************************************/
TIMR_INTClr(TIMR_TypeDef * TIMRx)362 void TIMR_INTClr(TIMR_TypeDef * TIMRx)
363 {
364     TIMRx->IF = (1 << TIMR_IF_TO_Pos);
365 }
366 
367 /******************************************************************************************************************************************
368 * 函数名称: TIMR_INTStat()
369 * 功能说明: 获取中断状态
370 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
371 * 输    出: uint32_t              0 TIMRx未产生中断    1 TIMRx产生了中断
372 * 注意事项: 无
373 ******************************************************************************************************************************************/
TIMR_INTStat(TIMR_TypeDef * TIMRx)374 uint32_t TIMR_INTStat(TIMR_TypeDef * TIMRx)
375 {
376     return (TIMRx->IF & TIMR_IF_TO_Msk) ? 1 : 0;
377 }
378 
379 
380 /******************************************************************************************************************************************
381 * 函数名称: TIMR_OC_Init()
382 * 功能说明: 输出比较功能初始化
383 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
384 *           uint32_t match          当计数器的值递减到match时引脚输出电平翻转
385 *           uint32_t match_int_en   当计数器的值递减到match时是否产生中断
386 *           uint32_t init_lvl       初始输出电平
387 * 输    出: 无
388 * 注意事项: 无
389 ******************************************************************************************************************************************/
TIMR_OC_Init(TIMR_TypeDef * TIMRx,uint32_t match,uint32_t match_int_en,uint32_t init_lvl)390 void TIMR_OC_Init(TIMR_TypeDef * TIMRx, uint32_t match, uint32_t match_int_en, uint32_t init_lvl)
391 {
392     TIMRx->OCMAT = match;
393     if(init_lvl) TIMRx->OCCR |=  (1 << TIMR_OCCR_INITLVL_Pos);
394     else         TIMRx->OCCR &= ~(1 << TIMR_OCCR_INITLVL_Pos);
395 
396     TIMRx->IF = (1 << TIMR_IF_OC0_Pos);     //清除中断标志
397     if(match_int_en) TIMRx->IE |=  (1 << TIMR_IE_OC0_Pos);
398     else             TIMRx->IE &= ~(1 << TIMR_IE_OC0_Pos);
399 
400     switch((uint32_t)TIMRx)
401     {
402     case ((uint32_t)TIMR0):
403         if(match_int_en) NVIC_EnableIRQ(TIMR0_IRQn);
404         break;
405 
406     case ((uint32_t)TIMR1):
407         if(match_int_en) NVIC_EnableIRQ(TIMR1_IRQn);
408         break;
409 
410     case ((uint32_t)TIMR2):
411         if(match_int_en) NVIC_EnableIRQ(TIMR2_IRQn);
412         break;
413 
414     case ((uint32_t)TIMR3):
415         if(match_int_en) NVIC_EnableIRQ(TIMR3_IRQn);
416         break;
417 
418     case ((uint32_t)TIMR4):
419         if(match_int_en) NVIC_EnableIRQ(TIMR4_IRQn);
420         break;
421 
422     case ((uint32_t)BTIMR0):
423         if(match_int_en) NVIC_EnableIRQ(BTIMR0_IRQn);
424         break;
425 
426     case ((uint32_t)BTIMR1):
427         if(match_int_en) NVIC_EnableIRQ(BTIMR1_IRQn);
428         break;
429 
430     case ((uint32_t)BTIMR2):
431         if(match_int_en) NVIC_EnableIRQ(BTIMR2_IRQn);
432         break;
433 
434     case ((uint32_t)BTIMR3):
435         if(match_int_en) NVIC_EnableIRQ(BTIMR3_IRQn);
436         break;
437 
438     case ((uint32_t)BTIMR4):
439         if(match_int_en) NVIC_EnableIRQ(BTIMR4_IRQn);
440         break;
441 
442     case ((uint32_t)BTIMR5):
443         if(match_int_en) NVIC_EnableIRQ(BTIMR5_IRQn);
444         break;
445 
446     case ((uint32_t)BTIMR6):
447         if(match_int_en) NVIC_EnableIRQ(BTIMR6_IRQn);
448         break;
449 
450     case ((uint32_t)BTIMR7):
451         if(match_int_en) NVIC_EnableIRQ(BTIMR7_IRQn);
452         break;
453 
454     case ((uint32_t)BTIMR8):
455         if(match_int_en) NVIC_EnableIRQ(BTIMR8_IRQn);
456         break;
457 
458     case ((uint32_t)BTIMR9):
459         if(match_int_en) NVIC_EnableIRQ(BTIMR9_IRQn);
460         break;
461 
462     case ((uint32_t)BTIMR10):
463         if(match_int_en) NVIC_EnableIRQ(BTIMR10_IRQn);
464         break;
465 
466     case ((uint32_t)BTIMR11):
467         if(match_int_en) NVIC_EnableIRQ(BTIMR11_IRQn);
468         break;
469     }
470 }
471 
472 /******************************************************************************************************************************************
473 * 函数名称: TIMR_OC_OutputEn()
474 * 功能说明: 使能输出比较功能的波形输出
475 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
476 * 输    出: 无
477 * 注意事项: 无
478 ******************************************************************************************************************************************/
TIMR_OC_OutputEn(TIMR_TypeDef * TIMRx)479 void TIMR_OC_OutputEn(TIMR_TypeDef * TIMRx)
480 {
481     TIMRx->OCCR &= ~(TIMR_OCCR_FORCEEN_Pos);
482 }
483 
484 /******************************************************************************************************************************************
485 * 函数名称: TIMR_OC_OutputDis()
486 * 功能说明: 禁止输出比较功能的波形输出,且让输出比较功能引脚保持level电平
487 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
488 *           uint32_t level          禁止输出波形后在引脚上保持的电平
489 * 输    出: 无
490 * 注意事项: 无
491 ******************************************************************************************************************************************/
TIMR_OC_OutputDis(TIMR_TypeDef * TIMRx,uint32_t level)492 void TIMR_OC_OutputDis(TIMR_TypeDef * TIMRx, uint32_t level)
493 {
494     if(level) TIMRx->OCCR |=  (1 << TIMR_OCCR_FORCELVL_Pos);
495     else      TIMRx->OCCR &= ~(1 << TIMR_OCCR_FORCELVL_Pos);
496 
497     TIMRx->OCCR |= (TIMR_OCCR_FORCEEN_Pos);
498 }
499 
500 /******************************************************************************************************************************************
501 * 函数名称: TIMR_OC_SetMatch()
502 * 功能说明: 设置输出比较功能的比较值
503 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
504 *           uint32_t match          输出比较功能的比较值
505 * 输    出: 无
506 * 注意事项: 无
507 ******************************************************************************************************************************************/
TIMR_OC_SetMatch(TIMR_TypeDef * TIMRx,uint32_t match)508 void TIMR_OC_SetMatch(TIMR_TypeDef * TIMRx, uint32_t match)
509 {
510     TIMRx->OCMAT = match;
511 }
512 
513 /******************************************************************************************************************************************
514 * 函数名称: TIMR_OC_GetMatch()
515 * 功能说明: 获取输出比较功能的比较值
516 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
517 * 输    出: uint32_t              输出比较功能的比较值
518 * 注意事项: 无
519 ******************************************************************************************************************************************/
TIMR_OC_GetMatch(TIMR_TypeDef * TIMRx)520 uint32_t TIMR_OC_GetMatch(TIMR_TypeDef * TIMRx)
521 {
522     return TIMRx->OCMAT;
523 }
524 
525 /******************************************************************************************************************************************
526 * 函数名称: TIMR_OC_INTEn()
527 * 功能说明: 使能输出比较中断
528 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
529 * 输    出: 无
530 * 注意事项: 无
531 ******************************************************************************************************************************************/
TIMR_OC_INTEn(TIMR_TypeDef * TIMRx)532 void TIMR_OC_INTEn(TIMR_TypeDef * TIMRx)
533 {
534     TIMRx->IE |= (1 << TIMR_IE_OC0_Pos);
535 }
536 
537 /******************************************************************************************************************************************
538 * 函数名称: TIMR_OC_INTDis()
539 * 功能说明: 禁能输出比较中断
540 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
541 * 输    出: 无
542 * 注意事项: 无
543 ******************************************************************************************************************************************/
TIMR_OC_INTDis(TIMR_TypeDef * TIMRx)544 void TIMR_OC_INTDis(TIMR_TypeDef * TIMRx)
545 {
546     TIMRx->IE &= ~(1 << TIMR_IE_OC0_Pos);
547 }
548 
549 /******************************************************************************************************************************************
550 * 函数名称: TIMR_OC_INTClr()
551 * 功能说明: 清除输出比较中断标志
552 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
553 * 输    出: 无
554 * 注意事项: 无
555 ******************************************************************************************************************************************/
TIMR_OC_INTClr(TIMR_TypeDef * TIMRx)556 void TIMR_OC_INTClr(TIMR_TypeDef * TIMRx)
557 {
558     TIMRx->IF = (1 << TIMR_IF_OC0_Pos);
559 }
560 
561 /******************************************************************************************************************************************
562 * 函数名称: TIMR_OC_INTStat()
563 * 功能说明: 获取输出比较中断状态
564 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、BTIMR0、BTIMR1、...、BTIMR11
565 * 输    出: uint32_t              0 输出比较match未发生   1 输出比较match发生
566 * 注意事项: 无
567 ******************************************************************************************************************************************/
TIMR_OC_INTStat(TIMR_TypeDef * TIMRx)568 uint32_t TIMR_OC_INTStat(TIMR_TypeDef * TIMRx)
569 {
570     return (TIMRx->IF & TIMR_IF_OC0_Msk) ? 1 : 0;
571 }
572 
573 /******************************************************************************************************************************************
574 * 函数名称: TIMR_IC_Init()
575 * 功能说明: 输入捕获功能初始化
576 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
577 *           uint32_t captureH_int_en    测量高电平长度完成中断使能
578 *           uint32_t captureL_int_en    测量低电平长度完成中断使能
579 * 输    出: 无
580 * 注意事项: 无
581 ******************************************************************************************************************************************/
TIMR_IC_Init(TIMR_TypeDef * TIMRx,uint32_t captureH_int_en,uint32_t captureL_int_en)582 void TIMR_IC_Init(TIMR_TypeDef * TIMRx, uint32_t captureH_int_en, uint32_t captureL_int_en)
583 {
584     TIMRx->IF = (TIMR_IF_ICR_Msk | TIMR_IF_ICF_Msk);
585     if(captureH_int_en) TIMRx->IE |=  (1 << TIMR_IE_ICF_Pos);
586     else                TIMRx->IE &= ~(1 << TIMR_IE_ICF_Pos);
587     if(captureL_int_en) TIMRx->IE |=  (1 << TIMR_IE_ICR_Pos);
588     else                TIMRx->IE &= ~(1 << TIMR_IE_ICR_Pos);
589 
590     switch((uint32_t)TIMRx)
591     {
592     case ((uint32_t)TIMR0):
593         if(captureH_int_en | captureL_int_en) NVIC_EnableIRQ(TIMR0_IRQn);
594         break;
595 
596     case ((uint32_t)TIMR1):
597         if(captureH_int_en | captureL_int_en) NVIC_EnableIRQ(TIMR1_IRQn);
598         break;
599 
600     case ((uint32_t)TIMR2):
601         if(captureH_int_en | captureL_int_en) NVIC_EnableIRQ(TIMR2_IRQn);
602         break;
603 
604     case ((uint32_t)TIMR3):
605         if(captureH_int_en | captureL_int_en) NVIC_EnableIRQ(TIMR3_IRQn);
606         break;
607 
608     case ((uint32_t)TIMR4):
609         if(captureH_int_en | captureL_int_en) NVIC_EnableIRQ(TIMR4_IRQn);
610         break;
611     }
612 }
613 
614 /******************************************************************************************************************************************
615 * 函数名称: TIMR_IC_GetCaptureH()
616 * 功能说明: 获取高电平长度测量结果
617 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
618 * 输    出: uint32_t              高电平长度测量结果
619 * 注意事项: 无
620 ******************************************************************************************************************************************/
TIMR_IC_GetCaptureH(TIMR_TypeDef * TIMRx)621 uint32_t TIMR_IC_GetCaptureH(TIMR_TypeDef * TIMRx)
622 {
623     return TIMRx->ICHIGH;
624 }
625 
626 /******************************************************************************************************************************************
627 * 函数名称: TIMR_IC_GetCaptureL()
628 * 功能说明: 获取低电平长度测量结果
629 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
630 * 输    出: uint32_t              低电平长度测量结果
631 * 注意事项: 无
632 ******************************************************************************************************************************************/
TIMR_IC_GetCaptureL(TIMR_TypeDef * TIMRx)633 uint32_t TIMR_IC_GetCaptureL(TIMR_TypeDef * TIMRx)
634 {
635     return TIMRx->ICLOW;
636 }
637 
638 /******************************************************************************************************************************************
639 * 函数名称: TIMR_IC_CaptureH_INTEn()
640 * 功能说明: 使能输入捕获高电平长度测量完成中断
641 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
642 * 输    出: 无
643 * 注意事项: 无
644 ******************************************************************************************************************************************/
TIMR_IC_CaptureH_INTEn(TIMR_TypeDef * TIMRx)645 void TIMR_IC_CaptureH_INTEn(TIMR_TypeDef * TIMRx)
646 {
647     TIMRx->IE |= (1 << TIMR_IE_ICF_Pos);
648 }
649 
650 /******************************************************************************************************************************************
651 * 函数名称: TIMR_IC_CaptureH_INTDis()
652 * 功能说明: 禁能输入捕获高电平长度测量完成中断
653 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
654 * 输    出: 无
655 * 注意事项: 无
656 ******************************************************************************************************************************************/
TIMR_IC_CaptureH_INTDis(TIMR_TypeDef * TIMRx)657 void TIMR_IC_CaptureH_INTDis(TIMR_TypeDef * TIMRx)
658 {
659     TIMRx->IE &= ~(1 << TIMR_IE_ICF_Pos);
660 }
661 
662 /******************************************************************************************************************************************
663 * 函数名称: TIMR_IC_CaptureH_INTClr()
664 * 功能说明: 清除输入捕获高电平长度测量完成中断标志
665 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
666 * 输    出: 无
667 * 注意事项: 无
668 ******************************************************************************************************************************************/
TIMR_IC_CaptureH_INTClr(TIMR_TypeDef * TIMRx)669 void TIMR_IC_CaptureH_INTClr(TIMR_TypeDef * TIMRx)
670 {
671     TIMRx->IF = (1 << TIMR_IF_ICF_Pos);
672 }
673 
674 /******************************************************************************************************************************************
675 * 函数名称: TIMR_IC_CaptureH_INTStat()
676 * 功能说明: 获取输入捕获高电平长度测量完成中断状态
677 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
678 * 输    出: uint32_t              0 高电平长度测量未完成    1 高电平长度测量完成
679 * 注意事项: 无
680 ******************************************************************************************************************************************/
TIMR_IC_CaptureH_INTStat(TIMR_TypeDef * TIMRx)681 uint32_t TIMR_IC_CaptureH_INTStat(TIMR_TypeDef * TIMRx)
682 {
683     return (TIMRx->IF & TIMR_IF_ICF_Msk) ? 1 : 0;
684 }
685 
686 /******************************************************************************************************************************************
687 * 函数名称: TIMR_IC_CaptureL_INTEn()
688 * 功能说明: 使能输入捕获低电平长度测量完成中断
689 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
690 * 输    出: 无
691 * 注意事项: 无
692 ******************************************************************************************************************************************/
TIMR_IC_CaptureL_INTEn(TIMR_TypeDef * TIMRx)693 void TIMR_IC_CaptureL_INTEn(TIMR_TypeDef * TIMRx)
694 {
695     TIMRx->IE |= (1 << TIMR_IE_ICR_Pos);
696 }
697 
698 /******************************************************************************************************************************************
699 * 函数名称: TIMR_IC_CaptureL_INTDis()
700 * 功能说明: 禁能输入捕获低电平长度测量完成中断
701 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
702 * 输    出: 无
703 * 注意事项: 无
704 ******************************************************************************************************************************************/
TIMR_IC_CaptureL_INTDis(TIMR_TypeDef * TIMRx)705 void TIMR_IC_CaptureL_INTDis(TIMR_TypeDef * TIMRx)
706 {
707     TIMRx->IE &= ~(1 << TIMR_IE_ICR_Pos);
708 }
709 
710 /******************************************************************************************************************************************
711 * 函数名称: TIMR_IC_CaptureL_INTClr()
712 * 功能说明: 清除输入捕获低电平长度测量完成中断标志
713 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
714 * 输    出: 无
715 * 注意事项: 无
716 ******************************************************************************************************************************************/
TIMR_IC_CaptureL_INTClr(TIMR_TypeDef * TIMRx)717 void TIMR_IC_CaptureL_INTClr(TIMR_TypeDef * TIMRx)
718 {
719     TIMRx->IF = (1 << TIMR_IF_ICR_Pos);
720 }
721 
722 /******************************************************************************************************************************************
723 * 函数名称: TIMR_IC_CaptureL_INTStat()
724 * 功能说明: 获取输入捕获低电平长度测量完成中断状态
725 * 输    入: TIMR_TypeDef * TIMRx  指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4
726 * 输    出: uint32_t              0 低电平长度测量未完成    1 低电平长度测量完成
727 * 注意事项: 无
728 ******************************************************************************************************************************************/
TIMR_IC_CaptureL_INTStat(TIMR_TypeDef * TIMRx)729 uint32_t TIMR_IC_CaptureL_INTStat(TIMR_TypeDef * TIMRx)
730 {
731     return (TIMRx->IF & TIMR_IF_ICR_Msk) ? 1 : 0;
732 }
733