目前按照下面代码方式能获取到两个通道的ADC值了,各位大哥帮忙看看有没有其他问题,感谢感谢!
#include "STC15.h"
#include <stdio.h>
#include "intrins.h"
#define ADC_POWER 0x80 // ADC电源控制位
#define ADC_FLAG 0x10 // ADC完成标志
#define ADC_START 0x08 // ADC起始控制位
#define ADC_SPEEDLL 0x00 // 540个时钟
#define ADC_SPEEDL 0x20 // 360个时钟
#define ADC_SPEEDH 0x40 // 180个时钟
#define ADC_SPEEDHH 0x60 // 90个时钟
#define VCC 5000 // set ADC_REF_VCC 设置ADC基准电压为5V
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE ch ; // ADC通道号
BYTE flag = 0; // 设置转换完成标志位
BYTE H8bit = 0, L2bit = 0;
WORD AD10bit = 0, temp = 0, dat = 0, dat1 = 0;
unsigned long result = 0;result1 = 0;
void InitADC();
void UartInit(void);
char putchar(char c);
void Delayms(unsigned xms);
void DisplayData(unsigned int iData);
void main()
{
InitADC(); //初始化ADC
while (1)
{
if (flag)
{
if(ch==1)
{
flag = 0;
AD10bit = (AD10bit | H8bit) & 0x00ff;
AD10bit = (AD10bit << 2) | (L2bit & 0X03); //合并成10位二进制数
temp = AD10bit;
AD10bit = 0;
ADC_RES = 0;
ADC_RESL = 0;
// printf("value_X=%d", temp);
// printf(" ");
//注意:先把temp强制转换成long型,再进行电压转换才可获得正确的电压值
result = ((long)temp * VCC / 1024);
// DisplayData(result);
// printf("\r\n");
}
if(ch==0)
{
flag = 0;
AD10bit = (AD10bit | H8bit) & 0x00ff;
AD10bit = (AD10bit << 2) | (L2bit & 0X03); //合并成10位二进制数
dat = AD10bit;
AD10bit = 0;
ADC_RES = 0;
ADC_RESL = 0;
// printf("value_Y=%d", dat);
// printf(" ");
//注意:先把dat强制转换成long型,再进行电压转换才可获得正确的电压值
result1 = ((long)dat * VCC / 1024);
// DisplayData(result);
// printf("\r\n");
}
}
}
}
/*----------------------------
初始化ADC
----------------------------*/
void InitADC()
{
P1ASF = 0x03; //设置P1.0/P1.1口为AD口
P1M1 = 0x03; // set 1# chanel to AD 高阻
P1M0 = 0x00; // set 1# chanel to AD 高阻
CLK_DIV = 0x00; //结果右对齐
ADC_RES = 0; //清除结果寄存器
ADC_RESL = 0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|ch ;
Delayms(1); // ADC上电并延时1ms
AUXR |= 0x10; // set EADCI
IE = 0xa0; // Enable ADC interrupt and Open master interrupt switch
}
/*----------------------------
软件延时
----------------------------*/
void Delayms(unsigned xms) //@11.0592MHz
{
unsigned char data i, j;
while(xms--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void DisplayData(unsigned int iData)
{
char NumStr[4] = {0, 0, 0, 0}, i = 0;
NumStr[0] = iData / 1000; // 千位
NumStr[1] = (iData % 1000) / 100; // 百位
NumStr[2] = iData % 100 / 10; // 十位
NumStr[3] = iData % 10; // 个位
printf("ADC1=");
printf("%c", NumStr[0] + 0x30);
printf("%c", NumStr[1] + 0x30);
printf(".");
printf("%c", NumStr[2] + 0x30);
printf("%c", NumStr[3] + 0x30);
}
/*----------------------------
ADC中断服务程序
----------------------------*/
void adc_isr() interrupt 5
{
H8bit = ADC_RES;
L2bit = ADC_RESL;
ADC_CONTR &= !ADC_FLAG; //清除ADC中断标志
ch++;
ch%=2;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
flag = 1;
}
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xE8; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
ES=1; //允许串行口中断
EA=1; //允许总中断
}
char putchar(char c)
{
UartInit();
ES = 0;
SBUF = c;
while (TI == 0);
TI = 0;
ES = 1;
return 0;
}
|