ADC,爲模數轉換器。S3C2440中提供的ADC轉換器有8個通道,其中4個用於觸摸屏,4個爲正常用途{ A[0..3] }(如下圖),中間有個8:1的爲8選一開關,用來選通AD轉換通道,INT_ADC爲中斷產生信號線,(XP,XM,YP,YM用於觸摸屏)
ADC的基本應用主要是配置ADCCON
注:ADC時鐘來源爲PCLK,配置時ADC的時鐘一定要小於PCLK的1/5,而且ADC時鐘頻率不能大於2.5MHz
ADC需要配置的:
1.是否開啓預分頻,開啓則設置預分頻係數
2.Mux 。 8個通道中選一個通道
3.是否讀取後自動開啓ADC
4.開啓ADC
一般通道轉換後的數據放在ADCDAT0的低10位
#include "2440addr.h"
#include "config.h"
#define PCLK 50000000
#define PRSCEN 1 //使能預分頻係數
#define PRSCVL 49 //預分頻係數,ADC Freq = (FCLK/PRSCVL + 1)
#define STDBM 0 //正常模式
#define READ_START 0 //讀取後不自動開啓ADC轉換,手動開啓
#define Adc_Start() rADCCON |= 1 //開啓ADC
void adcInit(unsigned char channel);
int adcRead(void);
void uartInit(int baudrate);
void putc(unsigned char c);
void Main(void)
{
int value;
/* 使用2號通道 */
adcInit(2);
uartInit(115200);
while(1)
{
value = adcRead();
/* value的範圍爲 0 ~ 1023 */
putc(value/1000 + '0');
putc( (value%1000)/100 + '0');
putc( (value%100)/10 + '0');
putc( (value%10) + '0');
putc('\n');
/* 延時 */
for(value = 0; value<1000000;value++)
;
}
}
void adcInit(unsigned char channel)
{
/* 配置ADC,手動啓動*/
rADCCON &= ~(0xffff);
rADCCON |= ((READ_START<<1) | (STDBM<<2) | (channel<<3) |
(PRSCVL<<6) | (PRSCEN<<14));
}
int adcRead(void)
{
/* 使能ADC */
Adc_Start();
/* ADC啓動後位0會自動置位0 */
while(rADCCON & (1<<0))
;
/* 等待ADC轉換結束 */
while(!(rADCCON & (1<<15)))
;
return ( (int)(rADCDAT0 & 0x3ff) );
}
/* 串口通信的配置 */
void uartInit(int baudrate)
{
rGPHCON &= ~((3<<4) | (3<<6));
rGPHCON |= (1<<5) | (1<<7);
rULCON0 &= ~(0xff);
rULCON0 |= (0x3<<0);
rUCON0 &= ~(0xffff);
rUCON0 |= ((1<<0) | (1<<2));
rUBRDIV0 = (int)(PCLK/baudrate/16) - 1;
}
void putc(unsigned char c)
{
rUTXH0 = c;
while(!(rUTRSTAT0 & (1<<2)))
;
}