/*------------------------------------
程序名称:双路AD0832数据采集
功能:将双路AD0832采集信号通过数码管显示
日期:2011/11/26
修改:无
-------------------------------------*/
#include <reg51.h>
#include <intrins.h>
#include <AD0832.h>
#include <Serial.h>
#define uint unsigned int
#define uchar unsigned char
uchar code dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳数码管段码,此处反相后用于共阴数码管
uchar code wela[]={0x80,0x40,0x20,0x10}; //4位数码管位码
extern uchar ADC_Num1,AD_Num2;
uchar ADC_Val1,ADC_Val2;
uchar UpLimit1,DownLimit1;
uchar UpLimit2,DownLimit2;
void InitTimer()
{
TMOD=0x01;
TH0=(65536-50000)/256; //定时器0重装初值
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
}
/*---------------------------------
定时器0中断函数
-----------------------------------*/
void Timer0() interrupt 1
{
static uchar count1;
TR0=0;
TH0=(65536-50000)/256; //定时器0重装初值
TL0=(65536-50000)%256;
count1++;
if(count1>=4)
{
count1=0;
AdConvert(0);
AdConvert(1);
}
TR0=1;
}
/*------------------------------------
延时函数
--------------------------------------*/
void delayms(xms)
{
uint i,j;
for(i=xms;i>0;i--)
{
for(j=110;j>0;j--);
}
}
/*-------------------------------------
数码管显示
--------------------------------------*/
void display(uchar num1,uchar num2)
{
uchar w1,w2,w3,w4;
w1=num1/10; //取num1十位
w2=num1%10; //取num1个位
w3=num2/10;
w4=num2%10;
P2=0x00;
P0=dula[w1]&0x7f; //数码管显示小数点
P2=wela[0]; //选择0位数码管
delayms(2);
P2=0x00;
P0=dula[w2];
P2=wela[1];
delayms(2);
P2=0x00;
P0=dula[w3]&0x7f;
P2=wela[2];
delayms(2);
P2=0x00;
P0=dula[w4];
P2=wela[3];
}
/*-------------------------------------
主函数
--------------------------------------*/
void main()
{
ADC_Init();
InitTimer();
while(1)
{
ADC_Val1=(uchar)(ADC_Num1*5.0*10/255); //2-10转换显示的电压值
ADC_Val2=(uchar)(ADC_Num2*5.0*10/255);
display(ADC_Val1,ADC_Val2);
/*if((ADC_Num1>66||ADC_Num1<35)&&(ADC_Num2<87||ADC_Num2>117)) //检测异常报警
{
display(0x01,0x01); //异常显示11
}
else
{
display(ADC_Val1,ADC_Val2); //正常的电压值
}*/
}
} |