标题:
模块化51单片机库程序温度计18B20源程序,保证好用,有需要的拿走吧!
[打印本页]
作者:
lczhanga
时间:
2019-8-25 11:41
标题:
模块化51单片机库程序温度计18B20源程序,保证好用,有需要的拿走吧!
模块化51库程序温度计18B20程序,保证好用,有需要的拿走吧!!!!!
单片机源程序如下:
#include <REGX52.H>
#include <intrins.h>
//=====18B20位申明======//
sbit cs=P2^2;
sbit dula=P2^6;
sbit wela=P2^7;
//=====18B20位申明完成======//
unsigned char code table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f
};
unsigned char temp,temp1; //18B20全局变量。
//===============================================//
//====11.0592Mhz晶振前提下,延时1毫秒左右=====//
void delay1ms(unsigned int ms)
{
unsigned int x,y;
for(x=ms;x>0;x--)
for(y=112;y>0;y--);
}
//================延时1毫秒完毕==============//
//=============================================//
//====11.0592Mhz晶振前提下,延时110微秒左右====//
void delay110us(unsigned int us)
{
unsigned int x,y;
for(x=us;x>0;x--)
for(y=11;y>0;y--);
}
//=============================================//
//=====18B20函数申明======//
void init_18B20(void);
void write_18B20byte(unsigned char record);
unsigned char read_18B20byte(void);
unsigned char shift_18B20temp(void);
void display_18B20(unsigned char temp,unsigned char temp1);
//=====18B20函数申明======//
//======18B20用到的函数=====//
void init_18B20(void)
{
cs=1;
cs=0;
delay110us(8);
cs=1;
delay110us(4);
cs=1;
}
void write_18B20byte(unsigned char record)
{
unsigned char a;
cs=1;
for(a=0;a<8;a++)
{
cs=0;
_nop_();
cs=record&0x01;
delay110us(1);
cs=1;
record>>=1;
}
delay110us(2);
}
unsigned char read_18B20byte(void)
{
unsigned char a,value;
cs=1;
for(a=0;a<8;a++)
{
cs=0;
_nop_();
value>>=1;
cs=1;
if(cs)
value|=0x80;
delay110us(1);
}
return value;
}
unsigned char shift_18B20temp(void)
{
unsigned char low,high,temp=0;
init_18B20();
write_18B20byte(0xcc);
write_18B20byte(0x44);
init_18B20();
write_18B20byte(0xcc);
write_18B20byte(0xbe);
low=read_18B20byte();
high=read_18B20byte();
temp1=(low&0x0f)*6.25;
high<<=4;
low>>=4;
high=high+low;
delay110us(5);
return high;
}
//======18B20用到的函数完成=====//
void display_18B20(unsigned char temp,unsigned char temp1)
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[temp/10];
dula=0;
delay1ms(2);
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[temp%10]+0x80;
dula=0;
delay1ms(2);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[temp1/10];
dula=0;
delay1ms(2);
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[temp1%10];
dula=0;
delay1ms(2);
}
void main()
{
while(1)
{
temp=shift_18B20temp();
display_18B20(temp,temp1);
}
}
复制代码
以上资料51hei提供下载:
18B20.rar
(20.92 KB, 下载次数: 27)
2019-8-25 11:40 上传
点击文件名下载附件
模块化51库程序温度计18B20程序,保证好用,有需要的拿走吧!!!!!
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1