【蓝桥杯(51) STC15F2K60S2】之 “DS18B20实验“

相关代码:
主函数模块:
#include “STC15F2K60S2.H”
#include “absacc.h”
#include “onewire.h”

unsigned char SMGNoDot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char SMGDot_CA[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char temp;

void Display_SMG_Temp();

void Delay(unsigned char t)
{
while(t–);
}

void Delay1(unsigned char t)
{
while(t–)
{
Display_SMG_Temp();
}
}

void Close_All(unsigned char dat)
{
XBYTE[0xc000] = 0xf8;
XBYTE[0xe000] = dat;
}

void Display_SMG_Bit(unsigned char pos ,unsigned char dat)
{
XBYTE[0xc000] = 0x01 << pos;
XBYTE[0xe000] = dat;
}

void Read_DS18B20_Temp()
{
unsigned char LSB,MSB;

init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay1(1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = MSB << 8 | LSB;
temp = temp >> 4;

// if((temp & 0xf800) == 0x0000)
// {
// temp = temp >> 4;
// temp *= 10;
// temp = temp + (LSB & 0x0f) * 0.625;
// }
}

void Display_SMG_Temp()
{
// Close_All(0xff);
Display_SMG_Bit(7,SMGNoDot_CA[temp % 10]);
Delay(100);
Display_SMG_Bit(6,SMGDot_CA[temp / 10 % 10]);
Delay(100);
Display_SMG_Bit(5,SMGNoDot_CA[temp / 100]);
Delay(100);
Display_SMG_Bit(4,0xff);
Delay(100);
Display_SMG_Bit(3,0xff);
Delay(10);
// Display_SMG_Bit(2,0xff);
// Delay(50);
// Display_SMG_Bit(1,0xff);
// Delay(50);
// Display_SMG_Bit(0,0xff);
// Delay(50);

}

void main()
{
while(1)
{
Read_DS18B20_Temp();
Display_SMG_Temp();
}
}

DS18B20驱动.c文件:
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include “reg52.h”

sbit DQ = P1^4; //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t–);
}

//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(60);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(60);
}

//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;

for(i=0;i<8;i++)
{
	DQ = 0;
	dat >>= 1;
	DQ = 1;
	if(DQ)
	{
		dat |= 0x80;
	}	    
	Delay_OneWire(60);
}
return dat;

}

//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;

DQ = 1;
Delay_OneWire(144);
DQ = 0;
Delay_OneWire(960 );
DQ = 1;
Delay_OneWire(120); 
initflag = DQ;     
Delay_OneWire(60);

return initflag;

}

DS18B20驱动.h文件:
#ifndef __ONEWIRE_H
#define __ONEWIRE_H

bit init_ds18b20(void); //; ;
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);

#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

world呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值