#include "adc.h"


void ADC_PortInit(void)
{
    //打开GPIO时钟
    REGBITS_SET( CW_SYSCTRL->AHBEN, SYSCTRL_AHBEN_GPIOA_Msk );
    //打开ADC时钟
    REGBITS_SET( CW_SYSCTRL->APBEN2, SYSCTRL_APBEN2_ADC_Msk );
    //set PA01 as AIN1 INPUT
    PA01_ANALOG_ENABLE();
}

void ADC_Configuration(void)
{
		ADC_InitTypeDef ADC_InitStructure;
		ADC_SingleChTypeDef ADC_SingleChStructure;
		ADC_WdtTypeDef ADC_WdtStructure;
		GPIO_InitTypeDef GPIO_InitStructure;
	
		ADC_WdtInit(&ADC_WdtStructure);//ADC模拟看门狗通道初始化
	
		GPIO_InitStructure.IT=GPIO_IT_NONE;
		GPIO_InitStructure.Mode=GPIO_MODE_ANALOG;
		GPIO_InitStructure.Pins=GPIO_PIN_1;
		GPIO_InitStructure.Speed=GPIO_SPEED_HIGH;
		GPIO_Init(CW_GPIOA,&GPIO_InitStructure);
	
		ADC_InitStructure.ADC_AccEn=ADC_AccDisable;
		ADC_InitStructure.ADC_Align=ADC_AlignRight;
		ADC_InitStructure.ADC_ClkDiv=ADC_Clk_Div1;
		ADC_InitStructure.ADC_DMAEn=ADC_DmaEnable;
		ADC_InitStructure.ADC_InBufEn=ADC_BufDisable;
		ADC_InitStructure.ADC_OpMode=ADC_SingleChOneMode;
		ADC_InitStructure.ADC_SampleTime=ADC_SampTime5Clk;
		ADC_InitStructure.ADC_TsEn=ADC_TsDisable;
		ADC_InitStructure.ADC_VrefSel=ADC_Vref_BGR2p5;
		ADC_Init(&ADC_InitStructure);//ADC_Init   
	
		ADC_SingleChStructure.ADC_Chmux=ADC_ExInputCH1;//PA1
		ADC_SingleChStructure.ADC_DiscardEn=ADC_DiscardNull;//unknown
		ADC_SingleChStructure.ADC_InitStruct=ADC_InitStructure;
		ADC_SingleChStructure.ADC_WdtStruct=ADC_WdtStructure;
		ADC_SingleChOneModeCfg(&ADC_SingleChStructure);//ADC_channel_Init  
		
		ADC_ITConfig(ADC_IT_EOC, ENABLE);
		ADC_EnableIrq(ADC_INT_PRIORITY);
		ADC_ClearITPendingAll();
		ADC_Enable(); 
		ADC_ExtTrigCfg(ADC_TRIG_GTIM1, ENABLE);//ADC外部中断触发源配置
}
