LPC23xx的ADC功能實現(xiàn)
{
UINT32 ADC_Data=0;
PCONP=PCONP|(1<<12); /*ADC上電 */
/*The PCONP register allows turning off selected peripheral functions for the purpose of
saving power. This is accomplished by gating off the clock source to the specified
peripheral blocks.Some peripherals, particularly those that include analog functions, may consume power
that is not clock dependent. These peripherals may contain a separate disable control that
turns off additional circuitry to reduce power.If a peripheral control bit is 1, that peripheral is enabled.
If a peripheral bit is 0, that peripheral’s clock is disabled (gated off) to conserve power */
switch(unit)
{
case 0:
PINSEL1 |= (1<<14); /* AD0.0 *//*引腳功能配置*/
AD0CR=(1<<0) | /* SEL =0,選擇通道0*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR0;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x01|(1<<24) ; /* 設置通道0,并進行第一次轉換*/
//DelayNS(20);
while((AD0DR0&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR0&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR0;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 1:
PINSEL1 |= (1<<16); /* AD0.1 */
AD0CR=(1<<1) | /* SEL =1,選擇通道1*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR1;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x02|(1<<24) ; /* 設置通道1,并進行第一次轉換*/
//DelayNS(20);
while((AD0DR1&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR1&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR1;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 2:
PINSEL1 |= (1<<18); /* AD0.2 */
AD0CR=(1<<2) | /* SEL =2,選擇通道2*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR2;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x04|(1<<24) ; /* 設置通道2,并進行第一次轉換*/
//DelayNS(20);
while((AD0DR2&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR2&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR2;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 3:
PINSEL1 |= (1<<20); /* AD0.3 */
AD0CR=(1<<3) | /* SEL =3,選擇通道3*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR3;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x08|(1<<24) ; /* 設置通道3,并進行第一次轉換*/
//DelayNS(20);
while((AD0DR3&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR3&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR3;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 4:
PINSEL3 |= (3<<28); /* AD0.4 */
AD0CR=(1<<4) | /* SEL =4,選擇通道4*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR4;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x10|(1<<24) ; /* 設置通道4,并進行第一次轉換*/
//DelayNS(20);
while((AD0DR4&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR4&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR4;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 5:
PINSEL3|=0xc0000000; /* AD0.5 */
AD0CR=(1<<5) | /* SEL =5,選擇通道5*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR5;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x20|(1<<24) ; /* 設置通道5,進行第一次轉換*/
//DelayNS(20);
while((AD0DR5&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR5&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR5;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
case 6:
PINSEL0 |= (3<<24); /* AD0.6 */
AD0CR=(1<<6) | /* SEL =6,選擇通道6*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR6;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x40|(1<<24) ; /* 設置通道6,進行第一次轉換*/
//DelayNS(20);
while((AD0DR6&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR6&0x80000000)==0); /*等待轉換結束 */
ADC_Data=AD0DR6; /*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF; /*數(shù)值轉換*/
/********************************************************/
break;
case 7:
PINSEL0 |= (3<<26); /* AD0.7 */
AD0CR=(1<<7) | /* SEL =7,選擇通道7*/
((Fpclk/1000000-1)<<8) | /* CLKDIV=Fpclk/1000000-1,即轉換時鐘為1MHz*/
(0<<16) | /* BURST=0,軟件控制轉換操作 */
(0<<17) | /* CLKS=0, 使用11clock轉換 */
(1<<21) | /* PDN=1, 正常工作模式 (非掉電轉換模式)*/
(0<<22) | /* Reversed*/
(1<<24) | /* START=1, 直接啟動AD轉換 */
(0<<27);
//DelayNS(0x10);
ADC_Data=AD0DR7;
/**************************************************/
AD0CR=(AD0CR&0x00FFFF00)|0x40|(1<<24) ; /* 設置通道6,進行第一次轉換*/
//DelayNS(20);
while((AD0DR7&0x80000000)==0); /*等待轉換結束 */
//DelayNS(20);
AD0CR=AD0CR|(1<<24); /*再次啟動轉換 */
while((AD0DR7&0x80000000)==0);/*等待轉換結束 */
ADC_Data=AD0DR7;/*讀取轉換結果 */
ADC_Data=(ADC_Data>>6)&0x3FF;/*數(shù)值轉換*/
/********************************************************/
break;
default:
break;
}
//ADC_result = ((ADC_Data*1.0/1023)/0.4*3.3);
return(ADC_Data);
}
評論