test.c
Upload User: yzchenlin
Upload Date: 2022-03-09
Package Size: 712k
Code Size: 3k
Development Platform:

C/C++

  1. //=======================================
  2. // File Name : test.c
  3. // Function : C语言主程序
  4. // History : 2007 02 14
  5. //=======================================
  6. //2410各寄存器的地址
  7. // CLOCK MANAGEMENT
  8. #define rMPLLCON (*(volatile unsigned *)0x4c000004) //MPLL Control
  9. #define rCLKDIVN (*(volatile unsigned *)0x4c000014) //Clock divider control
  10. // WATCH DOG TIMER
  11. #define rWTCON (*(volatile unsigned *)0x53000000) //Watch-dog timer mode
  12. #define rWTDAT (*(volatile unsigned *)0x53000004) //Watch-dog timer data
  13. #define rWTCNT   (*(volatile unsigned *)0x53000008) //Eatch-dog timer count
  14. // I/O PORT 
  15. #define rGPBCON     (*(volatile unsigned *)0x56000010) //Port B control
  16. #define rGPBDAT     (*(volatile unsigned *)0x56000014) //Port B data
  17. #define rGPBUP      (*(volatile unsigned *)0x56000018) //Pull-up control B
  18. #define FCLK  202800000
  19. #define PCLK  (202800000/4)
  20. static int delayLoopCount = FCLK/10000/10;
  21. void Delay(int time);              
  22. void Led_Display(int data);
  23. void ChangeMPllValue(int m,int p,int s);
  24. void ChangeClockDivider(int hdivn,int pdivn);
  25. void testMain(void)
  26. {
  27.         
  28.     ChangeClockDivider(1,1);          // 1:2:4           
  29.     ChangeMPllValue(0xa1,0x3,0x1);    // FCLK=202.8MHz     
  30. Delay(0);
  31.     
  32.     while(1)
  33.     {
  34.                   
  35.         Led_Display(1);
  36.         Delay(500);   
  37.         Led_Display(0);
  38.         Delay(500);             
  39.     }
  40. }
  41. void Delay(int time)
  42. {
  43.     int i,adjust=0;
  44.     if(time==0)
  45.     {
  46.         time   = 200;
  47.         adjust = 1;
  48.         delayLoopCount = 400;
  49.         rWTCON = ((PCLK/1000000-1)<<8)|(2<<3); 
  50.         rWTDAT = 0xffff;                              //for first update
  51.         rWTCNT = 0xffff;                              //resolution=64us @any PCLK 
  52.         rWTCON = ((PCLK/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
  53.     }
  54.     for(;time>0;time--)
  55.         for(i=0;i<delayLoopCount;i++);
  56.     if(adjust==1)
  57.     {
  58.         rWTCON = ((PCLK/1000000-1)<<8)|(2<<3); //Watch-dog timer stop
  59.         i = 0xffff - rWTCNT;                     //1count->64us, 200*400 cycle runtime = 64*i us
  60.         delayLoopCount = 8000000/(i*64);         //200*400:64*i=1*x:100 -> x=80000*100/(64*i)   
  61.     }
  62. }
  63. void Delay1us(int time)
  64. {
  65. // resolution of time is 100us/100=1us.
  66. int delayLoop = FCLK/10000/10/100;
  67.     int i;
  68.     for(;time>0;time--)
  69.     for(i=0;i<delayLoop;i++);
  70.     
  71. }
  72. //========================[ BOARD LED ]=============================
  73. void Led_Display(int data)
  74. {
  75.     rGPBDAT = (rGPBDAT & ~(0xf<<7)) | ((~data & 0xf)<<7);    
  76. }
  77. //========================[ MPLL ]==================================
  78. void ChangeMPllValue(int mdiv,int pdiv,int sdiv)
  79. {
  80.     rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
  81. }
  82. //========================[ HCLK, PCLK ]============================
  83. void ChangeClockDivider(int hdivn,int pdivn)
  84. {
  85.     rCLKDIVN = (hdivn<<1) | pdivn;        
  86. }