|
下面是小虾我写的个代码,主要是测量电流和温度,温度转换函数我还没最后确定就没写出来,P1口是采集端,用P37控制采集端(采集电流/温度),P0和P0口是复用端口,P36控制数码管(显示温度/电流),由于我采集的温度不可能超过200度,所以我的百位是用P30,P31控制的,在用KEIL和PROTEUS仿真时,总是出不来正确的。
#include "reg51.h"
#define uint unsigned int
#define uchar unsigned char
code uchar showdata[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x0,0x10};//,0x08,0x03,0x46,0x21,0x06,0x0e};
sbit P37=P3^7; //选通采集通道,P37为0,选通电流采集通道,为1,选通温度采集通道
sbit P36=P3^6; //选通数码管
sbit P35=P3^5; //温度报警控制
sbit P34=P3^4; //电流报警控制
sbit P30=P3^0; //P30,P31为控制温度的百位,一般温度不会超过两百度
sbit P31=P3^1;
uchar n0;
uchar tt=0;
uchar n1=10;
void delay(uchar m)
{
uint n;
for (;m>0;m--)
for (n=0;n<650;n++)
{};
}
void showI(uchar val)
{
P36=0; //选通显示电流数码管
n0=val;
if(n0>13)
{
P34=0; //电流报警
delay(2000);
}
if(n0<10)
{
P0=showdata[n0];
P2=0xff;
delay(1000); //调整数值,控制数码管的通电时间;
P0=0xff;
P2=0xff;
P34=1;
P37=1; //选通采集温度通道;
n0 =0;
}
if(n0>9)
{
#pragma ASM
PUSH ACC
PUSH B
MOV A,n0
MOV B,n1
DIV AB
MOV n0,A
MOV n1,B
POP B
POP ACC
#pragma ENDASM
P0=showdata[n1];
P2=showdata[n0];
delay(1000); //调整数值,控制数码管的通电时间;
P0=0xff;
P2=0xff;
P34=1;
P37=1; //选通采集温度通道;
n0 = 0;
}
}
void showT(uchar value2)
{
P0=0xff;
P2=0xff;
P30=1;
P31=1;
P36=1; //选通数码管
n0=value2;
if(n0<10)
{
P0=showdata[n0];
P2=showdata[n1];
delay(1000);
P0=0xff;
P2=0xff;
P37 = 0; //选通采集电流
P35 = 1;
P36 = 0;
n0 = 0;
}
else if(n0-9>0&&n0-100<0)
{
#pragma ASM
PUSH ACC
PUSH B
MOV A,n0
MOV B,n1
DIV AB
MOV n0,A
MOV n1,B
MOV A,0xff
MOV B,0xff
POP B
POP ACC
#pragma ENDASM
P2 = showdata[n0];
P0 = showdata[n1];
delay(1000); //数码管通电持续时间
P0=0xff;
P2=0xff;
P37 = 0; //选通采集电流
P35 = 1;
P36 = 0;
n0 = 0;
}
else if(n0-100>0)
{
if (n0>120)
{
P35=0; //p35点亮温度报警指示灯
}
P30=0;
P31=0;
n0=n0-100;
#pragma ASM
PUSH ACC
PUSH B
MOV A,n0
MOV B,n1
DIV AB
MOV n0,A
MOV n1,B
MOV A,0xff
MOV A,0xff
POP B
POP ACC
#pragma ENDASM
P2 = showdata[n0];
P0 = showdata[n1];
delay(1000);
P30 = 1;
P31 = 1;
P0=0xff;
P2=0xff;
P37 = 0; //选通采集电流
P35 = 1;
P36 = 0;
n0 = 0;
}
}
void init()
{
P0 = 0xff;
P2 = 0xff;
P30 = 1;
P31 = 1;
P34 = 1;
P35 = 1;
P36 = 1; //选通温度数码管
P37 = 1; //进行温度采集
}
void main ()
{
void showI(uchar val);
void showT(uchar val);
void init();
uchar i=0;
uchar eyv;
init();
showI(54);
showT(45);
for(i=0;i<10;i++)
{
eyv=P1=0x17;
if (P37==0) //采集的电流;
{
tt=eyv>tt?eyv:tt;
showI(tt);
}
else
showT(eyv);
}
}
|
|