-
Notifications
You must be signed in to change notification settings - Fork 2
/
delay.c
71 lines (57 loc) · 1.52 KB
/
delay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Includes ------------------------------------------------------------------*/
#include "config.h"
/**
* @brief delay for some time in ms unit
* @caller auto_test
* @param n_ms is how many ms of time to delay
* @retval None
*/
void delay_ms(u16 n_ms)
{
/* Init TIMER 4 */
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
/* Init TIMER 4 prescaler: / (2^6) = /64 */
TIM2->PSCR = 6;
/* HSI div by 1 --> Auto-Reload value: 16M / 64 = 1/4M, 1/4M / 1k = 250*/
TIM2->ARRH = 0;
TIM2->ARRL = 250;
/* Counter value: 2, to compensate the initialization of TIMER*/
TIM2->CNTRH = 0;
TIM2->CNTRL = 2;
/* clear update flag */
TIM2->SR1 &= (u8)(~0x01);
/* Enable Counter */
TIM2->CR1 |= 0x01;
while(n_ms--)
{
while((TIM2->SR1 & 0x01) == 0) ;
TIM2->SR1 &= (u8)(~0x01);
}
/* Disable Counter */
TIM2->CR1 &= (u8)(~0x01);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
}
/**
* @brief delay for some time in 10us unit(partial accurate)
* @caller auto_test
* @param n_10us is how many 10us of time to delay
* @retval None
*/
void delay_10us(u8 n_10us)
{
/* Counter value: 10, to compensate the initialization of TIMER */
TIM2->CNTRH = 0;
TIM2->CNTRL = 10;
/* clear update flag */
TIM2->SR1 &= (u8)(~0x01);
/* Enable Counter */
TIM2->CR1 |= 0x01;
while(n_10us--)
{
while((TIM2->SR1 & 0x01) == 0) ;
TIM2->SR1 &= 0xFE;
}
/* Disable Counter */
TIM2->CR1 &= 0xFE;
}
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/