#include "Function.h"

//======================================================================================     
//函数名：DelayMs
//功  能：短延时,误差1%以内
//输  入：软件延时,延时时间ms
//输  出：无
//======================================================================================
void DelayMs(u16 ms)  //16M/6T
{                         
	u16 a;
	while(ms)
	{
		a=354;
		while(a--);
		ms--;
	}
	return;
}

//======================================================================================     
//函数名：Initial_trim
//功  能：主程序开始运行之前可以通过调用此函数初始化ADC,16MHz时钟,32KHz时钟的修调值 
//输  入：无
//输  出：无
//======================================================================================
void Initial_trim()
{
  u8 temp;
	
  FLASH_ADL =0;
	FLASH_ADH =0;
	FLASH_CR = 0xC2;
	temp = FLASH_DR;
	BG_VTRIM = temp;	//BG电压修调
	
  FLASH_ADL =2 ;
	FLASH_CR = 0xC2;
	temp = FLASH_DR;
	IMO_TRIM = temp;		
	
	FLASH_ADL =3 ;
	FLASH_CR = 0xC2;
	temp = FLASH_DR;
	IMO_TRIMH = temp;	//IMO 修调共8BIT 	

  FLASH_ADL =4 ;
	FLASH_CR = 0xC2;
	temp = FLASH_DR;
	ILO_TRIM = temp;  //ILO 电流电阻修调
	FLASH_CR = 0xC0;	
}

//======================================================================================     
//函数名：System_Init
//功  能：配置系统时钟， SCK0/SCK1/SCK2/SCK3设置
//输  入：无
//输  出：无
//======================================================================================
void System_Init(void)
{
	IMO_CR  = 0x20; 			//选择内部时钟24M两分频，即12M
												//bit[7:6]系统时钟源选择：
												//0x    选择内部8MHz时钟
												//10    保留，不允许使用
												//11    选择内部16MHz时钟
	CLK_CR     = 0x03;    // Core Work Clock Select System Clock
												//SCK3IF bit7: 0 = 没有SCK3中断发生,1 = 有SCK3中断发生.对该位写1会将其清零
												//bit[6:3] N/A 保留
												//CPUCKS bit[2:0] 内核工作频率选择:
												//000   SYSCLK/8
												//001   SYSCLK/4
												//010   SYSCLK/2
												//011   SYSCLK
												//100   SYSCLK/16
												//101   SYSCLK/32
												//110   SYSCLK/64
												//111   SYSCLK/128

	PCLK_CR    = 0xf7;    // Enable SCK0/SCK1/SCK2/SCK3, Disable SCK3 Interrupt, SCK1 Select System, SCK2 Select System, SCK3 Select SCK2
												//SCK0EN bit7: 0 = 禁止SCK0时钟,1 = 使能SCK0时钟
												//SCK1EN bit6: 0 = 禁止SCK1时钟,1 = 使能SCK1时钟
												//SCK2EN bit5: 0 = 禁止SCK2时钟,1 = 使能SCK2时钟
												//SCK3EN bit4: 0 = 禁止SCK3时钟,1 = 使能SCK3时钟
												//SCK3_IE bit3: 0 = 禁止SCK3时钟中断,1 = 使能SCK3时钟中断
												//SCK2SS bit2: SCK2时钟源选择，具体使用见SCK2CKS说明：
												//0 = CLK_SYS作为SCK2的时钟源,1 = SCK1作为SCK2的时钟源
												//SCK3SS bit[1:0] SCK3时钟源选择：
												//00   关闭SCK3时钟
												//01   来自SYSCLK
												//10   来自SCK1时钟
												//11   来自SCK2时钟
	
//		PCLK_DIV12 = 0x60;    
//												//SCK1CKS bit[7:4] 控制SCK1时钟分频：fSCK1=fSYSCLK/(SCK1CKS+1)
//												//SCK2CKS bit[3:0] 控制SCK2时钟分频：
//												//SCK2SS=0时 fSCK2=fSYSCLK/(SCK2CKS+1)
//												//SCK2SS=1时 fSCK2=fSYSCLK/(SCK2CKS+1)/(SCK1CKS+1)

//  PCLK_DIV3  = 103;    //SCK3 = SCK2/104
//											//SCK3CKS bit[7:0] 控制SCK3时钟的分频，频率和SCK3SS的值相关，具体计算方法如下：
//											//当SCK3SS等于:
//											//00   关闭SCK3时钟
//											//10	 fSCK3=fSYSCLK/(SCK3CKS+1)
//											//10   fSCK3=fSYSCLK/(SCK3CKS+1)/(SCK1CKS+1)
//											//11   fSCK3=fSYSCLK/(SCK3CKS+1)/(SCK2CKS+1)
		
	DelayMs(100);       //延时时间最小8ms，具体和电源上电爬坡时间有关
	Initial_trim();	    //修调函数，必须加入	
}


