飛凌ok6410定時器精確控制led
#define PCLK 66000000 //forS3C6410 66MHZ
// #define HCLK 133000000//forS3C6410 133MHZ 這一句沒用到,寫上可以。
#define rTCFG0 (*(volatile unsigned*)(0x7F006000))
#define rTCFG1 (*(volatileunsigned*)(0x7F006004))
#define rTCON (*(volatileunsigned*)(0x7F006008))
#define rTCNTB0 (*(volatileunsigned*)(0x7F00600C))
#define rTCMPB0 (*(volatileunsigned*)(0x7F006010))
#define rTCNTO0 (*(volatileunsigned*)(0x7F006014))
#define rTCNTB1 (*(volatileunsigned*)(0x7F006018))
#define rTCMPB1 (*(volatileunsigned*)(0x7F00601c))
#define rTCNTO1 (*(volatileunsigned*)(0x7F006020))
#define rTCNTB2 (*(volatileunsigned*)(0x7F006024))
#define rTCNTO2 (*(volatileunsigned*)(0x7F00602c))
#define rTCNTB3 (*(volatileunsigned*)(0x7F006030))
#define rTCNTO3 (*(volatileunsigned*)(0x7F006038))
#define rTCNTB4 (*(volatileunsigned*)(0x7F00603c))
#define rTCNTO4 (*(volatileunsigned*)(0x7F006040))
#definerTINT_CSTAT (*(volatileunsigned*)(0x7F006044))
void uDelay(intusec)
{
unsignedint val=(PCLK)/1000000-1; //val = 65
//configure prescalerand divider
rTCFG0&=~(0xff<<8); //0000_0000_1111_1111 TCFG0[15:8-7:0]
rTCFG0|=0<<8; //0000_0000_0000_0000 |0000_0000_1111_1111 prescalar0 = 255 timer0,timer1 的prescalar value= 255 timer2,3,4的prescalar1 value = 0
rTCFG1&=~(0xf<<8); // 0000_1111_1111 TCFG1 [7:0] =1111_1111 TCFG1[11:8] = 0000(select mux for timer2. divider value = 1 );
rTCFG1|=0<<8;
//compute :
//timerinput clock frequency = PCLK /({prescaler value + 1})/{divider value}
// timer2input clock frequency = 66M /(1)/(1)= 66M hz
//configure timercounter buffer and enable timer2
rTCNTB2=val;
rTCON&=~(0xf<<12); // 0000_1111_1111_1111
rTCON|=0xb<<12; // 1011_0000_0000_0000|0000_1111_1111_1111 = 1011_1111_1111_1111
rTCON&=~(2<<12); // 1101_1111_1111_1111&1011_1111_1111_1111 = 1001_1111_1111_1111
【// TCON(Timer control register)
//1001 : 表示 : auto-reload , starttimer2】
//TCON[15]=1 auto-reload
//TCON[14] Reserved bits
//TCON[13]=0 no operatin , =1,update TCNTB2 TCMPB2
//TCON[12]=0 stop , =1 ,start timer2
//rTCON&= 0x9fff;
while(usec--){
while(rTCNTO2 >= val>>1);
while(rTCNTO2 < val>>1);
};
}
void msDelay(inttime)
{
volatile unsigned int i,j;
for(i=0;i<2000000;i++)
for(j=0;j
評論