实验现象:数码管显示实时温度
#include "STC15F2K60S2.h"
#include "intrins.h"
#include "onewire.h"
code unsigned char Seg_Table[] =
{ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xFF,0xbf};
unsigned int temp = 235;
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void SMG_Show(unsigned char pos,unsigned char num)
{
P0 =0xff;
P2 = (P2 & 0x1f) | 0xc0;
P0 = 0x01<<pos;
P2 = (P2 & 0x1f) | 0xe0;
P0 = num;
}
unsigned int Read_DS18B20_temp()
{
unsigned char LSB,MSB;
unsigned int temp;
init_ds18b20();//DS18B20复位
Write_DS18B20(0xcc);//跳过ROM操作指令
Write_DS18B20(0x44); //开始温度转换
Delay_OneWire(200);
init_ds18b20(); //DS18B20复位
Write_DS18B20(0xcc);//跳过ROM操作指令
Write_DS18B20(0xbe); //开始读取高速暂存器
LSB=Read_DS18B20();//读取温度数据的低8位
MSB=Read_DS18B20();//读取温度数据的高8位
/** 精度为0.0625摄氏度 */
temp = (MSB & 0x0f); //清前四位
temp = (temp<<8)|LSB; //将LSB和MSB整合成为一个16位的整数
temp = temp * 0.0625 * 10; //放大十倍
return temp;
}
void main()
{
while(1)
{
temp = Read_DS18B20_temp(); //读出来的是放大十倍的温度
SMG_Show(5,Seg_Table[temp/100]); //23.5
Delay1ms();
SMG_Show(6,(Seg_Table[temp/10%10])& 0x7f);
Delay1ms();
SMG_Show(7,Seg_Table[temp%10]);
Delay1ms();
}
}