-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
34 lines (31 loc) · 885 Bytes
/
adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<LPC214x.H>
#include"adc.h"
unsigned int ADC(int adc_num,int channel,unsigned int mode)
{
unsigned int val;
if(adc_num==0)
{
AD0CR&=~(0xFF);
AD0CR = AD0CR|(0x0000FF00&(CLKDIV<<8)); // Dividing PCLK by CLKDIV set to lowest value
AD0CR = AD0CR|PDN; // ADC is operational not power dwn
AD0CR = AD0CR|(1<<channel);//channel of AD0 SELECT
if(mode==CONTROLLED_MODE)
{
AD0CR = (AD0CR & ~(7<<24))|(1<<24); // Start Conversion NOW bIt 24 is 1
}
// wait to finish conversion
if(channel==1)
{
while(!(AD0GDR&(0x80000000)));
val = AD0DR1;// bit 6-15 contains data
}
else if(channel==2)
{
while(!(AD0GDR&(0x80000000)));
val = AD0DR2;
}
val = val & DATA_MASK; // data manipulation to get 10 bit value
return(val>>6);
}
return(0);
}