#define GLOBAL_CLK 1
/**
1.初始化串口配置
2.循环进行任意键测试adc模数转换,按ESC键退出
①保存rADCCON原有的数据
②设置转换速度频率,第几个通道进行转换
③读取任意键(除了ESC键)
④启动转换
⑤判断是否启动成功
⑥判断是否转换成功
⑦返回读到的内容
⑧打印结果出来
*/
#include "uart.h"
#include <stdlib.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#define ADC_FREQ 2500000
volatile U32 prescaler;
int readadc(int ch)
{
int i;
//只初始化一次,第二次运行的时候,会跳过这段代码的,因为它是静态的。。
static int prevch = -1;
//设置 使能频率 | 频率 | 第几个通道
rADCCON = (1<<14)|(prescaler<<6)|(ch<<3);
if(prevch != ch)
{
rADCCON = (1<<14)|(prescaler<<6)|(ch<<3);
for(i=0; i<10000; i++);
for(i=0; i<10000; i++);
prevch = ch;
}
//置1,启动转换
rADCCON |= 0x1;
//由于每次 开始转换的启动成功 时,它都会自动清零的。
while(rADCCON & 0x1);
//判断是否转换完结,和上面的那个不一样的,上面的是开启成功,这个是转换完结成功,一个始,一个终
while(!(rADCCON & 0x8000));
//读出它的内容
return (((int)rADCDAT0 & 0x3ff));
}
void testadc(void)
{
int i,a2=0;
//保存寄存器的数据
U32 rADCCON_save = rADCCON;
uart_printf("\nADC TEST frequency %d\n",ADC_FREQ);
//设置频率
prescaler = 50000000/ADC_FREQ -1;
uart_printf("\n ADC PRSCVL %d\n",prescaler);
//判断是否是ESC键,任意键死循环(除了ESC键退出)
while(uart_getch() != 0x1b)
{
//类似阻塞的读取转换结果
a2 = readadc(2);
//打印结果
uart_printf("AIN2 : %d\n", a2);
for(i=0; i<10000; i++);
for(i=0; i<10000; i++);
for(i=0; i<10000; i++);
for(i=0; i<10000; i++);
}
//复位数据
rADCCON = rADCCON_save;
uart_printf("end\n");
}
void Main(void)
{
//初始化串口
init_uart();
//测试adc模数转换
testadc();
while(1);
}