/* Initialize the ADC peripheral and the ADC setup structure to default value */ void Chip_ADC_Init(LPC_ADC_T *pADC, ADC_CLOCK_SETUP_T *ADCSetup) { uint8_t div; uint32_t cr = 0; uint32_t clk; Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_ADC); #if defined(ADC_TRIM_SUPPORT) pADC->ADTRM = 0xF00; #endif pADC->INTEN = 0; /* Disable all interrupts */ cr |= ADC_CR_PDN; ADCSetup->adcRate = ADC_MAX_SAMPLE_RATE; ADCSetup->bitsAccuracy = 0; /* LPC17xx/40xx doesn't support this setting */ clk = 0; ADCSetup->burstMode = false; div = getClkDiv(pADC, false, ADCSetup->adcRate, clk); cr |= ADC_CR_CLKDIV(div); #if !defined(ADC_ACC_12BITS) cr |= ADC_CR_BITACC(ADCSetup->bitsAccuracy); #endif /*defined(ADC_ACC_12BITS)*/ pADC->CR = cr; }
/*********************************************************************//** * @brief Initial for ADC * + Set bit PCADC * + Set clock for ADC * + Set Clock Frequency * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC * @param[in] rate ADC conversion rate, should be <=200KHz * @return None **********************************************************************/ void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate) { uint32_t ADCPClk, temp, tmp; CHECK_PARAM(PARAM_ADCx(ADCx)); CHECK_PARAM(PARAM_ADC_RATE(rate)); // Turn on power and clock CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCAD, ENABLE); ADCx->ADCR = 0; //Enable PDN bit tmp = ADC_CR_PDN; // Set clock frequency ADCPClk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_ADC); /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for * A/D converter, which should be less than or equal to 13MHz. * A fully conversion requires 65 of these clocks. * ADC clock = PCLK_ADC0 / (CLKDIV + 1); * ADC rate = ADC clock / 65; */ temp = rate * 65; temp = (ADCPClk * 2 + temp)/(2 * temp) - 1; //get the round value by fomular: (2*A + B)/(2*B) tmp |= ADC_CR_CLKDIV(temp); ADCx->ADCR = tmp; }
/*********************************************************************//** * @brief Initial for ADC * + Set bit PCADC * + Set clock for ADC * + Set Clock Frequency * @param[in] ADCx pointer to LPC_ADCn_Type, should be: LPC_ADC * @param[in] rate ADC conversion rate, should be <=200KHz * @param[in] bits_accuracy number of bits accuracy, should be <=10 bits and >=3bits * @return None **********************************************************************/ void ADC_Init(LPC_ADCn_Type *ADCx, uint32_t rate, uint8_t bits_accuracy) { uint32_t temp, tmpreg, ADCbitrate; CHECK_PARAM(PARAM_ADCx(ADCx)); CHECK_PARAM(PARAM_ADC_RATE(rate)); // Turn on power and clock //CGU_ConfigPPWR (CGU_PCONP_PCAD, ENABLE); ADCx->CR = 0; //Enable PDN bit tmpreg = ADC_CR_PDN; // Set clock frequency if(ADCx == LPC_ADC0) temp = CGU_GetPCLKFrequency(CGU_PERIPHERAL_ADC0); else if(ADCx == LPC_ADC1) temp = CGU_GetPCLKFrequency(CGU_PERIPHERAL_ADC1); /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for * A/D converter, which should be less than or equal to 13MHz. * A fully conversion requires (bits_accuracy+1) of these clocks. * ADC clock = PCLK_ADC0 / (CLKDIV + 1); * ADC rate = ADC clock / (bits_accuracy+1); */ ADCbitrate = (rate * (bits_accuracy+1)); temp = ((temp*2 + ADCbitrate) / (ADCbitrate*2)) - 1;//get the round value by fomular: (2*A + B)/(2*B) tmpreg |= ADC_CR_CLKDIV(temp) | ADC_CR_BITACC(10 - bits_accuracy); ADCx->CR = tmpreg; }
/*********************************************************************//** * @brief Initial for ADC * + Set bit PCADC * + Set clock for ADC * + Set Clock Frequency * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC * @param[in] rate ADC conversion rate, should be <=200KHz * @return None **********************************************************************/ void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate) { uint32_t temp, tmp; // Turn on power and clock CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCADC, ENABLE); ADCx->CR = 0; //Enable PDN bit tmp = ADC_CR_PDN; // Set clock frequency temp = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER); /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for * A/D converter, which should be less than or equal to 12.4MHz. * A fully conversion requires 31 of these clocks. * ADC clock = PCLK_ADC0 / (CLKDIV + 1); * ADC rate = ADC clock / 31; */ temp = (temp /(rate * 31)) - 1; tmp |= ADC_CR_CLKDIV(temp); ADCx->CR = tmp; }
/*********************************************************************//** * @brief Initial for ADC * - Set bit PCADC * - Set clock for ADC * - Set Clock Frequency * * @param[in] ConvFreq Clock frequency * @return None ************************************************************************/ void ADC_Init(uint32_t ConvFreq) { uint32_t temp, tmp; tmp = 0; CHECK_PARAM(PARAM_ADC_FREQUENCY(ConvFreq)); // Turn on power SYSCON_PowerCon(SYSCON_ABLOCK_ADC, ENABLE); // Turn on clock SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_ADC, ENABLE); LPC_ADC->CR = 0; // The A/D converter is in power-down mode //SYSCON_DeepSleepPowerCon(SYSCON_ABLOCK_ADC, ENABLE); // Set clock frequency temp = SystemCoreClock / LPC_SYSCON->SYSAHBCLKDIV; temp = (temp /ConvFreq) - 1; tmp |= ADC_CR_CLKDIV(temp); LPC_ADC->CR = tmp; }
/* Initialize the ADC */ void IP_ADC_Init(IP_ADC_001_T *pADC, uint8_t div, uint8_t bitsAcc, uint32_t flag) { uint32_t cr; #if defined(ADC_TRIM_SUPPORT) pADC->ADTRM = 0xF00; #endif pADC->INTEN = 0; /* Disable all interrupts */ cr = pADC->CR & (~ADC_CONFIG_MASK); #if defined(ADC_ACC_12BITS) cr |= ADC_CR_CLKDIV(div) | flag; #else cr |= ADC_CR_CLKDIV(div) | ADC_CR_BITACC(bitsAcc) | flag; #endif /*defined(ADC_ACC_12BITS)*/ pADC->CR = cr; }
/* Set the ADC Sample rate */ void Chip_ADC_SetSampleRate(LPC_ADC_T *pADC, ADC_CLOCK_SETUP_T *ADCSetup, uint32_t rate) { uint8_t div; uint32_t cr; cr = pADC->CR & (~ADC_SAMPLE_RATE_CONFIG_MASK); ADCSetup->adcRate = rate; div = getClkDiv(pADC, ADCSetup->burstMode, rate, (11 - ADCSetup->bitsAccuracy)); cr |= ADC_CR_CLKDIV(div); cr |= ADC_CR_BITACC(ADCSetup->bitsAccuracy); pADC->CR = cr; }
/* Initialize the ADC peripheral and the ADC setup structure to default value */ void Chip_ADC_Init(LPC_ADC_T *pADC, ADC_CLOCK_SETUP_T *ADCSetup) { uint8_t div; uint32_t cr = 0; uint32_t clk; Chip_Clock_EnableOpts(Chip_ADC_GetClockIndex(pADC), true, true, 1); pADC->INTEN = 0; /* Disable all interrupts */ cr |= ADC_CR_PDN; ADCSetup->adcRate = ADC_MAX_SAMPLE_RATE; ADCSetup->bitsAccuracy = ADC_10BITS; clk = 11; ADCSetup->burstMode = false; div = getClkDiv(pADC, false, ADCSetup->adcRate, clk); cr |= ADC_CR_CLKDIV(div); cr |= ADC_CR_BITACC(ADCSetup->bitsAccuracy); pADC->CR = cr; }
/** * @brief Initial for ADC * - Set bit PCADC * - Set clock for ADC * - Set Clock Frequency * * @param[in] ADCx pointer to ADC_TypeDef * @param[in] ConvFreq Clock frequency * @return None */ void ADC_Init(ADC_TypeDef *ADCx, uint32_t ConvFreq) { uint32_t temp, tmp; CHECK_PARAM(PARAM_ADCx(ADCx)); CHECK_PARAM(PARAM_ADC_FREQUENCY(ConvFreq)); // Turn on power and clock CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCAD, ENABLE); // Set clock divider for ADC to 4 from CCLK as default // CLKPWR_SetPCLKDiv(CLKPWR_PCLKSEL_ADC,CLKPWR_PCLKSEL_CCLK_DIV_4); ADCx->ADCR = 0; //Enable PDN bit tmp = ADC_CR_PDN; // Set clock frequency temp = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_ADC) ; temp = (temp /ConvFreq) - 1; tmp |= ADC_CR_CLKDIV(temp); ADCx->ADCR = tmp; }
/* Initialize the ADC peripheral and the ADC setup structure to default value */ void Chip_ADC_Init(LPC_ADC_T *pADC, ADC_CLOCK_SETUP_T *ADCSetup) { uint8_t div; uint32_t cr = 0; uint32_t clk; Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_ADC_PD); Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_ADC); pADC->INTEN = 0; /* Disable all interrupts */ cr |= ADC_CR_PDN; ADCSetup->adcRate = ADC_MAX_SAMPLE_RATE; ADCSetup->bitsAccuracy = ADC_10BITS; clk = 11; ADCSetup->burstMode = false; div = getClkDiv(pADC, false, ADCSetup->adcRate, clk); cr |= ADC_CR_CLKDIV(div); cr |= ADC_CR_BITACC(ADCSetup->bitsAccuracy); pADC->CR = cr; }