智能车制作

标题: 【MK60DN512VLQ10 】倍频问题 [打印本页]

作者: [JACE]    时间: 2015-1-31 14:32
标题: 【MK60DN512VLQ10 】倍频问题
本帖最后由 [JACE] 于 2015-1-31 14:34 编辑

【MK60DN512VLQ10 】倍频问题

要知道带Z的芯片似乎停产了?
而且带Z的芯片与不带Z的倍频操作是有区别的!!!
不知道你们发现没有!!!
以下倍频方案由官方提供的程序改来的,
但死活上不了160M,求大神解答~~~


这是运行结果:

[attach]74208[/attach]



  1. int pll_init(int crystal_val,                   //外部时钟输入频率
  2.              unsigned char hgo_val,             //For Low Power
  3.              unsigned char erefs_val,           //选择内/外时钟
  4.              signed char prdiv_val,             //分频系数
  5.              signed char vdiv_val,              //倍频系数
  6.              unsigned char mcgout_select)       // Use or not the output from this PLL as the MCGOUT
  7. {
  8.   unsigned char frdiv_val;
  9.   unsigned char temp_reg;
  10.   unsigned char prdiv, vdiv;
  11.   short i;
  12.   int ref_freq;
  13.   int pll_freq;

  14.   // check if in FEI mode
  15.   if (!((((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) == 0x0) && // check CLKS mux has selcted FLL output
  16.       (MCG_S & MCG_S_IREFST_MASK) &&                                  // check FLL ref is internal ref clk
  17.       (!(MCG_S & MCG_S_PLLST_MASK))))                                 // check PLLS mux has selected FLL
  18.   {
  19.     return 0x1;                                                     // return error code
  20.   }

  21.   // check external frequency is less than the maximum frequency
  22.   if  (crystal_val > 50000000) {return 0x21;}

  23.   // check crystal frequency is within spec. if crystal osc is being used as PLL ref
  24.   if (erefs_val)
  25.   {
  26.     if ((crystal_val < 3000000) || (crystal_val > 32000000)) {return 0x22;} // return 1 if one of the available crystal options is not available
  27.   }

  28.   // make sure HGO will never be greater than 1. Could return an error instead if desired.
  29.   if (hgo_val > 0)
  30.   {
  31.     hgo_val = 1; // force hgo_val to 1 if > 0
  32.   }

  33.   // Check PLL divider settings are within spec.
  34.   if ((prdiv_val < 1) || (prdiv_val > 25)) {return 0x41;}
  35.   if ((vdiv_val < 24) || (vdiv_val > 55)) {return 0x42;}

  36.   // Check PLL reference clock frequency is within spec(2M - 4M).
  37.   ref_freq = crystal_val / prdiv_val;
  38.   if ((ref_freq < 2000000) || (ref_freq > 4000000)) {return 0x43;}

  39.   // Check PLL output frequency is within spec(现改为48M-250M).
  40.   pll_freq = (crystal_val / prdiv_val) * vdiv_val;
  41.   //if ((pll_freq < 48000000) || (pll_freq > 100000000)) {return 0x45;}
  42.   if ((pll_freq < 48000000) || (pll_freq > 250000000)) {return 0x45;}

  43.   // configure the MCG_C2 register
  44.   // the RANGE value is determined by the external frequency. Since the RANGE parameter affects the FRDIV divide value
  45.   // it still needs to be set correctly even if the oscillator is not being used
  46.       
  47.   temp_reg = MCG_C2;
  48.   temp_reg &= ~(MCG_C2_RANGE0_MASK | MCG_C2_HGO0_MASK | MCG_C2_EREFS0_MASK); // clear fields before writing new values
  49.    
  50.   if (crystal_val <= 8000000) //高频 设置
  51.   {
  52.     temp_reg |= (MCG_C2_RANGE0(1) | (hgo_val << MCG_C2_HGO0_SHIFT) | (erefs_val << MCG_C2_EREFS0_SHIFT));
  53.   }
  54.   else                        //超高频 设置
  55.   {
  56.     temp_reg |= (MCG_C2_RANGE0(2) | (hgo_val << MCG_C2_HGO0_SHIFT) | (erefs_val << MCG_C2_EREFS0_SHIFT));
  57.   }
  58.   MCG_C2 = temp_reg;
  59.   
  60.   // determine FRDIV based on reference clock frequency
  61.   // since the external frequency has already been checked only the maximum frequency for each FRDIV value needs to be compared here.
  62.   if (crystal_val <= 1250000) {frdiv_val = 0;}
  63.   else if (crystal_val <= 2500000) {frdiv_val = 1;}
  64.   else if (crystal_val <= 5000000) {frdiv_val = 2;}
  65.   else if (crystal_val <= 10000000) {frdiv_val = 3;}
  66.   else if (crystal_val <= 20000000) {frdiv_val = 4;}
  67.   else {frdiv_val = 5;}

  68.   // Select external oscillator and Reference Divider and clear IREFS to start ext osc
  69.   // If IRCLK is required it must be enabled outside of this driver, existing state will be maintained
  70.   // CLKS=2, FRDIV=frdiv_val, IREFS=0, IRCLKEN=0, IREFSTEN=0
  71.   temp_reg = MCG_C1;
  72.   temp_reg &= ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK); // Clear values in these fields
  73.   temp_reg |= (MCG_C1_CLKS(2) | MCG_C1_FRDIV(frdiv_val)); // Set the required CLKS and FRDIV values
  74.   MCG_C1 = temp_reg;

  75.   // if the external oscillator is used need to wait for OSCINIT to set
  76.   if (erefs_val)
  77.   {
  78.     for (i = 0 ; i < 20000 ; i++)
  79.     {
  80.       if (MCG_S & MCG_S_OSCINIT0_MASK) break; // jump out early if OSCINIT sets before loop finishes
  81.     }
  82.   if (!(MCG_S & MCG_S_OSCINIT0_MASK)) return 0x23; // check bit is really set and return with error if not set
  83.   }

  84.   // wait for Reference clock Status bit to clear
  85.   for (i = 0 ; i < 2000 ; i++)
  86.   {
  87.     if (!(MCG_S & MCG_S_IREFST_MASK)) break; // jump out early if IREFST clears before loop finishes
  88.   }
  89.   if (MCG_S & MCG_S_IREFST_MASK) return 0x11; // check bit is really clear and return with error if not set

  90.   // Wait for clock status bits to show clock source is ext ref clk
  91.   for (i = 0 ; i < 2000 ; i++)
  92.   {
  93.     if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) == 0x2) break; // jump out early if CLKST shows EXT CLK slected before loop finishes
  94.   }
  95.   if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2) return 0x1A; // check EXT CLK is really selected and return with error if not

  96.   // Now in FBE
  97.   // It is recommended that the clock monitor is enabled when using an external clock as the clock source/reference.
  98.   // It is enabled here but can be removed if this is not required.
  99.   MCG_C6 |= MCG_C6_CME0_MASK;
  100.   
  101.   // Configure PLL
  102.   // Configure MCG_C5
  103.   // If the PLL is to run in STOP mode then the PLLSTEN bit needs to be OR'ed in here or in user code.
  104.   temp_reg = MCG_C5;
  105.   temp_reg &= ~MCG_C5_PRDIV0_MASK;
  106.   temp_reg |= MCG_C5_PRDIV0(prdiv_val - 1);    //set PLL ref divider
  107.   MCG_C5 = temp_reg;

  108.   // Configure MCG_C6
  109.   // The PLLS bit is set to enable the PLL, MCGOUT still sourced from ext ref clk
  110.   // The loss of lock interrupt can be enabled by seperately OR'ing in the LOLIE bit in MCG_C6
  111.   temp_reg = MCG_C6; // store present C6 value
  112.   temp_reg &= ~MCG_C6_VDIV0_MASK; // clear VDIV settings
  113.   temp_reg |= MCG_C6_PLLS_MASK | MCG_C6_VDIV0(vdiv_val - 24); // write new VDIV and enable PLL
  114.   MCG_C6 = temp_reg; // update MCG_C6

  115.   // wait for PLLST status bit to set
  116.   for (i = 0 ; i < 2000 ; i++)
  117.   {
  118.     if (MCG_S & MCG_S_PLLST_MASK) break; // jump out early if PLLST sets before loop finishes
  119.   }
  120.   if (!(MCG_S & MCG_S_PLLST_MASK)) return 0x16; // check bit is really set and return with error if not set

  121.   // Wait for LOCK bit to set
  122.   for (i = 0 ; i < 4000 ; i++)
  123.   {
  124.     if (MCG_S & MCG_S_LOCK0_MASK) break; // jump out early if LOCK sets before loop finishes
  125.   }
  126.   if (!(MCG_S & MCG_S_LOCK0_MASK)) return 0x44; // check bit is really set and return with error if not set

  127.   // Use actual PLL settings to calculate PLL frequency
  128.   prdiv = ((MCG_C5 & MCG_C5_PRDIV0_MASK) + 1);
  129.   vdiv = ((MCG_C6 & MCG_C6_VDIV0_MASK) + 24);

  130.   // now in PBE

  131.   MCG_C1 &= ~MCG_C1_CLKS_MASK; // clear CLKS to switch CLKS mux to select PLL as MCG_OUT

  132.   // Wait for clock status bits to update
  133.   for (i = 0 ; i < 2000 ; i++)
  134.   {
  135.     if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) == 0x3) break; // jump out early if CLKST = 3 before loop finishes
  136.   }
  137.   if (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x3) return 0x1B; // check CLKST is set correctly and return with error if not

  138.   // Now in PEE
  139.   
  140.   return ((crystal_val / prdiv) * vdiv); //MCGOUT equals PLL output frequency
  141. } // pll_init
复制代码









作者: [JACE]    时间: 2015-1-31 14:36
本帖最后由 [JACE] 于 2015-1-31 14:38 编辑

aytc100  天翊
黑色密码
abacrya、军

快来回答

作者: 空有一人    时间: 2015-1-31 14:49
6666666




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2