S3C2440 進(jìn)行微秒級、毫秒級延時函數(shù)
/******************************************************
*說 明:S3C2440 進(jìn)行微秒級、毫秒級延時函數(shù)
******************************************************/
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表示進(jìn)行微秒級延時
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表示進(jìn)行毫級延時
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 }
特別是要注意rTCNTOn與rTCNTBn的關(guān)系。