ds18b20 C程序proteus仿真
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
DS18B20测温显示,proteus仿真。Written by jinsongliang
具体程序如下
Main.c:
#include
#include"lib51v3.c"
#include"18b20.c"
#include"segv3.c"
void main (void)
{
unsigned char disp[5]={10,0,0,14,2};
signed char temp=0;
while(1)
{
/*1、读取温度值*/
temp=Get_Temputer();
//2、显示前处理*/
if(temp<0)
{disp[0]=13;
temp=-temp;
disp[1]=temp/10;
disp[2]=temp%10;
}
else
{disp[0]=temp/100;
disp[1]=temp/10-disp[0]*10;
disp[2]=temp%10;
if(!disp[0]) //百位数不为0则显示,若要显示0,可将其注释掉disp[0]=10;
}
//3、显示*/
Seg_Display(disp);
}
}
Lib51v3.c
void Delay_Nus (unsigned char n) {
while(n--);
//for (;n>0;n--);
}
18b20.c:
/******************************************************************
时序很重要,移植时注意延时函数
******************************************************************/
#define B20_PORT P1 //此处可以设置I/O口
#define B20_CHANNEL 0
#define R_B20_CHANNEL() B20_PORT&(1< #define W_B20_CHANNEL_1() B20_PORT|=(1< #define W_B20_CHANNEL_0() B20_PORT&=~(1< #define SKIP_ROM 0xCC #define TEMPUTER_CONVERT 0x44 #define READ_ROM 0xBE //one /* 初始化函数,失败会返回1,初始化过程见注释 若单片机读到了数据线上的低电平“0”后,还要做延时, 其延时的时间从单片机发出的高电平算起最少要480微秒。 之后单片机将数据线再次拉高到高电平“1”后结束。 */ static unsigned char Init_18b20 (void) { unsigned char x=0; W_B20_CHANNEL_1(); //1、从单片机拉高数据线开始 Delay_Nus(8); //6*n useconds lib51v3.c W_B20_CHANNEL_0(); Delay_Nus(81); W_B20_CHANNEL_1(); //2、单片机拉低数据线480us以上,拉高数据线,释放Delay_Nus(14); //3、之后ds18b20,15~60us以后反应,拉高数据线x=R_B20_CHANNEL(); //4、单片机读取数据线 if(x) return x; Delay_Nus(20); return x; //x=0代表复位成功 } //two static void Write_18b20(unsigned char w_data) { unsigned char i; unsigned char temp; for(i=0;i<8;i++) { W_B20_CHANNEL_1(); temp=w_data&(1<<0); W_B20_CHANNEL_0(); //单片机从高到低,拉低1us以上,并在15us内产生写间隙B20_PORT=temp< Delay_Nus(7); //15~60us内18B20采样 w_data>>=1; } W_B20_CHANNEL_1(); Delay_Nus(4); } //three static unsigned char Read_18b20(void) { unsigned char i; unsigned char temp; unsigned char r_data=0x00; for(i=0;i<8;i++) { r_data>>=1; W_B20_CHANNEL_1(); W_B20_CHANNEL_0(); //从高到低15us内,再到高,产生读间隙 Delay_Nus(1); W_B20_CHANNEL_1(); temp=B20_PORT<<(7-B20_CHANNEL); //读数据,从低位开始 temp&=(1<<7); r_data+=temp; Delay_Nus(8); //整个读一位过程在60~120us