Professional Documents
Culture Documents
h"
#include "type.h"
#include "adc.h"
#ifndef _BV
#define _BV(_x_) (1UL << (_x_))
#endif
volatile
volatile
volatile
volatile
uint32_t
uint32_t
uint32_t
uint32_t
ADCValue[ADC_NUM];
ADCIntDone = 0;
BurstCounter = 0;
OverRunCounter = 0;
#if BURST_MODE
volatile uint32_t channel_flag = 0;
#endif
#if ADC_INTERRUPT_FLAG
/******************************************************************************
** Function name:
ADC_IRQHandler
**
** Descriptions:
ADC interrupt handler
**
** parameters:
None
** Returned value:
None
**
******************************************************************************/
void ADC_IRQHandler (void)
{
uint32_t regVal;
volatile uint32_t dummy;
int i;
regVal = LPC_ADC->STAT;
/* Read ADC will clear the interrupt */
if ( regVal & 0x0000FF00 )
/* check OVERRUN error first */
{
OverRunCounter++;
for ( i = 0; i < ADC_NUM; i++ )
{
regVal = (regVal & 0x0000FF00) >> 0x08;
/* if overrun, just read ADDR to clear */
/* regVal variable has been reused. */
if ( regVal & _BV(i) )
{
dummy = LPC_ADC->DR[i];
}
}
LPC_ADC->CR &= ~((0x7<<24)|(0x1<<16)); /* stop ADC now, turn off BURST
bit. */
ADCIntDone = 1;
return;
}
for ( i = 0; i < ADC_NUM; i++ )
{
if ( regVal & _BV(i) )
{
ADCValue[i] = ( LPC_ADC->DR[i] >> 4 ) & 0xFFF;
}
}
#if BURST_MODE
BurstCounter++;
channel_flag |= (regVal & 0xFF);
if ( (channel_flag & 0xFF) == 0xFF )
{
/* All the bits in have been set, it indicates all the ADC
channels have been converted. */
LPC_ADC->CR &= ~(0x1<<16);
/* Clear BURST mode, stop ADC now */
channel_flag = 0;
ADCIntDone = 1;
}
#else
LPC_ADC->CR &= ~(0x7<<24);
/* stop ADC now */
ADCIntDone = 1;
#endif
return;
}
#endif
/*****************************************************************************
** Function name:
ADCInit
**
** Descriptions:
initialize ADC channel
**
** parameters:
ADC clock rate
** Returned value:
None
**
*****************************************************************************/
void ADCInit( uint32_t ADC_Clk )
{
uint32_t i, pclkdiv, pclk;
/* Enable CLOCK into ADC controller */
LPC_SC->PCONP |= (1 << 12);
for ( i = 0; i < ADC_NUM; i++ )
{
ADCValue[i] = 0x0;
}
/* all the related pins are set to ADC inputs, AD0.0~7 */
LPC_PINCON->PINSEL0 &= ~0x000000F0; /* P0.2~3, A0.6~7, function 10 */
LPC_PINCON->PINSEL0 |= 0x000000A0;
LPC_PINCON->PINSEL1 &= ~0x003FC000; /* P0.23~26, A0.0~3, function 01 */
LPC_PINCON->PINSEL1 |= 0x00154000;
LPC_PINCON->PINSEL3 |= 0xF0000000;
/* P1.30~31, A0.4~5, function 11 */
/* No pull-up no pull-down (function 10) on these ADC pins. */
LPC_PINCON->PINMODE0 &= ~0x000000F0;
LPC_PINCON->PINMODE0 |= 0x000000A0;
LPC_PINCON->PINMODE1 &= ~0x003FC000;
LPC_PINCON->PINMODE1 |= 0x002A8000;
LPC_PINCON->PINMODE3 &= ~0xF0000000;
LPC_PINCON->PINMODE3 |= 0xA0000000;
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1/4 of the SystemFrequency. */
/* Bit 24~25 is for ADC */
pclkdiv = (LPC_SC->PCLKSEL0 >> 24) & 0x03;
switch ( pclkdiv )
{
case 0x00:
default:
pclk = SystemFrequency/4;
break;
case 0x01:
pclk = SystemFrequency;
break;
case 0x02:
pclk = SystemFrequency/2;
break;
case 0x03:
pclk = SystemFrequency/8;
break;
}
LPC_ADC->CR = ( 0x01 <<
( ( pclk
- 1 */
( 0 << 16
led */
( 0 << 17
( 1 << 21
( 0 << 24
( 0 << 27
ger A/D conversion) */
) |
) |
) |
);
/*
/*
/*
/*