#include "gpio.h"
#include "adcts.h"
#include "uart.h"
// adccon
void AdInit(u32 freq)
{
u32 TEMP;
SYSC_GetClkInform();
TEMP = g_PCLK/freq -1;
ADCTS->rADCCON = (1<<14)|(TEMP<<6);
return ;
}
//判断是否已经转换好
u32 ReadData(void)
{
ADCTS->rADCCON |= 0x1; //start ADC
while (ADCTS->rADCCON & 0x1); //check if Enable_start is low
while (!(ADCTS->rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
return ( (int)ADCTS->rADCDAT0 & 0x3ff );
}
void OutRead(u32 x)
{
u8 TEMP;
TEMP = (u8)x/1000;
Sendchar(TEMP);
x = x%1000;
TEMP = (u8)x/100;
Sendchar(TEMP);
x %= 100;
TEMP = (u8)x/10;
Sendchar(TEMP);
x = (u8)x%10;
Sendchar(x);
}
void delay(int times)
{
int i;
for(;times>0;times--)
for(i=0;i<3000;i++);
}
int main()
{
u32 temp;
UartInit();
BaudInit(9600);
AdInit(2500000);
Sendchar('r');
do{
temp = ReadData();
OutRead(temp);
delay(8000);
} while(Getchar() != 'o');
}
相关的register,ADCCON ,ADCDAT0
adccon 设定ad channel 及转换频率,以及使能,还有转换的位数
adcdatx 就是存储的相关值,ADCTS->rADCCON |= 0x1; //start ADC
while (ADCTS->rADCCON & 0x1); //check if Enable_start is low
while (!(ADCTS->rADCCON & 0x8000)); //检测结束
(int)ADCTS->rADCDAT0 & 0x3ff ); 这是所得的值
很简单,不难