/******************************************************
*作 者:温子祺
*联系方式:wenziqi@hotmail.com
*创建事件:2010-09-06
*说 明:S3C2440 进行微秒级、毫秒级延时函数
******************************************************/
MCLK:405MHz
HCLK:135MHz
PCLK:67.5MHz
在这里定时器时钟频率为PCLK/8=67.5/8=8.4375MHz
定时器0示例代码:
1 void T0DelayNus(UINT16 t)
2 {
3 rTCFG1 &= ~0x0F;
4 rTCFG1 |= 2;
5 rTCNTB0 =(S3C2440PCLK)/8/1000000; //时钟频率为PCLK/8
6 //除以1000000表示进行微秒级延时
7 rTCON &= ~0x0F;
8 rTCON |= (1<<3)|(1<<1)|(1<<0);
9 rTCON &= ~(1<<1);
10
11
12 while(t--)
13 {
14 while(rTCNTO0!=(rTCNTB0-1));
15 }
16
17 rTCON &= ~(1<<0); //停止定时器0
18
19 }
20
21 void T0DelayNms(UINT16 t)
22 {
23 rTCFG1 &= ~0x0F;
24 rTCFG1 |= 2;
25 rTCNTB0 =(S3C2440PCLK)/8/1000; //时钟频率为PCLK/8
26 //除以1000表示进行毫级延时
27 rTCON &= ~0x0F;
28 rTCON |= (1<<3)|(1<<1)|(1<<0);
29 rTCON &= ~(1<<1);
30
31 while(t--)
32 {
33 while(rTCNTO0!=(rTCNTB0-1));
34 }
35
36 rTCON &= ~(1<<0); //停止定时器0
37 }
2 {
3 rTCFG1 &= ~0x0F;
4 rTCFG1 |= 2;
5 rTCNTB0 =(S3C2440PCLK)/8/1000000; //时钟频率为PCLK/8
6 //除以1000000表示进行微秒级延时
7 rTCON &= ~0x0F;
8 rTCON |= (1<<3)|(1<<1)|(1<<0);
9 rTCON &= ~(1<<1);
10
11
12 while(t--)
13 {
14 while(rTCNTO0!=(rTCNTB0-1));
15 }
16
17 rTCON &= ~(1<<0); //停止定时器0
18
19 }
20
21 void T0DelayNms(UINT16 t)
22 {
23 rTCFG1 &= ~0x0F;
24 rTCFG1 |= 2;
25 rTCNTB0 =(S3C2440PCLK)/8/1000; //时钟频率为PCLK/8
26 //除以1000表示进行毫级延时
27 rTCON &= ~0x0F;
28 rTCON |= (1<<3)|(1<<1)|(1<<0);
29 rTCON &= ~(1<<1);
30
31 while(t--)
32 {
33 while(rTCNTO0!=(rTCNTB0-1));
34 }
35
36 rTCON &= ~(1<<0); //停止定时器0
37 }
定时器1示例代码:
转载请注明出处,谢谢!
1
void
T1DelayNus(UINT16 t)
2 {
3 rTCFG1 &= ~ ( 0x0F << 4 );
4 rTCFG1 |= ( 2 << 4 );
5 rTCNTB1 = (S3C2440PCLK) / 8 / 1000000 ;
6 rTCON &= ~ ( 0x0F << 8 );
7 rTCON |= ( 1 << 11 ) | ( 1 << 9 ) | ( 1 << 8 );
8 rTCON &= ~ ( 1 << 9 );
9
10
11 while (t -- )
12 {
13
14 while (rTCNTO1 != (rTCNTB1 - 1 ));
15
16 }
17
18 rTCON &= ~ ( 1 << 8 );
19
20 }
21
22
23 void T1DelayNms(UINT16 t)
24 {
25 rTCFG1 &= ~ ( 0x0F << 4 );
26 rTCFG1 |= ( 2 << 4 );
27 rTCNTB1 = (S3C2440PCLK) / 8 / 1000 ;
28 rTCON &= ~ ( 0x0F << 8 );
29 rTCON |= ( 1 << 11 ) | ( 1 << 9 ) | ( 1 << 8 );
30 rTCON &= ~ ( 1 << 9 );
31
32
33 while (t -- )
34 {
35 while (rTCNTO1 != (rTCNTB1 - 1 ));
36 }
37
38 rTCON &= ~ ( 1 << 8 );
39
40 }
2 {
3 rTCFG1 &= ~ ( 0x0F << 4 );
4 rTCFG1 |= ( 2 << 4 );
5 rTCNTB1 = (S3C2440PCLK) / 8 / 1000000 ;
6 rTCON &= ~ ( 0x0F << 8 );
7 rTCON |= ( 1 << 11 ) | ( 1 << 9 ) | ( 1 << 8 );
8 rTCON &= ~ ( 1 << 9 );
9
10
11 while (t -- )
12 {
13
14 while (rTCNTO1 != (rTCNTB1 - 1 ));
15
16 }
17
18 rTCON &= ~ ( 1 << 8 );
19
20 }
21
22
23 void T1DelayNms(UINT16 t)
24 {
25 rTCFG1 &= ~ ( 0x0F << 4 );
26 rTCFG1 |= ( 2 << 4 );
27 rTCNTB1 = (S3C2440PCLK) / 8 / 1000 ;
28 rTCON &= ~ ( 0x0F << 8 );
29 rTCON |= ( 1 << 11 ) | ( 1 << 9 ) | ( 1 << 8 );
30 rTCON &= ~ ( 1 << 9 );
31
32
33 while (t -- )
34 {
35 while (rTCNTO1 != (rTCNTB1 - 1 ));
36 }
37
38 rTCON &= ~ ( 1 << 8 );
39
40 }
特别是要注意rTCNTOn与rTCNTBn的关系。