基于51单片机的电压测量(4路,ADS1110)
4路电压采集
1、测量并显示4路采集通道电压
2、设置上下限报警,电压过限蜂鸣器响
3、测量范围0-10V
#include<reg51.h>
#include "ADS1110.h"
#include "lcd1602.h"
sbit high1=P1^0;//阀值调节
sbit low1 =P1^1;
sbit high2=P1^2;
sbit low2 =P1^3;
sbit high3=P1^4;
sbit low3 =P1^5;
sbit high4=P1^6;
sbit low4 =P1^7;
sbit set=P2^7;//设置
sbit beep=P2^6;//蜂鸣器
uchar ye=0;//页数
uchar hang1[]="00.00V 00.00V ";
uchar hang2[]="00.00V 00.00V ";
uchar hang3[]="00V 00V 00V 00V";//上限
uchar hang4[]="00V 00V 00V 00V";//下限
uchar up1=6,up2=6,up3=6,up4=6;//上限
uchar down1=3,down2=3,down3=3,down4=3;//下限
uint u1=0,u2=0,u3=0,u4=0;//电压
uchar time=0;//定时
unsigned long num=0;
void main()//主函数
{
uchar i;
init_1602();
TMOD|=0X01;
TH0=0X3C; //给定时器赋初值,定时50ms
TL0=0XB0;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
while(1)
{
//按键检测
if(!set)
{
ye=!ye;
while(!set);
}
if(ye)
{
if(!high1) //上限设置
{
if(up1<10)
up1++;
else
up1=down1;
while(!high1);
}
if(!high2)
{
if(up2<10)
up2++;
else
up2=down2;
while(!high2);
}
if(!high3)
{
if(up3<10)
up3++;
else
up3=down3;
while(!high3);
}
if(!high4)
{
if(up4<10)
up4++;
else
up4=down4;
while(!high4);
}
if(!low1) //下限设置
{
if(down1>0)
down1--;
else
down1=up1;
while(!low1);
}
if(!low2)
{
if(down2>0)
down2--;
else
down2=up2;
while(!low2);
}
if(!low3)
{
if(down3>0)
down3--;
else
down3=up3;
while(!low3);
}
if(!low4)
{
if(down4>0)
down4--;
else
down4=up4;
while(!low4);
}
}
//报警
i=0;
if((u1/100<down1)||(u1/100>up1))
i++;
if((u2/100<down2)||(u2/100>up2))
i++;
if((u3/100<down3)||(u3/100>up3))
i++;
if((u4/100<down4)||(u4/100>up4))
i++;
if(i)
beep=0;
else
beep=1;
}
}
void Timer0() interrupt 1//定时器中断
{
if(time<10)//定时0.5s
time++;
else
{
time=0;
u1=Ads1110_volt1();//测量
u2=Ads1110_volt2();
u3=Ads1110_volt3();
u4=Ads1110_volt4();
//电压计算
num=u1;
num=num*1000/32625;
u1=num;
num=u2;
num=num*1000/32625;
u2=num;
num=u3;
num=num*1000/32625;
u3=num;
num=u4;
num=num*1000/32625;
u4=num;
//显示计算
hang1[0]=u1/1000+0x30;
hang1[1]=u1%1000/100+0x30;
hang1[3]=u1%100/10+0x30;
hang1[4]=u1%10+0x30;
hang1[8]=u2/1000+0x30;
hang1[9]=u2%1000/100+0x30;
hang1[11]=u2%100/10+0x30;
hang1[12]=u2%10+0x30;
hang2[0]=u3/1000+0x30;
hang2[1]=u3%1000/100+0x30;
hang2[3]=u3%100/10+0x30;
hang2[4]=u3%10+0x30;
hang2[8]=u4/1000+0x30;
hang2[9]=u4%1000/100+0x30;
hang2[11]=u4%100/10+0x30;
hang2[12]=u4%10+0x30;
hang3[0]=up1/10+0x30;
hang3[1]=up1%10+0x30;
hang3[4]=up2/10+0x30;
hang3[5]=up2%10+0x30;
hang3[8]=up3/10+0x30;
hang3[9]=up3%10+0x30;
hang3[12]=up4/10+0x30;
hang3[13]=up4%10+0x30;
hang4[0]=down1/10+0x30;
hang4[1]=down1%10+0x30;
hang4[4]=down2/10+0x30;
hang4[5]=down2%10+0x30;
hang4[8]=down3/10+0x30;
hang4[9]=down3%10+0x30;
hang4[12]=down4/10+0x30;
hang4[13]=down4%10+0x30;
if(ye)//显示
{
write_string(1,0,hang3);
write_string(2,0,hang4);
}
else
{
write_string(1,0,hang1);
write_string(2,0,hang2);
}
}
TH0=0X3C; //给定时器赋初值,定时50ms
TL0=0XB0;
}