验证过的STC12C5A60S2单片机AD转换调试程序

/***********程序简介:利用STC单片机的AD采样0-5V的数据然后用串口显示转换的电压********
************程序的主要目的是对STC单片机AD的测试**************************************
************************************************************************************/
#include "reg51.h"
#include "intrins.h"

sfr AUXR=0x8E;
sfr AUXR1=0xA2;
sfr ADC_CONTR = 0xBC;
sfr ADC_RES = 0xBD;
sfr ADC_RESL = 0xBE;
sfr P1ASF = 0x9D;

float res;
unsigned int res1;
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_START 0x08
#define ADC_SPEEDLL 0x00 //540个时钟周期转换一次
unsigned char lux[6]={0};

void Delay(unsigned int n)
{
unsigned int x;
while(n--)
{
x=5000;
while(x--);
}
}

void InitADC()
{
P1ASF=0xff;
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR=0x88; //ADC控制寄存器设置1000 1000--即开启ADC的电源,540时钟转换周期
Delay(2); //延时一段时间让ADC的电源稳定 //开启ADC转换,选择通道0
AUXR1&=0xfb; //将ADRJ置0
}

void Init_serial()
{
SCON=0x50; //串口1工作方式1
AUXR|=0x40; //Timer1 speed 12 of MCU
AUXR&=0xFE; //use T1 as baud sender
TMOD=0x20;
TL1 = 0xF7; //设定定时初值 波特率38400
TH1 = 0xF7; //设定定时器重装值
//ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
//ES = 1; //允许串行中断
//EADC=1;
//EA=1; //开总中断
IE=0xA0;
}

void SendData(unsigned char dat)
{
SBUF = dat;
while (!TI);
TI = 0;
}

void Send_String(unsigned char *p)
{
unsigned char i;
for(i=0;i<=5;i++)
{
SendData(*p++);
}
}

void Get_ADdate()
{
if(ADC_CONTR&0x10)
{
ADC_CONTR=0x88;
Delay(2);
ADC_RESL&=0x03;
res=ADC_RES*4+ADC_RESL;
res=(res*5)/1024;
res1=res*10000;
lux[0]=res1/10000+'0';
lux[1]='.';
lux[2]=res1%10000/1000+'0';
lux[3]=res1%1000/100+'0';
lux[4]=res1%100/10+'0';
lux[5]=res1%10+'0';
Send_String(lux);
SendData('\n');
Delay(100);
}
}

void main()
{
Init_serial();
InitADC();
while (1)
{
Get_ADdate();
}
}

相关主题
相关文档
最新文档