单片机源程序如下:
#include <REGX51.H>
#include "intrins.h"
sbit DQ = P3^0;
uchar i;
int tem;
unsigned char NixieTable[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
void Nixie(unsigned char location,unsigned char number)
{
P2 = 0x00;
switch(location)
{
case 1:P2_0 = 1; P2_1 = 0; P2_2 = 0; P2_3 = 0; break;
case 2:P2_0 = 0; P2_1 = 1; P2_2 = 0; P2_3 = 0; break;
case 3:P2_0 = 0; P2_1 = 0; P2_2 = 1; P2_3 = 0; break;
case 4:P2_0 = 0; P2_1 = 0; P2_2 = 0; P2_3 = 1; break;
}
P0 = NixieTable[number];
Delay1ms(10);
}
int DS18B20_Init(void)
{
uint Ack;
DQ = 1;
Delay1us(10);
DQ = 0;
Delay1us(480);
DQ = 1;
Delay1us(40);
Ack = DQ;
return Ack;
}
void SendByte(uchar Byte)
{
DQ=1;
Delay1us(1);
for(i=0;i<8;i++)
{
DQ=0;
DQ=Byte&0x01;
Delay1us(5);
DQ=1;
Byte>>=1;
}
}
uchar RcvByte(void)
{
uchar Byte;
DQ=1;
Delay1us(1);
for (i=8;i>0;i--)
{
DQ=0;
Byte>>=1;
DQ=1;
Delay1us(1);
if(DQ)
Byte|=0x80;
Delay1us(30);
DQ=1;
}
return Byte;
}
float Get_temp(void)
{
uchar TL, TH;
int temp; float T;
DS18B20_Init();
SendByte(0xcc);
SendByte(0x44);
Delay1us(5);
DS18B20_Init();
SendByte(0xcc);
SendByte(0x44);
SendByte(0xBE);
TL = RcvByte();
TH = RcvByte();
temp = (TH << 8) | TL;
T = temp * 0.0625;
return T;
}
void main()
{
while(1)
{
tem = (int)Get_temp();
Nixie(1,tem/10);
Nixie(2,tem%10);
}
}
|