小弟 学习18b20 不会读数
小弟 想用12864显示18b20温度可是对 18b20读数不了解在网上找的程序 没有解释 看不太懂求 哪位前辈传个以前做过的程序或指点一下思路 小弟 万分感谢{:3_52:} void ds_init(){
uchar x=0;
ds=1;
_nop_();_nop_();
ds=0;
tempdelay(85);
ds=1;
tempdelay(16);
}
void tempwrite(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
ds=0;
_nop_();
ds=dat&0x01;
tempdelay(5);
_nop_();
_nop_();
ds=1;
dat=dat>>1;
}
delayms(3);
}
uchar tempread()
{
uchar i,dat;
for(i=0;i<8;i++)
{
ds=0;
_nop_();
_nop_();
dat=dat>>1;
ds=1;
_nop_();
_nop_();
if(ds==1)
dat=dat|0x80;
tempdelay(5);
}
return(dat);
}
uint get_temp()
{
uchar a,b;
ds_init();
tempwrite(0xcc); //写跳过读rom指令
tempwrite(0x44); //写温度转换指令
tempdelay(10);
ds_init();
tempwrite(0xcc);
tempwrite(0xbe);
a=tempread();
b=tempread();
temp=(b<<4)|(a>>4);
if(temp>128)
{temp=~temp+1;}
return temp;
} 楼主应该仔细钻研一下PDF中的时序和命令,然后再对照正规例程来学习。不然总是一知半解的。 Edward3121 发表于 2012-4-12 11:47 static/image/common/back.gif
楼主应该仔细钻研一下PDF中的时序和命令,然后再对照正规例程来学习。不然总是一知半解的。 ...
敢问哪里有正规例程? 我曾经做过一个,你可以看一下
#include<reg52.h>
#include <intrins.h>
sbit ge2=P3^2; //数码管个位
sbit shi2=P3^3; //数码管十位
sbit DQ =P2^7; //定义通信端口1
unsigned char tab[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
//0 1 2 3 4 5 6 7 8 9
void delay(unsigned char i)
{
while(i--);
}
//初始化函数1
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8);//稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for(i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(4);
}
//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
a=a>>4; //低位右移4位,舍弃小数部分
t=b<<4; //高位左移4位,舍弃符号位
t=t|a;
return(t);
}
void display_tempmain(unsigned char i) //1个18b20温度显示函数
{
shi2=1;
ge2=0;
P1=tab; //dis shiwei
delay(200);
P1=0xff;
shi2=0;
ge2=1;
P1=tab; //disgewei
delay(200);
P1=0xff;
delay(200);++
}
//****************************************************
//主函数
void main(void)
{
unsigned char temp;
int i ;
temp = 0;
while(1) //主循环
{
for( i = 0;i<300; i++)
{
display_tempmain(temp);
}
temp=ReadTemperature();
}
}
这个程序是:读取温度在数码管上显示。有不懂得地方可以问我。 这个绝对没问题 讲的也很细致 唉 没穿上来
呵呵,热心的人很多啊, tepaiyuan 发表于 2012-4-12 13:23 static/image/common/back.gif
敢问哪里有正规例程?
正规例程就是形式规范的,不是视频里随手写的那种。 Edward3121 发表于 2012-4-12 18:20 static/image/common/back.gif
正规例程就是形式规范的,不是视频里随手写的那种。
你就给他一份规范的,让他自己学会看时序图 tepaiyuan 发表于 2012-4-12 21:38 static/image/common/back.gif
你就给他一份规范的,让他自己学会看时序图
http://wenku.baidu.com/view/88d013303968011ca30091e6.html
这个差不多吧,我找不到当时学的时候用的了,这个差不多吧…… 到官网去下载技术文档,然后仔细研读,很详细的
http://www.maxim-ic.com/datasheet/index.mvp/id/2812/t/al 小弟在此谢过楼上各位大侠了 Arachne29 发表于 2012-4-12 15:39 static/image/common/back.gif
我曾经做过一个,你可以看一下
#include
大叔我想认识下你,,教教我温度传感器吧 ds18的麻烦说下qq好吗? 18B20已经算是最简单的了,你刚起步应该多自己琢磨,这样才能有所收获。 yesno 发表于 2012-4-18 14:11 static/image/common/back.gif
大叔我想认识下你,,教教我温度传感器吧 ds18的麻烦说下qq好吗?
嘿嘿 很荣幸,我的qq648855521 贴一个
/*DS18B20 正常显示 by whj 11 11 18*/
#include<reg52.h>
//#include<intrins.h>
//#include<absacc.h>
//#include <stdio.h>
#define uintunsigned int
#define uchar unsigned char
sbit DS = P2^0; //DS18B20的数据输入/输出脚DQ,根据情况设定
uchar flag1; //正负标志
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar code wei[]={0x00,0x01,0x02,0x04,0x08};
uint temp,t;
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void)//DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc);// address all drivers on bus
tmpwritebyte(0x44);//initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two bytecompose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp)
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
t=400;
P1= wei;;
if (flag1==1)
{
P0=0x40;
}
else
{
P0=0x00;
}
while(t--);
t=400;
P1= wei;
P0=table;
while(t--);
t=400;
P1= wei;
P0=table1;
while(t--);
t=400;
P1= wei;
P0=table;
while(t--);
P1=wei;
}
void main()
{
uchar a;
do
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
} while(1);
}
页:
[1]