STC12C5A60S2用DS18B20测温度及用自身ADC测0-5v电压并用数码管显示,但不知道为啥只有当温度在24-28时电压才能正常显示,不然就显示乱码。将温度程序注释掉就正常了。 我是新手,麻烦众高手们给看看。感激不尽!
#include<STC12C5A60S2.H>
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit da=P2^2;
sbit dula=P2^5;
sbit wela=P2^6;
sbit led1=P1^6;
sbit beep=P1^7;
float adc_v;
typedef unsigned char BYTE;
typedef unsigned int WORD;
uchar qian=0,bai=0,shi=0,ge=0;
uchar smg_dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar smgxiaoshu_dula[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delayus(uchar a1,b1,c1)
{
uchar a,b,c;
for(a=0;a<a1;a++)
for(b=0;b<b1;b++)
for(c=0;c<c1;c++);
}
void ds18b20_init() //18b20初始化
{
uchar a;
da=1;
delayus(1,1,1);
da=0;
delayus(6,1,63);
da=1;
delayus(1,1,15);
a=da;
delayus(5,1,63);
}
void ds18b20write_com(uchar com)//18b20写数据
{
uchar i;
for(i=0;i<8;i++)
{
da=0;
_nop_();
da=com&0x01;
delayus(1,1,15);
da=1;
com=com>>1;
};
delayus(1,1,1);
}
uchar ds18b20read_date()//18b20读数据
{
uchar i,v;
for(i=0;i<8;i++)
{
da=0;
_nop_();
v>>=1;
da=1;
delayus(1,1,1);
if(da)
{
v|=0x80;
};
delayus(1,1,8);
};
return v;
}
uchar ds18b20wendu_read() //18b20数据处理 无小数位
{
uchar a,b,wendu;
ds18b20_init();
ds18b20write_com(0xcc);
ds18b20write_com(0x44);
delayus(3,1,63);
ds18b20_init();
ds18b20write_com(0xcc);
ds18b20write_com(0xbe);
a=ds18b20read_date();
b=ds18b20read_date();
wendu=b<<4;
wendu+=(a&0xf0)>>4;
return wendu;
}
void adc_init()
{
P1ASF=0x01;
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR=0x80;
delayms(1);
}
WORD adc_get()
{
ADC_CONTR=0X98;
_nop_();_nop_();_nop_();_nop_();
while(!(ADC_CONTR&0x10));
ADC_CONTR&=0xef;
return(ADC_RES*4+ADC_RESL);
}
float ADC_work()
{
float ADC_valve;
uchar k;
for(k=0;k<100;k++)
{
ADC_valve+=adc_get();
}
ADC_valve/=100;
return ((ADC_valve*5)/1024);
}
void main()
{
BYTE tt;
uint kk,yali;
uchar wdge,wdshi;
float yali1;
led1=1;
adc_init();
while(1)
{
tt=ds18b20wendu_read();
wdge=tt%10;
wdshi=tt/10;
if(kk==100)
{
yali1=ADC_work();
yali1*=1000;
yali=(int)yali1;
kk=0;
}
qian=yali/1000;
bai=yali%1000/100;
shi=yali%1000%100/10;
ge=yali%1000%100%10;
wela=1;
P0=0X01;
wela=0;
P0=0;
dula=1;
P0=smg_dula[wdshi];
dula=0;
delayms(5);
P0=0;
wela=1;
P0=0X02;
wela=0;
P0=0;
dula=1;
P0=smg_dula[wdge];
dula=0;
delayms(5);
P0=0;
wela=1;
P0=0X10;
wela=0;
P0=0;
dula=1;
P0=smgxiaoshu_dula[qian];
dula=0;
delayms(5);
P0=0;
wela=1;
P0=0X20;
wela=0;
P0=0;
dula=1;
P0=smg_dula[bai];
dula=0;
delayms(5);
P0=0;
wela=1;
P0=0X40;
wela=0;
P0=0;
P0=0;
dula=1;
P0=smg_dula[shi];
dula=0;
delayms(5);
P0=0;
wela=1;
P0=0X80;
wela=0;
P0=0;
dula=1;
P0=smg_dula[ge];
dula=0;
delayms(2);
P0=0;
if(tt>30)
{
beep=1;
}
if(tt<20)
{
beep=0;
}
led1=~led1;
kk++;
}
}
|